[Code Snippets] Image Manipulation - with actual examples
Hi All.
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
PHP:<?php
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
PHP:<?php
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
PHP:<?php
function resizeImage($img, $imgPath, $suffix, $by, $quality)
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<i>$imgPath/$img</i>)");
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
PHP:<?php
function reduceImage($img, $imgPath, $suffix, $quality)
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<i>$imgPath/$img</i>)");
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;
}
?>
Here are a few instructions.. nothing too hard!
PHP:<?php
$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 links to my examples
http://files.blue44.com/resampledImages/1-1.jpg - Original File (595k (1600x1200))
http://files.blue44.com/resampledImages/1-1-thumb.jpg - Thumbnail (17k (120x100))
http://files.blue44.com/resampledImages/1-1-resized.jpg - Resized (374k (800x600))
http://files.blue44.com/resampledImages/1-1-reduced.jpg - Reduced Quality (171k (1600x1200))
http://files.blue44.com/resampledImages/1-1-rotated.jpg - Rotated (and resized (374k (800x600)))
Hope this will help someone!
Jamie :)
___________________
J
Last edited by jamiem, September 22nd, 2005 10:38 PM (Edited 1 times)
