Creating Dynamic Image Thumbnails Using PHP
Creating Dynamic Image Thumbnails Using PHP
(http://stats.buysellads.com/click.go?z=1243230&b=2784417&g=&s=&sw=1366&sh=768&br=firefox,16,win&
r=0.2566768659670199&link=http://www.wix.com/eteamhtml/400?utm_campaign=ma_onextrapixel.com&
experiment_id=ma_onextrapixel.com_728genericsearch_400nn)
Think how many websites that are built and need an image, and a thumbnail version of that image. When building
boxofficeBUZ (http://www.boxofficebuz.com/) I came across a very simple but normal issue. Why waste time creating an
image and thumbnail version in Photoshop or any other image processing software, why not do it dynamically with
php.
From there I started researching php's built in GD image library, and ways to create thumbnails. There is a lot of
information on creating thumbnails, most either create a thumbnail by just giving a height or width. If you can specify a
height and width the thumbnails usually result in being stretched. It is from here that I started to combine scripts and
eventually came up with a standard thumbnail script that I use for most of my websites. This not only creates a
thumbnail, but crops the image keeping its dimensions intact and results in not stretching the thumbnail image.
The Code!
In this tutorial, we will be using php to create on the fly dynamic thumbnails of images. This tutorial will show you how
to do it for all major image formats ie. png, jpg, gif.
Step 1 is telling the script what size & width you would like the thumbnail images to be.
Now we want to tell the script the source file, and the destination of the thumbnail.
The next part of the code will dynamically figure out the extension of the file. We use php's explode function to find the
period before the file extension, and then use count to find everything after the period, resulting in the file extension.
1 $size = getimagesize($source);
2 $w = $size[0]; //Images width
3 $h = $size[1]; //Images height
The next part of the code is switch statement to make sure we use the right php function to create the thumbnail
image.
1 switch($stype) {
2 case 'gif':
3 $simg = imagecreatefromgif($source);
4 break;
5 case 'jpg':
6 $simg = imagecreatefromjpeg($source);
7 break;
8 case 'png':
9 $simg = imagecreatefrompng($source);
10 break;
11 }
The next part of the code is what does all the magic, it will create the thumbnail, and move it into the desired folder.
The last line of the next block of code goes in this order (destination image, destination, quality of image).
14 }
15
16 imagejpeg($dimg,$dest,100);
Varaition of code
This is just an added bonus variation of the code, it will take a desired folder, find every image in that folder, and
create thumbnails of all the images on the fly.
59 $wm = $w/$nw;
60 $hm = $h/$nh;
61 $h_height = $nh/2;
62 $w_height = $nw/2;
63
64 if($w> $h) {
65 $adjusted_width = $w / $hm;
66 $half_width = $adjusted_width / 2;
67 $int_width = $half_width - $w_height;
68 imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
69 } elseif(($w <$h) || ($w == $h)) {
70 $adjusted_height = $h / $wm;
71 $half_height = $adjusted_height / 2;
72 $int_height = $half_height - $h_height;
73
74 imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
75 } else {
76 imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
77 }
78 imagejpeg($dimg,$dest,100);
79 }
80 }
81
82 /* Closing the directory */
83 @closedir($dir_handle);
84
85 }
Conclusion
Why waste your time cutting and cropping images with image processing software, when you can just use a PHP built
in function to create image thumbnails on the fly. It will give you more free time in the end, and once mastered there
are many more things you can do with PHP's image functions.
Previous (http://www.onextrapixel.com/2011/02/24/the-art-of-career-networking-turn-your-snakes-into-ladders/)
Random (http://www.onextrapixel.com/random/)
Next (http://www.onextrapixel.com/2011/02/26/avoid-fluff-while-doing-your-web-design/)
Author
(http://www.ronnicholls.com)
Ronald Nicholls is a freelance web developer, and Owner/CEO of Geek Suite, Inc. A graduate of Multimedia Design at
durham college. While at Durham he spent his last 2 years of school freelancing, and learning more about the industry.
In July 2010 He started Geek Suite Inc. a network of websites that cover a variety of topics and interests, with the
basic theme being, "Nerd / Geek" culture.
great tutorial on image crop system using php, we could even do it using lots of available php
classes.
2. Josh (http://joshuawilcox.net)
February 25, 2011 at 9:33 pm
Great article! Thanks for sharing. I just had to do this for a client.
3. Darek
February 25, 2011 at 9:40 pm
The script does produce one or two thumbnails correctly and places them in the
correct dir, but then it stops and throws this error:
"HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was
attempting to fulfill the request."
Ronald (http://www.boxofficebuz.com)
February 26, 2011 at 12:56 am
First thing I would check if your trying to create a lot of images is, max execution time.
second check to make sure your images are all in the .jpg, .png, or .gif format. If that
doesn't work, shoot me an email threw my portfolio website with the issue and ill do my best to help you
out. http://www.ronnicholls.com (http://www.ronnicholls.com)
4. Josh F
February 25, 2011 at 10:33 pm
This is awesome! I've been looking for a way to dynamically create thumbnails on the fly without
resizing manually in Photoshop. Thanks!
5. Ijaas (http://dex-labs.com)
February 26, 2011 at 3:23 am
Ronald (http://www.boxofficebuz.com)
February 26, 2011 at 4:07 am
That is actually really nice code to use, I tend to build most of my applications from
scratch, so over the years this is just a nice little script that I came up with, And it was
more developed for things like, Multiple upload scripts. I some times have to upload 100's of images at
once, and this script mostly came from that.
6. Daniel S (http://www.shiftedwork.de/blog/)
February 26, 2011 at 5:01 am
Its better to use the imagick functions. They need less memory and produce better results.
Ronald (http://www.boxofficebuz.com)
February 26, 2011 at 6:15 am
I agree with Ijaas - timthumb and phpthumb are alternatives with good support and applications
9. towry (http://towry.me)
August 25, 2011 at 3:18 pm
10. gandalf117
September 29, 2011 at 5:52 am
Could you just explain how the height or the width gets adjusted?
Why do you need the third else statement when you cover all the conditions in the first two?
Ronald (http://boxofficeBUZ.com)
September 29, 2011 at 5:54 am
The last if statement is more of a "catch all" just in case by some weird reason it doesn't
fall into the first two if statements.
Great script, I was looking this to use for my new project and wondering that ever thing is
working well
Thanks
12. Rong
February 13, 2012 at 2:34 pm
Awesome script and it's helped me quickly create thumbs but I did have one problem.
The script throws off an error and creates a black thumbnail if the image is JPG, Jpg, jPG or any
variation like that. I corrected this by turning the extension into all lowercase letters by changing:
switch($stype) {
to
switch(strtolower($stype)) {
13. Brandon
April 26, 2012 at 12:16 am
I'm trying to figure out how to use this scipt.. I'm a newb. Is this an "in directory" script
14. juvl
July 4, 2012 at 10:13 pm
Thanks a lot!
Please note that comments are moderated - and rel="nofollow" is in use. Please no link dropping, no keywords or
domains as names. Kindly do not spam, and do not advertise!