I was struggling long for proper image resize function for maintaining image ratio.
Finally i end end up with merging and changing all codes and it works !

function resizeImage($sourceFile,$destFile,$width,$height)
sourceFile => Full path along with filename
destinationFile => Full path along with filename

I hope this will help coders..

moonknight33 commented: very good :) +0
<?
function resizeImage($sourceFile,$destFile,$width,$height)
{	
	$proportional = true;
	$output = 'file';
	copy($sourceFile,$destFile);
	
	$file = $destFile;
	if ( $height <= 0 && $width <= 0 ) return false;

	# get image size
	$info = getimagesize($file);
	$image = '';
	list($width_old, $height_old) = $info;	
	
	$final_width = 0;
	$final_height = 0;			
	
	$dims = resizeBoundary($width_old, $height_old,$width,$height);
	$final_height=$dims['height'];
	$final_width=$dims['width'];			

	# image loading
	switch ( $info[2] ) {
	  case IMAGETYPE_GIF: $image = imagecreatefromgif($file); break;
	  case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($file); break;
	  case IMAGETYPE_PNG: $image = imagecreatefrompng($file); break;
	  default: return false;
	}		
	
	# This is the resizing/resampling/transparency-preserving magic
	$image_resized = imagecreatetruecolor( $final_width, $final_height );
	if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
	  $transparency = imagecolortransparent($image);

	  if ($transparency >= 0) {
		$transparent_color = imagecolorsforindex($image, $trnprt_indx);
		$transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
		imagefill($image_resized, 0, 0, $transparency);
		imagecolortransparent($image_resized, $transparency);
	  }
	  elseif ($info[2] == IMAGETYPE_PNG) {
		imagealphablending($image_resized, false);
		$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
		imagefill($image_resized, 0, 0, $color);
		imagesavealpha($image_resized, true);
	  }
	}
	imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);	   

	$output = $file;
		
	# Writing image according to type to the output destination
	switch ( $info[2] ) {
	  case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
	  case IMAGETYPE_JPEG: imagejpeg($image_resized, $output); break;
	  case IMAGETYPE_PNG: imagepng($image_resized, $output); break;
	  default: return false;
	}
	return true;

}
function resizeBoundary($oldW,$oldH,$newW='',$newH='')
{		
		if(!($oldW>0 && $oldH>0))
			return;
		$tempW = ( $oldW * $newH ) / ( $oldH ); 
		$tempH = ( $oldH * $newW ) / ( $oldW );
		
		if($newW =="" && $newH != "")
		{
			if($newH >$oldH)
			{
				$dims = resizeBoundary($oldW, $oldH,'',$oldH);
				$finalH = $dims['height'];
				$finalW = $dims['width'];		
			}
			else
			{
				$finalH = $newH;
				$finalW = $tempW;
			}
		}
		else if($newW !="" && $newH == "")
		{
			if($newW >$oldW)
			{
				$dims = resizeBoundary($oldW, $oldH,$oldW,'');
				$finalH = $dims['height'];
				$finalW = $dims['width'];		
			}
			else
			{
				$finalH = $tempH;
				$finalW = $newW;
			}
		}
		else if($newW !="" && $newH != "")
		{		
			if($tempW > $newW)
			{
				if($newW >$oldW)
				{
					$dims = resizeBoundary($oldW, $oldH,$oldW,'');
					$finalH = $dims['height'];
					$finalW = $dims['width'];		
				}
				else
				{
					$finalH = $tempH;
					$finalW = $newW;
				}
			}
			else
			{
				if($newH >$oldH)
				{
					$dims = resizeBoundary($oldW, $oldH,'',$oldH);
					$finalH = $dims['height'];
					$finalW = $dims['width'];		
				}
				else
				{
					$finalH = $newH;
					$finalW = $tempW;
				}
			}			
		}		
		$dims['height'] = $finalH;
		$dims['width'] = $finalW;
		return $dims;
	}
	
resizeImage('test/computer.jpg','test/thumb/computer.jpg','200','200');

?>

thanks

I really appreciate it :-)

This is what I have been trying to do! Thanks much appreciated !

Very good! Thank you.
But what if the file extension is unknown (user uploads)?
That is, $sourceFile and $destFile of unknown extension.
Help me please?

Line 23-29 does a MIME type check. You can add an extension based on that info.

I solved the problem in another way.
In my code I rename files when uploading users (unique filename).
It works!

$sourceFile  = 'test.gif';
$destName = '1'; // its sample, i get unique filename from DB id
$destFile = $destName.'.'.pathinfo($sourceFile, PATHINFO_EXTENSION);

resizeImage($sourceFile, $destFile, '460', '460');

Function is very good, excellent defines formats. But can someone tell me a universal way with extensions?
And animated GIF function does not resize. It's probably best not to let then upload and resize animated gifs.

To detect an animated gif you'll have to read/parse the header of the gif file.

Yes, I have this function.
But resize for animated GIF (function from post) not working.

    function is_ani($sourceFile) {
    return (bool)preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', file_get_contents($sourceFile));
    }

    if(is_ani($sourceFile)) { 
    echo "Animated";
    } else { 
    echo "Static";
    }   
Member Avatar
diafol