Skip to content

Instantly share code, notes, and snippets.

@jkaflik
Created April 23, 2013 17:35
Show Gist options
  • Save jkaflik/5445692 to your computer and use it in GitHub Desktop.
Save jkaflik/5445692 to your computer and use it in GitHub Desktop.
PHP ~ calculating thumbnail dimensions
<?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