Resize image on the fly

Posted: April 26, 2012 in PHP
<?php 	
	define('LIBRARY_FOLDER_NAME', 'library');
	define('SITE_URL',"http://localhost/project/");
	define('SITE_PATH',"C:/wamp/www/project/");
	
	function resizeImage($sourceFile,$destFile,$width = '',$height ='')
	{	
		$proportional = true;
		$output = 'file';
		copy($sourceFile,$destFile);
		
		$file = $destFile;
		if ( $height <= 0 && $width <= 0 ) return false;
	
		# Setting defaults and meta
		$info = getimagesize($file);
		$image = '';
		list($width_old, $height_old) = $info;	
		
		$final_width = 0;
		$final_height = 0;			
		
		$dims = resizeBound($width_old, $height_old,$width,$height);
		$final_height=$dims['height'];
		$final_width=$dims['width'];			
	
		# Loading image to memory according to type
		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);		   
	
		# Preparing a method of providing result
		switch ( strtolower($output) ) {
		  case 'browser':
			$mime = image_type_to_mime_type($info[2]);
			header("Content-type: $mime");
			$output = NULL;
		  break;
		  case 'file':
			$output = $file;
		  break;
		  case 'return':
			return $image_resized;
		  break;
		  default:
		  break;
		}
		
		# 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;
		}
		//rename($file,$destFile);
		return true;
	}
	function resizeBound($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 = resizeBound($oldW, $oldH,'',$oldH);
					$finalH = $dims['height'];
					$finalW = $dims['width'];		
				}
				else
				{
					$finalH = $newH;
					$finalW = $tempW;
				}
			}
			else if($newW !="" && $newH == "")
			{
				if($newW >$oldW)
				{
					$dims = resizeBound($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 = resizeBound($oldW, $oldH,$oldW,'');
						$finalH = $dims['height'];
						$finalW = $dims['width'];		
					}
					else
					{
						$finalH = $tempH;
						$finalW = $newW;
					}
				}
				else
				{
					if($newH >$oldH)
					{
						$dims = resizeBound($oldW, $oldH,'',$oldH);
						$finalH = $dims['height'];
						$finalW = $dims['width'];		
					}
					else
					{
						$finalH = $newH;
						$finalW = $tempW;
					}
				}			
			}		
			$dims['height'] = $finalH;
			$dims['width'] = $finalW;
			return $dims;
		}
	function getExtension($filename)
	{
		return end(explode('.', $filename));
	}
	function getResizedImage($libraryImage, $width, $height)
	{
		$temp = explode('/',$libraryImage);
		$file = $temp[count($temp)-1];
		unset($temp[count($temp)-1]);
		$destFolder = implode('/',$temp);
		
		$temp = explode('.',$file);
		$ext = $temp[count($temp)-1];
		unset($temp[count($temp)-1]);
		$destFile = implode('.',$temp).'_'.$width.'X'.$height.'.'.$ext;
		
		$destination = $destFolder.'/'.$destFile;
		if(file_exists(SITE_PATH.LIBRARY_FOLDER_NAME.'/cache/'.$destination)) // file exists return direct url
		{ 
			return SITE_URL.LIBRARY_FOLDER_NAME.'/cache/'.$destination;
		}
		else // generate resize image
		{
			if(!file_exists(SITE_PATH.LIBRARY_FOLDER_NAME.'/cache/'.$destFolder))
			{
				mkdir(SITE_PATH.LIBRARY_FOLDER_NAME.'/cache/'.$destFolder,0,true);
			}
			resizeImage(SITE_PATH.LIBRARY_FOLDER_NAME.'/'.$libraryImage, SITE_PATH.LIBRARY_FOLDER_NAME.'/cache/'.$destination, $width, $height);
			return SITE_URL.LIBRARY_FOLDER_NAME.'/cache/'.$destination;
		}
	}
	
	$libraryImage = 'category/flower.jpg';	
?>
<img src="<?=getResizedImage($libraryImage, 300, 300);?>">
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s