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;
}
 
?&gt;

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;
}
 
?&gt;

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);
?&gt;

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))