Make WordPress Core


Ignore:
Timestamp:
11/30/2014 07:53:18 PM (10 years ago)
Author:
wonderboymusic
Message:

Use round() instead of floor() when resizing image dimensions.

Updates unit tests.

Props SergeyBiryukov, kitchin.
Fixes #18532.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r30656 r30660  
    380380    $larger_ratio  = max( $width_ratio, $height_ratio );
    381381
    382     if ( intval( $current_width * $larger_ratio ) > $max_width || intval( $current_height * $larger_ratio ) > $max_height )
     382    if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
    383383        // The larger ratio is too big. It would result in an overflow.
    384384        $ratio = $smaller_ratio;
    385     else
     385    } else {
    386386        // The larger ratio fits, and is likely to be a more "snug" fit.
    387387        $ratio = $larger_ratio;
     388    }
    388389
    389390    // Very small dimensions may result in 0, 1 should be the minimum.
    390     $w = max ( 1, intval( $current_width  * $ratio ) );
    391     $h = max ( 1, intval( $current_height * $ratio ) );
     391    $w = max ( 1, (int) round( $current_width  * $ratio ) );
     392    $h = max ( 1, (int) round( $current_height * $ratio ) );
    392393
    393394    // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
    394395    // We also have issues with recursive calls resulting in an ever-changing result. Constraining to the result of a constraint should yield the original result.
    395396    // Thus we look for dimensions that are one pixel shy of the max value and bump them up
    396     if ( $did_width && $w == $max_width - 1 )
     397
     398    // Note: $did_width means it is possible $smaller_ratio == $width_ratio.
     399    if ( $did_width && $w == $max_width - 1 ) {
    397400        $w = $max_width; // Round it up
    398     if ( $did_height && $h == $max_height - 1 )
     401    }
     402
     403    // Note: $did_height means it is possible $smaller_ratio == $height_ratio.
     404    if ( $did_height && $h == $max_height - 1 ) {
    399405        $h = $max_height; // Round it up
    400 
    401     return array( $w, $h );
     406    }
     407
     408    return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height );
    402409}
    403410
     
    460467        $new_h = min($dest_h, $orig_h);
    461468
    462         if ( !$new_w ) {
    463             $new_w = intval($new_h * $aspect_ratio);
    464         }
    465 
    466         if ( !$new_h ) {
    467             $new_h = intval($new_w / $aspect_ratio);
     469        if ( ! $new_w ) {
     470            $new_w = (int) round( $new_h * $aspect_ratio );
     471        }
     472
     473        if ( ! $new_h ) {
     474            $new_h = (int) round( $new_w / $aspect_ratio );
    468475        }
    469476
Note: See TracChangeset for help on using the changeset viewer.