Created
March 7, 2013 23:36
-
-
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.
This file contains 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
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