Created
March 10, 2015 13:14
-
-
Save admataz/4cb7a66d565998171f75 to your computer and use it in GitHub Desktop.
simple function helps calculate resized dimensions while keeping w/h aspect ratio
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
<?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