Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_constrain_dimensions [ WordPress Function ]

wp_constrain_dimensions ( $current_width, $current_height, $max_width = 0, $max_height = 0 )
Parameters:
  • (int) $current_width Current width of the image.
  • (int) $current_height Current height of the image.
  • (int) $max_width Optional. Maximum wanted width.
  • (int) $max_height Optional. Maximum wanted height.
Returns:
  • (array) First item is the width, the second item is the height.
Defined at:



Calculates the new dimensions for a downsampled image.

If either width or height are empty, no constraint is applied on that dimension.

Source


<?php
function wp_constrain_dimensions$current_width$current_height$max_width=0$max_height=) {
    if ( !
$max_width and !$max_height )
        return array( 
$current_width$current_height );

    
$width_ratio $height_ratio 1.0;
    
$did_width $did_height false;

    if ( 
$max_width && $current_width && $current_width $max_width ) {
        
$width_ratio $max_width $current_width;
        
$did_width true;
    }

    if ( 
$max_height && $current_height && $current_height $max_height ) {
        
$height_ratio $max_height $current_height;
        
$did_height true;
    }

    
// Calculate the larger/smaller ratios
    
$smaller_ratio min$width_ratio$height_ratio );
    
$larger_ratio  max$width_ratio$height_ratio );

    if ( 
intval$current_width $larger_ratio ) > $max_width || intval$current_height $larger_ratio ) > $max_height )
         
// The larger ratio is too big. It would result in an overflow.
        
$ratio $smaller_ratio;
    else
        
// The larger ratio fits, and is likely to be a more "snug" fit.
        
$ratio $larger_ratio;

    
$w intval$current_width  $ratio );
    
$h intval$current_height $ratio );

    
// Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
    // 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.
    // Thus we look for dimensions that are one pixel shy of the max value and bump them up
    
if ( $did_width && $w == $max_width )
        
$w $max_width// Round it up
    
if ( $did_height && $h == $max_height )
        
$h $max_height// Round it up

    
return array( $w$h );
}
?>

Examples [ wp-snippets.com ]

Google Arama Sonuçlarý

Dahasý ...

Kullanýcý Tartýþmalarý [ wordpress.org ]

- Bulunamadý -

Yeni bir konu yaz ...

0 User Note(s)

Henüz yok. Ýlk sen ol!

Yeni Ekle ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics