Skip to content

Instantly share code, notes, and snippets.

@billjohnston
Created March 7, 2013 23:36
Show Gist options
  • Save billjohnston/5112889 to your computer and use it in GitHub Desktop.
Save billjohnston/5112889 to your computer and use it in GitHub Desktop.
A function that returns the height or width of an image so that it is optimized for a bounding box set to overflow:hidden. The images will keep their aspect ratio and fit the box.
function smartCrop($height,$width,$endHeight,$endWidth){
$heightWidth='';
if($height <= $endHeight || $width <= $endWidth){
$height = round($endWidth*$height/$width);
if($height < $endHeight){
$width = round($endHeight*$width/$height);
$heightWidth = "width:".$width."px";
}
else{
$heightWidth = "height:".$height."px";
}
}
return $heightWidth;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment