Image resize function

vibhaJ 1 Tallied Votes 334 Views Share

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');

?>
Tko_1 0 Junior Poster

thanks

moonknight33 0 Newbie Poster

I really appreciate it :-)

coullone 0 Newbie Poster

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

laminator 0 Newbie Poster

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?

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

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

laminator 0 Newbie Poster

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.

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

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

laminator 0 Newbie Poster

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 for diafol
diafol
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.