Menu

Create Thumbnail Image Dynamically Using PHP

PHP By May 28, 2014 No Comments


Create Thumbnail PHP Script

Create Thumbnail Image Dynamically Using PHP

Hello friends earlier we have created the CAPTCHA code example using PHP today here i am putting the tutorial for Create Thumbnail Image Dynamically Using PHP. creating Thumbnail using PHP is very easy and useful in your many projects or applications. You dont have to worry that how you will create thumbnails for gallery just use this code and start working. In this tutorial we are creating the thumbnail after saving the image in the folder than by getting the URL we create the thumbnail of that image. Here we use the GD Library of PHP lets get start.

Step 1. Get Image

As i earlier said that we create the thumbnail of existing image which is saved in our project. Suppose we have an Image folder and in this folder we have an image named “t.gif”. Original Image is of 500px * 250px in size. We will set our parameters so that we will set the thumbnail size.

$rand=rand("100", "500");
$src="images/t.gif";
$dest="thumb/$rand.gif";
$desired_width="200px";
$desired_height="50px";

Let me explain the above code

$rand is used for creating the thumbnail name.

$src is used for getting the URL of the original image.

$dest is used for saving the thumbnail to our new location.

$desired_width & $desired_height is used for setting the thumbnail height.

If you want the thumbnail in proportion than dont set the fixed size just set either the width or the height.

Now we will create our function for creating the thumbnail.

Create Create Thumbnail Image Dynamically Code


function create_thumb($src, $dest, $desired_width,$desired_height) {
/* read the original image */
$source_image = imagecreatefromgif($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the “desired height” of this thumbnail, relative to the desired width */
// $desired_height = floor($height * ($desired_width / $width));
/* we will create a temporary image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a re-sized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0,
$desired_width, $desired_height, $width, $height);
/* create and save the thumbnail image to its destination */
imagegif($virtual_image, $dest);
}

If you need the relative height according to the image width than uncomment the below line.

// $desired_height = floor($height * ($desired_width / $width));

and remove the desired_height parameter from function.

In above example we use the “GIF” Image if you are using the other image type than please change the functions according to the image type.

Call our Create Thumbnail Function

Now everything is setup we have to just call the function and use the function.

create_thumb($src, $dest, $desired_width,$desired_height);

Hope you like the post about how to Create Thumbnail Image Dynamically Using PHP. If you have nay suggestions or any feedback or any question than please comment and let me know. Thank you.

~~~~ Happy Codeing ~~~~

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

No Comments

Leave a comment

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