WordPress.org

Make WordPress Core

Opened 9 months ago

Closed 9 months ago

Last modified 9 days ago

#44095 closed defect (bug) (duplicate)

wp_generate_attachment_metadata() can overwrite previously uploaded images with the intermediate sized images it creates

Reported by: pbiron Owned by:
Milestone: Priority: normal
Severity: normal Version: trunk
Component: Media Keywords: dev-feedback
Focuses: Cc:

Description

When wp_generate_attachment_metadata() creates/saves intermediate sized images, it can sometimes overwrite an original image from a previous upload.

Steps to reproduce:

  1. set thumbnail size to 150x150
  2. create a 300x300 image named foo-150x150.jpg
  3. upload foo-150x150.jpg
  4. create another 300x300 image named foo.jpg
  5. upload foo.jpg

If you then check the appropriate dir in /wp-content/uploads you will see that the original foo-150x150.jpg has been overwritten by the thumbnail-sized version of foo.jpg.

Original filenames containing -{width}x{height} suffixes can be quite common, especially on artwork-related sites. A real-world example of the "steps to reproduce" above is as follows:

  1. a painter does an oil painting of a sunset on a 150cmx150cm canvas and uploads a photo of that artwork named sunset-150x150.jpg (where, in their mind, the -150x150 suffix is the canvas size of their painting)
  2. a photographer then uploads their photograph of a sunset named sunset.jpg

...and the photographer has just overwritten the painter's original image :-(

Attachments (2)

foo-150x150.jpg (19.7 KB) - added by pbiron 9 months ago.
example image to use in the "steps to reproduce"
foo.jpg (16.2 KB) - added by pbiron 9 months ago.
example image to use in the "steps to reproduce"

Download all attachments as: .zip

Change History (9)

@pbiron
9 months ago

example image to use in the "steps to reproduce"

@pbiron
9 months ago

example image to use in the "steps to reproduce"

#1 @pbiron
9 months ago

  • Summary changed from wp_generate_attachment_metadata() can overwrite previously uploaded images to wp_generate_attachment_metadata() can overwrite previously uploaded images with the intermediate sized images it creates

#2 @pbiron
9 months ago

  • Keywords dev-feedback added

There are a number of places in the process where the intermediate sized filenames could be "unique"-ified, e.g.,

I'm fairly unfamiliar with that part of the core code so I'm not really sure which of those place (and maybe others that I've discovered) would be the safest/most reliable for core's needs.

However, I will note that 2 very widely used plugins (Regenerate Thumbnails and WooCommerce) make calls to WP_Image_Editor::generate_filename() but not WP_Image_Editor_{GD|Imagick}::multi_resize()when they resize images.

So, I'm very interested in the opinion of those who are familiar with both the relevant parts of the core code and how other plugins would deal with any change.

#3 follow-up: @swissspidy
9 months ago

This is a known issue. If I could just find the original ticket right now...

#4 in reply to: ↑ 3 @pbiron
9 months ago

Replying to swissspidy:

This is a known issue. If I could just find the original ticket right now...

I looked to see if it was a known issue before opening this ticket and couldn't find anything.

#5 follow-up: @desrosj
9 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

I think #42437 is the one. Feel free to reopen if this is something different.

#6 in reply to: ↑ 5 @pbiron
9 months ago

Replying to desrosj:

I think #42437 is the one. Feel free to reopen if this is something different.

yes, that ticket is the same problem. I'll join the discussion over there.

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


9 days ago

Note: See TracTickets for help on using tickets.