Skip to content

Don't re-generate scaled/rotated images when regenerating a specific image size#224

Merged
swissspidy merged 4 commits intomainfrom
copilot/fix-image-regeneration-issues
Feb 25, 2026
Merged

Don't re-generate scaled/rotated images when regenerating a specific image size#224
swissspidy merged 4 commits intomainfrom
copilot/fix-image-regeneration-issues

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

When using --image_size=<size>, wp media regenerate was unexpectedly re-creating the auto-scaled -scaled.jpg file (WP 5.3+) and potentially re-rotating the original, because wp_generate_attachment_metadata() was called with the original pre-scaled file. This caused unintended side effects: timestamp changes, quality shifts when JPEG settings had changed between upload and regeneration, and retroactive application of scaling/rotation to images uploaded before WP 5.3.

Changes

  • src/Media_Command.php: When --image_size is specified, pass get_attached_file($id) (the already-scaled version for big images) to wp_generate_attachment_metadata() instead of wp_get_original_image_path(). The scaled file is at/below the 2560px threshold so no re-scaling or re-rotation is triggered. Falls back to the original path if the scaled file doesn't exist (small images, pre-WP-5.3 uploads — no behavior change).

  • features/media-regenerate.feature: Added @require-wp-5.3 scenario that installs a jpeg_quality=1 filter after import, runs --image_size=medium, and asserts the MD5 of -scaled.jpg is unchanged — any re-generation would produce detectably different content.

Original prompt

This section details on the original issue you should resolve

<issue_title>Regenerating a single image size (re-)generates auto-scaled big images & auto-rotated images</issue_title>
<issue_description>## Bug Report

Describe the current, buggy behavior

In WP 5.3 two new features for uploaded images got introduced:

Unfortunately for any image affected by the above feature when regenerating any thumbnail size that auto-generated image gets regenerated as well. So even if you use wp media regenerate --image_size=<image_size>.

Under most circumstances this will just recreate an already existing image and won't cause any serious harm.

But it's still not ideal for at least these reasons:

  • it updates the timestamp of the image
  • it changes the image if between the initial upload and the regeneration the used image library changed from GD to Imagick or vice versa.
  • it changes the image if between the initial upload and the regeneration settings have changed, e.g. the JPEG compression
  • it newly creates auto-downscaled and auto-rotated images for files that don't have it yet (e.g. uploaded before WordPress 5.3 or while these features were deactivated)

I've noticed these unexpected changes while desperately attempting to understand what happened during a completely botched media regeneration caused by #130, #136 and this very issue.

Describe how other contributors can replicate this bug

  1. upload a JPG image that is bigger than 2560 in any dimension
  2. ensure that there has been created an image with the postfix -scaled.jpg
  3. note the last modified date of this file in the filesystem
  4. regenerate just one image size for the image: wp media regenerate <attachment-id> --image_size=medium
  5. see that last modified date of the -scaled.jpg file in the filesystem has changed to the time you ran the above command

An alternative approach to test this in a visually noticeable way is to add this snippet in a plugin or themes functions.php between the initial upload and the regeneration. (It reduces the JPEG quality of generated images to "terrible")

add_filter('jpeg_quality', function(){ return 0; });

After the regeneration you should be able to notice that the intentionally regenerated "medium" size thumbnail has terrible compression artifacts, but the -scaled.jpg unexpectedly also has.

Describe what you would expect as the correct outcome

When limiting the media regeneration to a specific image size I expect any other images to be unaffected.

Let us know what environment you are running this on

WordPress 6.4.3

OS:     Linux 6.5.0-26-generic wp-cli/media-command#26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Mar 12 10:22:43 UTC 2 x86_64
Shell:  
PHP binary:     /usr/local/bin/php
PHP version:    8.1.27
php.ini used:   /usr/local/etc/php/php.ini
MySQL binary:   /usr/bin/mysql
MySQL version:  mysql  Ver 15.1 Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:      
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /var/www/html/wp-content/plugins/my-plugin
WP-CLI packages dir:    /home/wordpress/.wp-cli/packages/
WP-CLI cache dir:       /home/wordpress/.wp-cli/cache
WP-CLI global config:   
WP-CLI project config:  
WP-CLI version: 2.10.0
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@danielbachhuber</author><body>
Thanks for the great report, @kraftner !

Feel free to submit a pull request, if you'd like. Here is some [guidance on our pull request best practices](https://make.wordpress.org/cli/handbook/contributions/pull-requests/).</body></comment_new>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…e_size is specified

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix image regeneration for auto-scaled and rotated images Don't re-generate scaled/rotated images when regenerating a specific image size Feb 25, 2026
Copilot AI requested a review from swissspidy February 25, 2026 18:06
@codecov
Copy link

codecov bot commented Feb 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

gemini-code-assist[bot]

This comment was marked as resolved.

@swissspidy swissspidy marked this pull request as ready for review February 25, 2026 23:28
@swissspidy swissspidy requested a review from a team as a code owner February 25, 2026 23:28
@swissspidy swissspidy merged commit 1e8e0f6 into main Feb 25, 2026
60 checks passed
@swissspidy swissspidy deleted the copilot/fix-image-regeneration-issues branch February 25, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regenerating a single image size (re-)generates auto-scaled big images & auto-rotated images

3 participants