PHP Create Image Thumbnail

In this article am going to speak about how to create image thumbnail in PHP. In lot of website and portals we need to show image thumbnail for profile image, products etc. By using PHP you can easily create image thumbnails with required dimensions.

PHP image Thumbnail

I have already posted an article about creating the image thumbnail on the fly called Create thumbnail image in PHP where we are creating the thumbnail image dynamically on the fly but where as in this article we permanently create the image thumbnail and save it in web directory.

Live Demo Download

Creating the Image Upload Form

Find the html code below to build an image upload form.

<form method="post" enctype="multipart/form-data" action="upload.php">
  <h4>PHP Image Thumbnail</h4>
    <label for="imageSource"></label>
    <input type="file" name="imageSource" id="imageSource">
    <label for="imageSource"></label>
    <input type="submit" name="uploadnCreate" id="uploadnCreate" value="Upload and Create Thumbnail">

Creating the Thumbnail Image in PHP

Save the below script as upload.php. Create an empty directory called profile_pics at the root of your project.

//Web Directory for Upload thumbnail Image
//Clean the Filename
$img = explode('.', $_FILES['krowAvatarImg']['name']);
//Thumbnail file
//Rename the thumbnail Image
$img_thumbLarge = $img_dirlarge . $krowAvatar;
//String lower case
$extension = strtolower($img[1]);
//Check the file format before upload
if(in_array($extension , array('jpg','jpeg', 'gif', 'png', 'bmp')))
	//Find the height and width of the image
	list($gotwidth, $gotheight, $gottype, $gotattr)= getimagesize($image_filePath);
	//Find the image type
	//---------- To create thumbnail of image---------------
	if($extension=="jpg" || $extension=="jpeg" ){
	$src = imagecreatefromjpeg($_FILES['krowAvatarImg']['tmp_name']);
	else if($extension=="png"){
	$src = imagecreatefrompng($_FILES['krowAvatarImg']['tmp_name']);
	$src = imagecreatefromgif($_FILES['krowAvatarImg']['tmp_name']);
	//Get the height and width of uploaded image

	// ----------------------------------------------------
	//Set new width for image

	//Set new height for image
	// $newheightLarge=160;

	// or Calculate and scale it proportanly
        // ----------------------------------------------------

	//Creating the thumbnail from true color
	//Enable image interlace property
	imageinterlace($tmp, 1);
	//Create a image with given dimension
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidthLarge,$newheightLarge, $width,$height);	
	//Put the image data to newly created Image

This kind of PHP thumbnail creation is more useful for Profile Image, Photos etc., Hope you will like this article. Don’t forget to make FREE email subscription for more updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>