Return to Snippet

Revision: 63669
at May 29, 2013 03:56 by ShawnG


Updated Code
function ratioResize( $image , $maxwidth , $maxheight , $forcewidth=false ){
		$return = array();list( $w , $h ) = getimagesize( $image );
		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['height'] = round($h*($return['ratio']));
		$return['width'] =  round($w*($return['ratio'])); //GET CALCULATED DIMENSIONS FROM RATIO
		$return['left'] = ($maxwidth - $return['width'])/2;
		$return['top'] =  ($maxheight - $return['height'])/2;//GET CALCULATED CENTERING PIXELS
		return $return;
	}

Revision: 63668
at May 29, 2013 03:48 by ShawnG


Initial Code
function ratioResize( $image , $maxwidth , $maxheight , $forcewidth=false ){
		list( $w , $h ) = getimagesize( $image );
		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['height'] = round($h*($return['ratio']));
		$return['width'] =  round($w*($return['ratio'])); //GET CALCULATED DIMENSIONS FROM RATIO
		$return['left'] = ($maxwidth - $return['width'])/2;
		$return['top'] =  ($maxheight - $return['height'])/2;//GET CALCULATED CENTERING PIXELS
		return $return;
	}

Initial URL

                                

Initial Description
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
     );

Initial Title
PHP Image Ratio Width & Height, Top & Left

Initial Tags
php, resize, image

Initial Language
PHP