Skip to content

Instantly share code, notes, and snippets.

@admataz
Created March 10, 2015 13:14
Show Gist options
  • Save admataz/4cb7a66d565998171f75 to your computer and use it in GitHub Desktop.
Save admataz/4cb7a66d565998171f75 to your computer and use it in GitHub Desktop.
simple function helps calculate resized dimensions while keeping w/h aspect ratio
<?php
function calc_aspect_ratio($w=0,$h=0,$image_dimensions=array()){
//work out some ratios for the resized dimensions
if($w){
$w_ratio=$w/$image_dimensions[0];
} else {
$w_ratio=1;
}
if($h){
$h_ratio=$h/$image_dimensions[1];
}else{
$h_ratio=1;
}
if($w_ratio<=$h_ratio){
$w=round($image_dimensions[0]*$w_ratio);
$h=round($image_dimensions[1]*$w_ratio);
}else{
$w=round($image_dimensions[0]*$h_ratio);
$h=round($image_dimensions[1]*$h_ratio);
}
return array('w'=>$w, 'h'=>$h);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment