Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#55403 closed defect (bug) (fixed)

`wp_crop_image` returning incorrect file path when image formats filtered

Reported by: mat-lipe's profile Mat Lipe Owned by: adamsilverstein's profile adamsilverstein
Milestone: 6.0 Priority: normal
Severity: normal Version: 5.8
Component: Media Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

The wp_crop_image function calculates the destination file path based on the source file's extension.

Version 5.8.0 introduced a new image_editor_output_format filter which allows you to change the output format (extension) of generated image sizes. If the generated image extension is changed, wp_crop_images does not honor the change and returns a broken path to an image with the correct name but the source files original (now incorrect) extension.

Example filter which converts the extensions to webp:

<?php
add_filter( 'image_editor_output_format', fn() => array_fill_keys( [ 'image/jpg', 'image/jpeg', 'image/png' ], 'image/webp' ) );

Recreate steps:

  1. Add the example filter.
  2. Upload a JPEG or PNG to media library.
  3. Edit the uploaded image via the media library.
  4. Crop the image and save it.
  5. Notice the image is now broken and errors will display in browser console.

Change History (11)

This ticket was mentioned in PR #2419 on WordPress/wordpress-develop by lipemat.


4 years ago
#1

  • Keywords has-patch has-unit-tests added

Version 5.8.0 introduced a new image_editor_output_format filter which
allows you to change the output format (extension) of generated image
sizes. If the generated image extension is changed, wp_crop_images must
honor the change and return the correct extension.

Fixes: #55403

Includes: Tests

Trac ticket: https://core.trac.wordpress.org/ticket/55403

#2 @adamsilverstein
4 years ago

  • Milestone changed from Awaiting Review to 6.0
  • Owner set to adamsilverstein
  • Status changed from new to assigned

Hi @mat-lipe - thanks for opening this ticket. Indeed, I see the issue you identified, the the output path is changed on the fly during the save, so we should rely on the path returned from the save call instead of the original destination path.

Fix in patch looks good to me, thanks for adding the test as well. I'd still like to test this via the steps you provided before committing; marking for 6.0.

This ticket was mentioned in Slack in #core by chaion07. View the logs.


4 years ago

#4 @peterwilsoncc
4 years ago

I've added some minor notes to the linked pull request.

@adamsilverstein are you able to take a look at the code and make sure you're happy too? I think beta 3 is a few hours away so if it's commit ready it would be good to get it in.

#5 @adamsilverstein
4 years ago

@peterwilsoncc looks good to me, left a comment one one extra empty line otherwise good to go. Sorry wasn't able to review this more quickly, I'm mostly out of office this week.

#6 @Mat Lipe
4 years ago

All feedback in the pull request has been addressed.

#7 @peterwilsoncc
4 years ago

  • Keywords commit added

#8 @peterwilsoncc
4 years ago

  • Component changed from General to Media

peterwilsoncc commented on PR #2419:


4 years ago
#9

Thanks for making the code changes.

I made a typo in my suggestion yesterday 🤦 so have pushed a fix to your branch. I merged in trunk so I can ensure the tests are still passing before committing.

#10 @peterwilsoncc
4 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 53292:

Media: Ensure wp_crop_image() returns correct file type.

Return the correct file path from wp_crop_image() when a developer modifies the file type with via the image_editor_output_format filter.

Previously the function would return a broken file reference containing the original file extension rather than the one specified via the filter.

Props mat-lipe, adamsilverstein.
Fixes #55403.

Note: See TracTickets for help on using tickets.