Make WordPress Core


Ignore:
Timestamp:
02/21/2008 06:00:15 AM (17 years ago)
Author:
ryan
Message:

Image size options from tellyworth. fixes #5933

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/image.php

    r6786 r6952  
    225225 */
    226226function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) {
    227     if ( $height <= $hmax && $width <= $wmax ){
    228         //Image is smaller than max
    229         return array( $width, $height);
    230     } elseif ( $width / $height > $wmax / $hmax ) {
    231         //Image Width will be greatest
    232         return array( $wmax, (int) ($height / $width * $wmax ));
    233     } else {
    234         //Image Height will be greatest
    235         return array( (int) ($width / $height * $hmax ), $hmax );
    236     }
     227    return wp_constrain_dimensions( $width, $height, $wmax, $hmax );
     228}
     229
     230// same as wp_shrink_dimensions, except the max parameters are optional.
     231// if either width or height are empty, no constraint is applied on that dimension.
     232function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
     233    if ( !$max_width and !$max_height )
     234        return array( $current_width, $current_height );
     235   
     236    $width_ratio = $height_ratio = 1.0;
     237   
     238    if ( $max_width > 0 && $current_width > $max_width )
     239        $width_ratio = $max_width / $current_width;
     240   
     241    if ( $max_height > 0 && $current_height > $max_height )
     242        $height_ratio = $max_height / $current_height;
     243   
     244    // the smaller ratio is the one we need to fit it to the constraining box
     245    $ratio = min( $width_ratio, $height_ratio );
     246   
     247    return array( intval($current_width * $ratio), intval($current_height * $ratio) );
    237248}
    238249
Note: See TracChangeset for help on using the changeset viewer.