How to resize or make thumbnail images in PHP

Published On: 12 December 2011.By .
  • Digital Engineering

When we display images on our web pages we use <img src=”path” height=”” width=””> tag but this is a hardcode method to display the images. Now suppose the uploaded image is of larger size then the image loading will takes the time and the image quality will not be same as original that’s why we have to restrict it. Similarly if we want to restrict the extension of file.

 

We can do all this process at the time of uploading here is the code:-

 

<?php

if(isset($_POST[‘submit’]))

{

$path_thumbs = “/path/to/thumbs/”; //make sure this directory is writable!

$img_thumb_width = 100; //the new width of the resized image, in pixels.

$extlimit = “yes”; //Limit allowed extensions? (No for all extensions allowed)

$limitedext = array(“.gif”,”.jpg”,”.png”,”.jpeg”,”.bmp”); //List of allowed extensions if extlimit = yes

$file_type = $_FILES[‘vImage’][‘type’]; //the image -> variables

$file_name = $_FILES[‘vImage’][‘name’];

$file_size = $_FILES[‘vImage’][‘size’];

$file_tmp = $_FILES[‘vImage’][‘tmp_name’];

if(!is_uploaded_file($file_tmp)){ //check if you have selected a file.

echo “Error: Please select a file to upload!. <br>–<a

href=”$_SERVER[PHP_SELF]”>back</a>”;

exit(); //exit the script and don’t process the rest of it!

}

$ext = strrchr($file_name,’.’); //check the file’s extension

$ext = strtolower($ext);

if (($extlimit == “yes”) && (!in_array($ext,$limitedext))) { //uh-oh! the file extension is not allowed!

echo “Wrong file extension. <br>–<a href=”$_SERVER[PHP_SELF]”>back</a>”;

exit();

}

$getExt = explode (‘.’, $file_name); //so, whats the file’s extension?

$file_ext = $getExt[count($getExt)-1];

$rand_name = md5(time()); //create a random file name

$rand_name= rand(0,999999999);

//the new width variable

$ThumbWidth = $img_thumb_width;

/////////////////////////////////

// CREATE THE THUMBNAIL //

////////////////////////////////

if($file_size){ //keep image type

if($file_type == “image/pjpeg” || $file_type == “image/jpeg”){

$new_img = imagecreatefromjpeg($file_tmp);

}elseif($file_type == “image/x-png” || $file_type == “image/png”){

$new_img = imagecreatefrompng($file_tmp);

}elseif($file_type == “image/gif”){

$new_img = imagecreatefromgif($file_tmp);

}

list($width, $height) = getimagesize($file_tmp); //list the width and height and keep the height ratio.

$imgratio=$width/$height; //calculate the image ratio

if ($imgratio>1){

$newwidth = $ThumbWidth;

$newheight = $ThumbWidth/$imgratio;

}else{

$newheight = $ThumbWidth;

$newwidth = $ThumbWidth*$imgratio;

}

if (function_exists(imagecreatetruecolor)){ //function for resize image.

$resized_img = imagecreatetruecolor($newwidth,$newheight);

}else{

die(“Error: Please make sure you have GD library ver 2+”);

}

imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, //the resizing is going on here!

$newheight, $width, $height);

ImageJpeg ($resized_img,”$path_thumbs/$rand_name.$file_ext”); //finally, save the image

ImageDestroy ($resized_img);

ImageDestroy ($new_img);

}

move_uploaded_file ($file_tmp, “$path_big/$rand_name.$file_ext”); //ok copy the finished file to the thumbnail directory

/* UNCOMMENT THIS IF YOU WANT TO CREATE A NEW FOLDER*/

//echo “IMG:<img src=”$path_big/$rand_name.$file_ext” />”;

//exit();

//*/

//and you should be set!

$msg = urlencode(“$title was uploaded! <a href=”Resize.php”>Upload //success message, redirect to main page.

More?</a>”);

header(“Location: Resize.php?msg=$msg”);

exit();

}else{

if(isset($_GET[‘msg’])) //if there is a message, display it

{

echo “<p>”.urldecode($_GET[‘msg’]).”</p>”; //but decode it first!

}

echo ” //the upload form

<form action=”$_SERVER[PHP_SELF]”

method=”post”enctype=”multipart/form-data”>n

<p>File:<input type=”file” name=”vImage” /></p>n

<p><input type=”submit” name=”submit” value=”Submit” /></p>”;

}

?>

Related content

That’s all for this blog