Make WordPress Core

Opened 6 years ago

Last modified 13 months ago

#44127 new defect (bug)

editing an image can result in intermediate sized images left over after the image is deleted

Reported by: pbiron's profile pbiron Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.5
Component: Media Keywords:
Focuses: Cc:

Description

Steps to reproduce:

  1. set thumbnail_size to 150x150
  2. upload a 300x300 image, e.g., foo.jpg
    • will result in /wp-content/uploads/foo-150x150.jpg being generated
  3. edit the image and scale to 200x200
    • will result in something like /wp-content/uploads/foo-e1526583456941.jpg being generated
    • /wp-content/uploads/foo-150x150.jpg will still be present, since it is still a valid thumbnail for the scaled image
  4. regenerate intermediate sized images (e.g., by calling wp_get_attachment_metadata() followed by wp_update_attachment_metadata(), which the Regenerate Thumbnails plugin does)
    • will result in something like /wp-content/uploads/foo-e1526583456941-150x150.jpg being generated
  5. "Delete Permanently" the image

Check /wp-content/uploads and you will see that foo-150x150.jpg is still there.

Attachments (4)

MediaEditBackupSizes.php (11.2 KB) - added by pbiron 6 years ago.
There are currently no unit tests for the creation/modification of the _wp_attachment_backup_sizes postmeta by wp_save_image(). So, before moving forward with a patch for this ticket I'm proposing these tests as benchmark on existing behavior. Add this file to /tests/phpunit/tests/ajax.
MediaEditBackupSizes.xml (1.2 KB) - added by pbiron 6 years ago.
This is a phpunit config file to run those tests in MediaEditBackupSizes.php that depend on IMAGE_EDIT_OVERWRITE being false (or are agnostic to its value). Add this file to /tests/phpunit and then run phpunit -c tests/phpunit/MeditEditBackupSizes.xml
MediaEditBackupSizesImageEditOverwrite.xml (1.2 KB) - added by pbiron 6 years ago.
This is a phpunit config file to run those tests in MediaEditBackupSizes.php that depend on IMAGE_EDIT_OVERWRITE being true (or are agnostic to its value). Add this file to /tests/phpunit and then run phpunit -c tests/phpunit/MeditEditBackupSizesImageEditOverwrite.xml
MediaEditBackupSizes-1.php (10.3 KB) - added by pbiron 6 years ago.
This fixes the bug I just mentioned. Do not use MediaEditBackupSizes.php!!! The bug was that the $_REQUEST array wasn't being emptied before setting up each new request; as a result, the testScaleImageImageEditOverwrite() and testMultipleScaleImageImageEditOverwrite() were actually doing the crop from the previous test instead of the scale they were supposed to be doing and I had constructed the $expected value for that "incorrect" operation rather than what it should it should have been. This version corrects those tests as well as fixing _setUpRequest().

Download all attachments as: .zip

Change History (14)

#1 @pbiron
6 years ago

There are probably a number of ways to address this but my suggestion is:

  1. in wp_save_image(), augment the _wp_attachment_backup_sizes postmeta to contain the info in the sizes portion of the wp_attachment_metadata postmeta
  2. in wp_delete_attachment(), use that augmented info to delete the files that are currently not being deleted

This ticket was mentioned in Slack in #core-media by pbiron. View the logs.


6 years ago

This ticket was mentioned in Slack in #core-media by pbiron. View the logs.


6 years ago

This ticket was mentioned in Slack in #core-media by pbiron. View the logs.


6 years ago

This ticket was mentioned in Slack in #core-media by pbiron. View the logs.


6 years ago

@pbiron
6 years ago

There are currently no unit tests for the creation/modification of the _wp_attachment_backup_sizes postmeta by wp_save_image(). So, before moving forward with a patch for this ticket I'm proposing these tests as benchmark on existing behavior. Add this file to /tests/phpunit/tests/ajax.

@pbiron
6 years ago

This is a phpunit config file to run those tests in MediaEditBackupSizes.php that depend on IMAGE_EDIT_OVERWRITE being false (or are agnostic to its value). Add this file to /tests/phpunit and then run phpunit -c tests/phpunit/MeditEditBackupSizes.xml

@pbiron
6 years ago

This is a phpunit config file to run those tests in MediaEditBackupSizes.php that depend on IMAGE_EDIT_OVERWRITE being true (or are agnostic to its value). Add this file to /tests/phpunit and then run phpunit -c tests/phpunit/MeditEditBackupSizesImageEditOverwrite.xml

#6 @pbiron
6 years ago

This is the 1st of several comments calling out specific questions in the unit tests.

I wrote tests for how scale and crop effect _wp_attachment_backup_sizes but not for rotate and flip because it doesn't seem to me that they were necessary as those operations produce the same mods on _wp_attachment_backup_sizes as crop does.

Can someone who knows the code better than I do confirm that tests for rotate and flip aren't necessary?

#7 @pbiron
6 years ago

Argh!!! I just discovered a MAJOR bug with the tests and found they are not testing what I thought they were testing. I'll upload another version when I get that bug fixed.

@pbiron
6 years ago

This fixes the bug I just mentioned. Do not use MediaEditBackupSizes.php!!! The bug was that the $_REQUEST array wasn't being emptied before setting up each new request; as a result, the testScaleImageImageEditOverwrite() and testMultipleScaleImageImageEditOverwrite() were actually doing the crop from the previous test instead of the scale they were supposed to be doing and I had constructed the $expected value for that "incorrect" operation rather than what it should it should have been. This version corrects those tests as well as fixing _setUpRequest().

#8 @pbiron
6 years ago

We now return you to your regularly scheduled questions :-)

Does anyone think there is a need for a test that includes a scale and a crop on the same image?

This ticket was mentioned in Slack in #core-media by pbiron. View the logs.


5 years ago

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


13 months ago

Note: See TracTickets for help on using tickets.