diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index 02bf5d32..9dfc015d 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -832,6 +832,43 @@ Feature: Regenerate WordPress attachments Success: Regenerated 1 of 1 images. """ + @require-wp-5.3 + Scenario: Regenerating a specific image size should not regenerate the scaled version of big images + Given download: + | path | url | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | + And I run `wp option update uploads_use_yearmonth_folders 0` + + When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported attachment" --porcelain` + Then save STDOUT as {LARGE_ATTACHMENT_ID} + And the wp-content/uploads/large-image-scaled.jpg file should exist + And the wp-content/uploads/large-image-300x225.jpg file should exist + + # Save a checksum of the scaled image before any regeneration. + When I run `md5sum wp-content/uploads/large-image-scaled.jpg` + Then save STDOUT as {SCALED_CHECKSUM} + + # Add a filter that would produce drastically different results if the scaled image were regenerated. + Given a wp-content/mu-plugins/media-settings.php file: + """ + add_site_icon_filter( $id ); - $metadata = wp_generate_attachment_metadata( $id, $fullsizepath ); + // When regenerating a specific image size, use the file that WordPress normally + // serves (the scaled version for big images), not the original pre-scaled file. + // This prevents wp_generate_attachment_metadata() from re-creating the scaled + // version or auto-rotating the original during specific-size regeneration. + $generate_file = $fullsizepath; + if ( $image_size && ! $is_pdf ) { + $wp_attached_file = \get_attached_file( $id ); + if ( $wp_attached_file && file_exists( $wp_attached_file ) ) { + $generate_file = $wp_attached_file; + } + } + + $metadata = wp_generate_attachment_metadata( $id, $generate_file ); if ( $site_icon_filter ) { remove_filter( 'intermediate_image_sizes_advanced', $site_icon_filter );