Skip to content

Commit 9938e9e

Browse files
Copilotswissspidy
andcommitted
Fix: prevent scaled image and auto-rotation re-generation when --image_size is specified
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 4b7410e commit 9938e9e

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

features/media-regenerate.feature

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,43 @@ Feature: Regenerate WordPress attachments
832832
Success: Regenerated 1 of 1 images.
833833
"""
834834

835+
@require-wp-5.3
836+
Scenario: Regenerating a specific image size should not regenerate the scaled version of big images
837+
Given download:
838+
| path | url |
839+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
840+
And I run `wp option update uploads_use_yearmonth_folders 0`
841+
842+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported attachment" --porcelain`
843+
Then save STDOUT as {LARGE_ATTACHMENT_ID}
844+
And the wp-content/uploads/large-image-scaled.jpg file should exist
845+
And the wp-content/uploads/large-image-300x225.jpg file should exist
846+
847+
# Save a checksum of the scaled image before any regeneration.
848+
When I run `md5sum wp-content/uploads/large-image-scaled.jpg`
849+
Then save STDOUT as {SCALED_CHECKSUM}
850+
851+
# Add a filter that would produce drastically different results if the scaled image were regenerated.
852+
Given a wp-content/mu-plugins/media-settings.php file:
853+
"""
854+
<?php
855+
add_filter( 'jpeg_quality', function() { return 1; } );
856+
"""
857+
858+
# Regenerate only the medium size - scaled image should not be touched.
859+
When I run `wp media regenerate {LARGE_ATTACHMENT_ID} --image_size=medium --yes`
860+
Then STDOUT should contain:
861+
"""
862+
Regenerated "medium" thumbnail for "My imported attachment"
863+
"""
864+
865+
# Verify the scaled image was NOT regenerated (checksum should be unchanged).
866+
When I run `md5sum wp-content/uploads/large-image-scaled.jpg`
867+
Then STDOUT should be:
868+
"""
869+
{SCALED_CHECKSUM}
870+
"""
871+
835872
@require-wp-4.7.3 @require-extension-imagick
836873
Scenario: Regenerate a specific image size for a PDF attachment
837874
Given download:

src/Media_Command.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,19 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $delete
666666
return;
667667
}
668668

669-
$metadata = wp_generate_attachment_metadata( $id, $fullsizepath );
669+
// When regenerating a specific image size, use the file that WordPress normally
670+
// serves (the scaled version for big images), not the original pre-scaled file.
671+
// This prevents wp_generate_attachment_metadata() from re-creating the scaled
672+
// version or auto-rotating the original during specific-size regeneration.
673+
$generate_file = $fullsizepath;
674+
if ( $image_size && ! $is_pdf ) {
675+
$wp_attached_file = \get_attached_file( $id );
676+
if ( $wp_attached_file && file_exists( $wp_attached_file ) ) {
677+
$generate_file = $wp_attached_file;
678+
}
679+
}
680+
681+
$metadata = wp_generate_attachment_metadata( $id, $generate_file );
670682

671683
// Note it's possible for no metadata to be generated for PDFs if restricted to a specific image size.
672684
if ( empty( $metadata ) && ! ( $is_pdf && $image_size ) ) {

0 commit comments

Comments
 (0)