Do it simpler : Image thumbnail and resize in PHP
One of the common complex task among webdevelopers is image resizing. It is a big headach when using multiple size images in multiple pages of a single version of image. For long I was using a an script that would create one thumnail version of the image when the image is uploaded. It was not a suitable solution. Many time we will need different size of thumbnails to be used in different location and pages.
I came across PHPThumb. It was a very much better one for me where it can deliver following types of resizing mechanism.
- adaptiveResize ($width, $height)
- crop ($startX, $startY, $cropWidth, $cropHeight)
- cropFromCenter ($cropWidth, $cropHeight = null)
- resize ($maxWidth, $maxHeight)
- resizePercent ($percent)
- rotateImage ($direction = ‘CW’)
- rotateImageNDegrees ($degrees)
- save ($fileName)
- show ()
You can learn more about this script here
Well,I have written a simple script that can work with phpThumb. What it would do is, when I request for a resized version of an original image, it will check for a same resized version. If the one resized version exist, it will return the path of that. Otherwise, a new
resized version will be created and saved before returning the path. This will help to use any size of thumbnail image at any time without wasting match resources.
The script
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | <?php require_once("phpthumb/ThumbLib.inc.php"); /** * Resize the image according to the given parameter * * This funciton resize the image to the given criteria and save it * in a well-named subdirectory. Then the path to the image will be returnd. * I the image exists allready with give criteria, the image path is * returned simply. * * @param string $image path to the source image * @param integer $width required width * @param integer $height required height * @param string $resizeStyle style of resizing (ADAPTIVE , CROP, * CROPCENTER, RESIZEPERCENT, RESIZE) * @param integer $percent resize percentage * @param integer $starX start pixel X axis * @param integer $starY start pixel Y axis * @param integer $quality picture quality * @return string path to the reqested resized image * * @author muneer <muneer@live.it> * @version 1.0 * @copyright (c) 2009 encodez solutions http://www.encodez.com */ function getResized($image, $width, $height, $resizeStyle = "ADAPTIVE", $percent = 100, $starX = 0, $starY = 0, $quality = 80) { /* setting file name */ $fileName = basename($image); /* creating the path for the resized image */ $path = substr($image, 0, -(strlen($fileName))); if($resizeStyle != "RESIZEPERCENT") { $path .= $width . "x" . $height . "-" . $resizeStyle; } else { $path .= $percent . "-" . $resizeStyle; } /* * checking whether image exists with the same criteria * If exist return the generated path, * If not, create the thumnail */ if(file_exists($path . "/" . $fileName) && filemtime($path . "/" . $fileName) > filemtime($image)) { return $path . "/" . $fileName; } else if(file_exists($image)) { /* making directory for the image if it is not exists */ if(!file_exists($path)) mkdir($path, 0777); $options = array('jpegQuality' => $quality); try { /* creating the PhpThumbFactory object */ $thumb = PhpThumbFactory::create($image, $options); } catch(Exception $ex) { echo "\n<!-- There was an error resizing the image \nSource Image : " . $image . " \nError : " . $ex->getMessage() . " -->"; } switch($resizeStyle) { case "ADAPTIVE": // adaptiveResize $thumb->adaptiveResize($width, $height)->save($path . "/" . $fileName); break; case "CROP": // crop $thumb->crop($startX, $startY, $width, $height)->save($path . "/" . $fileName); break; case "CROPCENTER": // cropFromCenter $thumb->cropFromCenter($width, $height = null)->save($path . "/" . $fileName); break; case "RESIZEPERCENT": // resizePercent $thumb->resizePercent($percent)->save($path . "/" . $fileName); break; case "RESIZE": // resize $thumb->resize($width, $height)->save($path . "/" . $fileName); break; default: // resize $thumb->resize($width, $height)->save($path . "/" . $fileName); } return $path . "/" . $fileName; } else { /* return 0 if the src image is not exists */ return 0; } } // end function getResized() |
How to use this function?
scenario : Need a adaptive resize verion for /img/catalogue/sample1.jpg at 120px x 100px
solution :
1 | <img src="<?php echo getResized("/img/catalogue/sample1.jpg", 120, 100, "ADAPTIVE") ?>" /> |
This will generate a “adaptive” resized image at the size of 120 px width and 100 px height.
This iamge will have the path
/img/catalogue/120×100-ADAPTIVE/sample1.jpg
This script might helpful you too. Please do not forget to put a comment if you like it Or any suggestion if you have.

