/ Published in: PHP
Returns array with information like:
Input image
width 200
height 200
print_r(ratioResize('path/to/image.ext' , 100 , 50));
output:
array(
[ratio] => .5,
[height] => 50,
[width] => 50,
[left] => 25,
[top] => 25
);
Input image
width 200
height 200
print_r(ratioResize('path/to/image.ext' , 100 , 50));
output:
array(
[ratio] => .5,
[height] => 50,
[width] => 50,
[left] => 25,
[top] => 25
);
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function ratioResize( $image , $maxwidth , $maxheight , $forcewidth=false ){ if($w < $maxwidth && $h < $maxheight && !$forcewidth)return array('width'=>$w,'height'=>$h,'left'=>($maxwidth-$w)/2,'top'=>($maxheight-$h)/2,'ratio'=>1); $rw = $maxwidth/$w; $rh = $maxheight/$h; //GET HEIGHT AND WIDTH RATIO $return['ratio'] = $rw <= $rh?$rw:$rh; $return['left'] = ($maxwidth - $return['width'])/2; $return['top'] = ($maxheight - $return['height'])/2;//GET CALCULATED CENTERING PIXELS return $return; }