Skip to content

Instantly share code, notes, and snippets.

@rbk
Created August 7, 2015 19:09
Show Gist options
  • Save rbk/ea5e73b390ac6f72b91c to your computer and use it in GitHub Desktop.
Save rbk/ea5e73b390ac6f72b91c to your computer and use it in GitHub Desktop.
Calculate width based on max height
function rbk_calculate_image_width_based_on_max_height( $filename, $max_height ) {
if( !empty($filename) ){
$attributes = getimagesize( $filename );
$image_width = $attributes[0];
$image_height = $attributes[1];
if( $image_width > $image_height ){
return $image_width / $image_height * $max_height;
} else {
return $image_height / $image_width * $max_height;
}
}
}
$width = rbk_calculate_image_width_based_on_max_height( $logo, 46 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment