Skip to content

Commit 1e8e0f6

Browse files
Copilotswissspidy
andauthored
Don't re-generate scaled/rotated images when regenerating a specific image size (#224)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent e6fd286 commit 1e8e0f6

2 files changed

Lines changed: 50 additions & 2 deletions

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,21 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $delete
665665
++$successes;
666666
return;
667667
}
668-
669668
$site_icon_filter = $this->add_site_icon_filter( $id );
670669

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

673684
if ( $site_icon_filter ) {
674685
remove_filter( 'intermediate_image_sizes_advanced', $site_icon_filter );

0 commit comments

Comments
 (0)