Make WordPress Core

Opened 7 years ago

Last modified 17 months ago

#18532 closed defect (bug)

resized image dimensions incorrectly floored instead of rounded — at Initial Version

Reported by: _ck_ Owned by:
Milestone: 4.1 Priority: normal
Severity: normal Version: 2.5
Component: Media Keywords: has-patch
Focuses: Cc:


Since WP 2.5 no one has noticed this, not sure if I am surprised.

Image dimensions are incorrectly floored instead of rounded to a proper number. Some attempt at "fixup" is done later in some processes to move them by a pixel, but still the way it's initially calculated is wrong.

For example:

A typical photo has a 3:2 (3/2 = 1.5) ratio.

If it's resized to 600px width, WP will make the height 399px - this is wrong.

It happens because of function wp_constrain_dimensions in media.php (~line 304) which floors the float using intval, which is wrong.

	$w = intval( $current_width  * $ratio );
	$h = intval( $current_height * $ratio );

I propose

	$w = round( $current_width  * $ratio );
	$h = round( $current_height * $ratio );

Earlier in the function, both $ratio and $current_width are already handled as numbers, so they have to be valid numbers, no need to use intval for that reason, it's just being used to floor, which is bad in this case.

Using round is not a performance issue because this function is not used in realtime output for templates but typically in admin area.

Optionally while you are at it, put a filter on wp_constrain_dimensions and pass the filter all the values passed to the function before the array is returned at the end.

Change History (0)

Note: See TracTickets for help on using tickets.