Image Resize And Upload Using PHP

Image Resize And Upload Using PHP

In this article I’ve taught about Image Resize And Upload with PHP (Using GD Library feature). Once one of my friend was trying to resize the image with php by using a code snippet but couldn’t do because the article he was reading had not any live demo. Then he asked me on facebook chat for another php code to resize the image. I checked his script and decided to rewrite that code again with a perfect live demo for dummies. The code is very easy to understand and configurable according to your requirement. Let’s have a look at the script.

Image Resize And Upload Using PHP

View Demo Of Image Resize And Upload Using PHP

The script has only a single php page with a simple HTML file uploading form as you are seeing below:

<?php
/*
 * @author Manish Jangir
 * @website: www.blogaddition.com
 * @information: Touch only configuration part
 */
/* Configuration Part Starts Here */

$config = array(
				'200'=>'image/200x200/',
				'100'=>'image/100x100/',
				'80'=>'image/60x60/',
				);
$max_size = 400; //Maximum size of image which you allow to upload
$allowed_extenstion = array('jpg','jpeg','png','gif'); //Allowed file extensions

/* Configuration Part Ends Here */

/*
 * The following function can be used to get the image extension
*/
function getExtension($str) {

	$i = strrpos($str,".");
	if (!$i) { return ""; }

	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l);
	return $ext;
}

/*
 * The following function will create a blank image with the same extension of uploaded file
 */
 function get_src($extension,$temp_path){
 	$image_src = '';
 	switch ($extension)
 	{
 		case 'jpg':
 			$image_src = imagecreatefromjpeg($temp_path);
 			break;
 		case 'jpeg':
 			$image_src = imagecreatefromjpeg($temp_path);
 			break;
 		case 'png':
 			$image_src = imagecreatefrompng($temp_path);
 			break;
 		case 'png':
 			$image_src = imagecreatefromgif($temp_path);
 			break;
 		default:
 			echo "File extension is not valid";
 	}
 	return $image_src;
 }

 /*
  * This is the main function which creates a new image and uploads
  * it to the respective directory
 */
 function new_image($width,$height,$new_width,$src,$upload_path){
 	$new_height =($height/$width)*$new_width;
	$new_image = imagecreatetruecolor($new_width,$new_height); //return a black image of specific size
	imagecopyresampled($new_image,$src,0,0,0,0,$new_width,$new_height,$width,$height);
	imagejpeg($new_image,$upload_path,100);
	//imagedestroy($src);
	imagedestroy($new_image);
 }

 if(isset($_POST['submit']))
 {
 	 $image_name =$_FILES["file"]["name"];
	 $temp_path = $_FILES['file']['tmp_name'];
  if (isset ( $image_name ) && $image_name != '') {
  	$error = 0;
  		list($width,$height) = getimagesize($temp_path); //return 7 elements (first 2 are width and height)
		$extension = getExtension ( strtolower ( $image_name ) );
		if (! in_array ( $extension, $allowed_extenstion )) {
			$msg = '<font color="#ff0000">This image type is not allowed</font>';
		} else {
			$image_size = filesize ( $_FILES ['file'] ['tmp_name'] );
			if ($image_size > $max_size * 1024) {
				$msg = '<font color="#ff0000">You have exceeded the size limit</font>';
			}

			$src = get_src ($extension, $temp_path);
			$uploaded_files = array();
			foreach($config as $newsize=>$newpath){
				$new_file_path = $newpath.$_FILES ['file'] ['name'];
				$uploaded_files[] = $new_file_path;
				$upload = new_image ( $width, $height, $newsize, $src, $new_file_path );

			}
		}
		$msg = "<font color='#23810A'>Image Uploaded Successfully</font>";
	}
}

 ?>

<html>
<body>
<style type="Text/css">
 body{
 	font-family:arial;
 	font-size:12px;
 }
 </style>
<div style="margin:auto;padding-top:20px;width:400px;">
	<form action="index.php" method="post" enctype="multipart/form-data">
		<input type="file" name="file" id="file"><input type="submit" name="submit" value="Submit">
	</form>
	<?php
	if(isset($msg))
	{
		echo $msg.'<br/><br/>';
	}
	if(isset($uploaded_files)){
		foreach($uploaded_files as $filesrc){
			echo '<img src="'.$filesrc.'"/>&nbsp;&nbsp;';
		}
	}
		?>
		</div>
</body>
</html>

How to use this script: Just download the script and extract it to your root folder of wamp server or whatever you’ve installed to make your computer local server. Set the new image size and upload folder in the configuration part of code according to your need. Now access this folder with browser e.g. http://localhost/image_upload. That’s it the very simple Image Resize And Upload script using php.