#55403 closed defect (bug) (fixed)
`wp_crop_image` returning incorrect file path when image formats filtered
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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:
- Add the example filter.
- Upload a JPEG or PNG to media library.
- Edit the uploaded image via the media library.
- Crop the image and save it.
- 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
#2
@
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
@
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
@
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.
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.
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