PHP Image Upload Resizer

PHP Image Upload Resizer

PHP Image Upload Resizer

Need a good way to resize your uploaded image?   While keeping the height/width ratio?

Why would you need to do this?  Well, like most of you, I did not need to keep full sized images that I upload on page, and needed a good way to create thumbnails, and resize the originals down to a more manageable, more web friendly size.

This will do the trick…  no words of caution, no instructions, just code… use it how you like 🙂

<?php
	function ResizeImage($inputFile, $filepath, $ext, $maxWidth, $maxHeight){
		/* Get some details about the image */
		$srcDetails = getimagesize($inputFile);
		switch ($srcDetails[2]) {
			case 1: //GIF
				$source_image = imagecreatefromgif($inputFile);
				break;			
			case 2: //JPEG
				$source_image = imagecreatefromjpeg($inputFile);
				break;			
			case 3: //PNG
				$source_image = imagecreatefrompng($inputFile);
				break;			
			case 6: //WBMP
				$source_image = imagecreatefromwbmp($inputFile);
				break;			
			default:
				break;
		}		
		/* Original Dimensions */
		$width = imagesx($source_image);
		$height = imagesy($source_image);	
		// Target and original image ratio
		$target_ratio = $maxWidth / $maxHeight;
		$original_ratio = $width / $height;	
		/* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */
		if(($maxWidth <= $width) || $maxHeight <= $height){
			if($target_ratio == $original_ratio){
				// ratio's are equal, no need to calculate
				$desired_height = $maxHeight;
				$desired_width = $maxWidth;
			}elseif($target_ratio > $original_ratio){
				// need to get the proper width
				$desired_height = $maxHeight;
				$desired_width = $width * ($maxHeight / $height);
			}elseif($target_ratio < $original_ratio){
				// need to get the proper height
				$desired_height = $height * ($maxWidth / $width);
				$desired_width = $maxWidth;
			}
		}else{
			$desired_height = $maxHeight;
			$desired_width = $maxWidth;
		}
		$virtual_image = imagecreatetruecolor($desired_width, $desired_height);		
		imagecopyresized($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);		
		/* create the physical thumbnail image to its destination */
		switch ($srcDetails[2]) {
			case 1: //GIF
				imagegif($virtual_image, $filepath);
				imagedestroy($virtual_image);
				break;			
			case 2: //JPEG
				imagejpeg($virtual_image, $filepath, 100);
				imagedestroy($virtual_image);
				break;			
			case 3: //PNG
				imagepng($virtual_image, $filepath, 6);
				imagedestroy($virtual_image);
				break;			
			case 6: //WBMP
				imagewbmp($virtual_image, $filepath);
				imagedestroy($virtual_image);
				break;			
			default: // Don't do anything because it's not an image
				imagedestroy($virtual_image);
				break;			
		}
	}
?>

Enjoy, and as always…

Happy Coding!

Categories


Let Us Help

Get You Online

Contact Us Today

Important Cookie Information
Our website uses cookies. By continuing to browse the site you are agreeing to our use of cookies. For more details about cookies and their use, please see our Cookie Policy.