I have been trawling around the internet looking for some simple functions for do basic automated manipulation to some images im using.
if you want to skip straight to the examples just to prove im not bulls**ting skip to the bottom
Basically i wanted to have these functions:
1. thumbnail
2. resize (actual image)
3. reduce (image file size)
4. rotate (by degrees)
Most of the scripts i found couldnt be changed easily to make them do what i wanted…
So here are what i got working.. and a few instructions…
Create a thumbnail
function createThumbnail($img, $imgPath, $suffix, $newWidth, $newHeight, $quality) { // Open the original image. $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original"); list($width, $height, $type, $attr) = getimagesize("$imgPath/$img"); // Resample the image. $tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image"); imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy"); // Create the new file name. $newNameE = explode(".", $img); $newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .''; // Save the image. imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image"); // Clean up. imagedestroy($original); imagedestroy($tempImg); return true; } ?>
Rotate
function rotateImage($img, $imgPath, $suffix, $degrees, $quality, $save) { // Open the original image. $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original"); list($width, $height, $type, $attr) = getimagesize("$imgPath/$img"); // Resample the image. $tempImg = imagecreatetruecolor($width, $height) or die("Cant create temp image"); imagecopyresized($tempImg, $original, 0, 0, 0, 0, $width, $height, $width, $height) or die("Cant resize copy"); // Rotate the image. $rotate = imagerotate($original, $degrees, 0); // Save. if($save) { // Create the new file name. $newNameE = explode(".", $img); $newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .''; // Save the image. imagejpeg($rotate, "$imgPath/$newName", $quality) or die("Cant save image"); } // Clean up. imagedestroy($original); imagedestroy($tempImg); return true; } ?>
Resize
function resizeImage($img, $imgPath, $suffix, $by, $quality) { // Open the original image. $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)"); list($width, $height, $type, $attr) = getimagesize("$imgPath/$img"); // Determine new width and height. $newWidth = ($width/$by); $newHeight = ($height/$by); // Resample the image. $tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image"); imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy"); // Create the new file name. $newNameE = explode(".", $img); $newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .''; // Save the image. imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image"); // Clean up. imagedestroy($original); imagedestroy($tempImg); return true; } ?>
Reduce
function reduceImage($img, $imgPath, $suffix, $quality) { // Open the original image. $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)"); list($width, $height, $type, $attr) = getimagesize("$imgPath/$img"); // Resample the image. $tempImg = imagecreatetruecolor($width, $height) or die("Cant create temp image"); imagecopyresized($tempImg, $original, 0, 0, 0, 0, $width, $height, $width, $height) or die("Cant resize copy"); // Create the new file name. $newNameE = explode(".", $img); $newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .''; // Save the image. imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image"); // Clean up. imagedestroy($original); imagedestroy($tempImg); return true; } ?>
The simple instructions I used for the examples
$thumb = createThumbnail($img, $imgPath, "-thumb", 120, 100, 100); $rotate = rotateImage($img, $imgPath, "-rotated", 270, 100, 1); $resize = resizeImage($img, $imgPath, "-resized", 2, 100); $reduce = reduceImage($img, $imgPath, "-reduced", 70); ?>
Those are the function calls i used to create the images linked to below.
createThumbnail variables
image file
image file path
suffix to append to the name before the .jpg
thumbnail width
thumbnail height
thumbnail quality
rotateImage variables
image file
image file path
suffix to append to the name before the .jpg
degrees to rotate (anticlockwise i think) by
image quality
save or not (if you wanted to output to a temp to preview, i dont use it, so leave as 1
resize variables
image file
image file path
suffix to append to the name before the .jpg
resize by (i used 2 which i thought was half.. not so hot on the maths)
image quality
reduce variables
image file
image file path
suffix to append to the name before the .jpg
quality (100 best, 0 worst… Duhh).
Here are the links to my examples
Original File (904k (1840×1232))
Thumbnail (16k (120×100))
Resized and Reduced (24k (368×246))

All these features are so handy for basic image manipulation. I have recently created a detailed image manipulation class, although, ironically, some of the fundamentals are missing.
Regardless, you should check it out as it may be of some use to you. It is still currently being developed, but still quite functional.
You can find it on my blog at http://tildeescape.com/2010/11/20/canvas-class/
This is incredibly helpful. Thank you! I had no idea PHP was so powerful when processing images.
Very usefull functions thank you very much!
Really great work, Jamie. Thanks very much for creating and sharing these terrific functions. (I also enjoyed the humor in your commentary of them!)
I have to really thank you for this. What a wonderful thing PHP can do. You explained it so well. Thanks!
I have to said you have a extremely good work here.
Greatly appreaciated your sample. Thanks!!!!
tnx ,This is very helpful,
I had no idea PHP was so powerful when processing images.!!!
Really good example, It will be fine if you show how to assign to src attribute of img.
awesome.. thanx for sharing
Thanks for this post… EXACTLY what I was looking for. Thanks for sharing!!
Thanks for sharing
Thanks a lot. This worked perfectly.