Created
April 23, 2013 17:35
-
-
Save jkaflik/5445692 to your computer and use it in GitHub Desktop.
PHP ~ calculating thumbnail dimensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function calculateThumbnailDimensions( $dim, $max ) | |
{ | |
$more = max( $dim ); | |
$more_max = $max[ end( array_keys($dim, $more) ) ]; | |
$scale = $more_max / $more; | |
return ($more_max == $max[0]) ? array($max[0], (int)($dim[1] * $scale)) : array((int)($dim[0] * $scale), $max[1]); | |
} | |
function calculateThumbnailDimensions2( $dim, $max ) | |
{ | |
if( $dim[0] > $dim[1] ) | |
{ | |
$ratio = $max[0] / $dim[0]; | |
} | |
else | |
{ | |
$ratio = $max[1] / $dim[1]; | |
} | |
return array( (int)($dim[0] * $ratio), (int)($dim[1] * $ratio) ); | |
} | |
echo implode('x', calculateThumbnailDimensions2( array(100, 53), array( 155, 155 ) ) ) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment