Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rizerzero/7536194 to your computer and use it in GitHub Desktop.
Save rizerzero/7536194 to your computer and use it in GitHub Desktop.
<?php
public function getImageSizeKeepAspectRatio( $imageUrl, $maxWidth, $maxHeight)
{
$imageDimensions = getimagesize($imageUrl);
$imageWidth = $imageDimensions[0];
$imageHeight = $imageDimensions[1];
$imageSize['width'] = $imageWidth;
$imageSize['height'] = $imageHeight;
if($imageWidth > $maxWidth || $imageHeight > $maxHeight)
{
if ( $imageWidth > $imageHeight ) {
$imageSize['height'] = floor(($imageHeight/$imageWidth)*$maxWidth);
$imageSize['width'] = $maxWidth;
} else {
$imageSize['width'] = floor(($imageWidth/$imageHeight)*$maxHeight);
$imageSize['height'] = $maxHeight;
}
}
return $imageSize;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment