In today’s visually-driven online world, the importance of images cannot be overstated. They engage users, convey messages, and make websites and applications visually appealing. However, using large images directly on your web pages can lead to slower load times, higher bandwidth consumption, and a less-than-ideal user experience.

This is where thumbnail images come into play. Thumbnails are smaller, more manageable versions of larger images, designed to provide a quick preview without the drawbacks of larger file sizes. They are widely used in galleries, product listings, and content management systems to ensure efficient web performance and a seamless user experience.

Creating thumbnail images on the fly is a common requirement for web developers and designers. Fortunately, PHP, a versatile and popular server-side scripting language, provides powerful tools for automating this task. In this article, we will delve into the world of thumbnail image creation using PHP code.

Whether you’re building a photo-sharing platform, an e-commerce website, or a personal blog, understanding how to generate thumbnail images dynamically can significantly enhance your web application’s efficiency and overall aesthetics. So, let’s embark on this journey to learn how PHP code can simplify the process of creating and optimizing thumbnail images for your web projects.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function make_thumb($img_name,$filename,$new_w,$new_h)
{
//get image extension.
$ext=getExtension($img_name);
//creates the new image using the appropriate function from gd library
if(!strcmp("jpg",$ext) || !strcmp("JPG",$ext) || !strcmp("jpeg",$ext) || !strcmp("JPEG",$ext))
$src_img=imagecreatefromjpeg($img_name);

if(!strcmp("gif",$ext) || !strcmp("GIF",$ext))
$src_img=imagecreatefromgif($img_name);

if(!strcmp("png",$ext) || !strcmp("PNG",$ext))
$src_img=imagecreatefrompng($img_name);

//gets the dimmensions of the image
$old_x=imagesx($src_img);
$old_y=imagesy($src_img);

$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;



if($ratio1>$ratio2) {
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}
else {
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
// we create a new image with the new dimmensions
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

// resize the big image to the new created one
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

// output the created image to the file. Now we will have the thumbnail into the file named by $filename
if(!strcmp("png",$ext))
imagepng($dst_img,$filename);
if(!strcmp("gif",$ext))
imagegif($dst_img,$filename);
else
imagejpeg($dst_img,$filename);

//destroys source and destination images.
imagedestroy($dst_img);
imagedestroy($src_img);
}

?>

To find out the extension of image with following function:

1
2
3
4
5
6
7
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

Call the particular thumbnail function like :

1
2
make_thumb('original.jpg','thumb.jpg',150,250);
echo "Thumbnail created successfully...!!!";

3 Comments

GP Code library · April 12, 2013 at 3:59 pm

GP Code library
Create thumbnail image by php
When we upload large size images on server. Uploaded large images take more time to load on webpage, so we need to show small size images on our webpage. Image thumbnail is a solution to generate uploaded image’s thumbnail to show required size images on our website.
http://codelibrary.googleplus.co.in/create-thumbnail-image-by-php/

iPad Demonstration / Review Thing Part 1 | Apple On The Longtail · June 3, 2010 at 6:58 pm

[…] Thumbnail image creation using php code « ANIL KUMAR PANIGRAHI 's Blog […]

Case Study : Upload and crop images with specific dimensions - Anil Labs · August 31, 2015 at 7:03 am

[…] Thumbnail image creation using PHP code […]

Leave a Reply

Avatar placeholder

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