Opened 2 years ago
Last modified 5 days ago
#17626 reopened defect (bug)
image_get_intermediate_size() may return wrong thumbnail size
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Media | Version: | |
| Severity: | normal | Keywords: | has-patch needs-testing |
| Cc: | carstenbach |
Description
See this WPSE question for a more detailed explanation of the problem.
Essentially, if get_the_post_thumbnail() is passed an array for the $size argument, and if two images have one dimension exactly the same, the image with the smaller opposing dimension will be returned, even if the dimensions for the other image are declared explicitly.
The issue appears to be due to the way that image_get_intermediate_size() determines if an image exists that is cropped to dimensions similar to the specified $size array. As soon as it finds one, it uses it.
I've attached a patch that first attempts to find an image cropped exactly on both dimensions, before looking for images cropped exactly only on one dimension. There will still be edge cases where the wrong image might be returned, but I'm not sure of the most efficient way to handle such cases.
(Note: props to Rarst for finding the underlying issue.)
Attachments (3)
Change History (21)
chipbennett
— 2 years ago
comment:2
chipbennett
— 2 years ago
I'm trying to work out the best way to cover the remaining edge case. For example, two image sizes have the same width, but different heights. In this case, the image size with the smaller height will return, even if the image size with the larger height has a height that is smaller than the height specified in the $size array.
My thinking is that, if $imagedata['sizes'] can be sorted from high to low on the variable dimension, before looping through the sizes, then the largest, rather than smallest, image would be returned.
Would a function like this work, prior to looping through the images?
// sort $imagemeta['sizes'] descending by width, height
function compare_sizes( $a, $b ) {
$sorted = strnatcmp( $a['width'], $b['width'] );
if( ! $sorted ) return strnatcmp( $a['height'], $b['height'] );
array_reverse( $sorted );
return $sorted;
}
usort( $imagedata['sizes'], 'compare_sizes' );
(I'm a bit out of my depth on this one, with sorting associative arrays. If there's a better way to do this, I'm all ears.)
comment:4
chipbennett
— 2 years ago
@scribu should I update the patch to use uasort() instead of usort()?
comment:5
follow-up:
↓ 6
scribu
— 2 years ago
Note that, even if you define the compare_sizes() function inside the if, it's still created globally.
Better to define it outside of image_get_intermediate_size(), as a private helper function - that is to say with a leading underscore: _compare_sizes(), or something even more obscure, to prevent collisions.
SergeyBiryukov
— 22 months ago
comment:6
in reply to:
↑ 5
SergeyBiryukov
— 22 months ago
Replying to scribu:
Better to define it outside of image_get_intermediate_size(), as a private helper function - that is to say with a leading underscore: _compare_sizes(), or something even more obscure, to prevent collisions.
Done in 17626.patch.
comment:8
SergeyBiryukov
— 19 months ago
Related: #18799
comment:9
chipbennett
— 16 months ago
Hey scribu, any chance we can move on this one?
comment:10
follow-up:
↓ 11
scribu
— 16 months ago
- Milestone changed from Awaiting Review to 3.4
One question: in _compare_imagedata_sizes(), why is strnatcmp() used to compare the dimensions (which are just numbers)?
comment:11
in reply to:
↑ 10
chipbennett
— 16 months ago
Replying to scribu:
One question: in _compare_imagedata_sizes(), why is strnatcmp() used to compare the dimensions (which are just numbers)?
Not sure? Probably because it worked, and I wasn't aware of a better method? :) I can change it though. Suggestions?
comment:12
scribu
— 16 months ago
Just use plain comparison operators: <, >, =, as in the uasort example: http://php.net/uasort#example-4584
comment:13
SergeyBiryukov
— 16 months ago
Noticed an inconsistency in the comments: $imagemeta['sizes'] should be $imagedata['sizes'].
comment:14
nacin
— 14 months ago
- Owner set to nacin
- Resolution set to fixed
- Status changed from new to closed
In [20696]:
comment:15
nacin
— 14 months ago
- Milestone changed from 3.4 to Future Release
- Resolution fixed deleted
- Status changed from closed to reopened
comment:16
SergeyBiryukov
— 6 months ago
Related: #21004
comment:17
carstenbach
— 3 months ago
- Cc carstenbach added
comment:18
roytanck
— 5 days ago
I'm running into this bug, and it's driving me crazy.
- Client uploads a 208*60px image for use in the Image Widget plugin
- Thumbnail size is set to 120*120px
- WP creates a 120*60px version of the image
- image_get_intermediate_size returns the 120*60px one, even though $size is array(208,60)
- Wrong image show in widget
I'm forced to use WP 3.4.2 (which means the plugin uses legacy media lib code) for now, but can reproduce the problem with trunk by calling wp_get_attachment_image directly.
@nacin Any suggestions? I think image_get_intermediate_size should either work with cropped images, or filter them out completely. In the first case, "area" is no longer a valid metric (a 400*100px and a 100*400px crop of the same 400*400px image have the same area).
slight modification to image_get_intermediate_size()