WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 5 days ago

#17626 reopened defect (bug)

image_get_intermediate_size() may return wrong thumbnail size

Reported by: chipbennett Owned by: nacin
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)

media.php.diff (1.2 KB) - added by chipbennett 2 years ago.
slight modification to image_get_intermediate_size()
media.php.sortimagedata.diff (1.6 KB) - added by chipbennett 2 years ago.
Replaced usort() with uasort(), per @scribu's recommendation
17626.patch (1.9 KB) - added by SergeyBiryukov 22 months ago.

Download all attachments as: .zip

Change History (21)

chipbennett2 years ago

slight modification to image_get_intermediate_size()

comment:1 scribu2 years ago

  • Keywords needs-testing added

I've ran into this issue myself.

comment:2 chipbennett2 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:3 scribu2 years ago

You can use uasort().

comment:4 chipbennett2 years ago

@scribu should I update the patch to use uasort() instead of usort()?

chipbennett2 years ago

Replaced usort() with uasort(), per @scribu's recommendation

comment:5 follow-up: scribu2 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.

SergeyBiryukov22 months ago

comment:6 in reply to: ↑ 5 SergeyBiryukov22 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:7 scribu22 months ago

Related: #18532

comment:9 chipbennett16 months ago

Hey scribu, any chance we can move on this one?

comment:10 follow-up: scribu16 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 chipbennett16 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 scribu16 months ago

Just use plain comparison operators: <, >, =, as in the uasort example: http://php.net/uasort#example-4584

comment:13 SergeyBiryukov16 months ago

Noticed an inconsistency in the comments: $imagemeta['sizes'] should be $imagedata['sizes'].

comment:14 nacin14 months ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In [20696]:

Have the $fields argument for XML-RPC's wp.getPostType, getPostTypes, wp.getTaxonomy and wp.getTaxonomies take the names of the post type or taxonomy object keys that will be returned.

  • capabilities becomes cap
  • object_types becomes object_type

fixes #17626.

comment:15 nacin14 months ago

  • Milestone changed from 3.4 to Future Release
  • Resolution fixed deleted
  • Status changed from closed to reopened

[20696] was meant for #20566.

Re-opening, and also punting.

comment:17 carstenbach3 months ago

  • Cc carstenbach added

comment:18 roytanck5 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).

Note: See TracTickets for help on using tickets.