Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

99 users online



[Code Snippets] Image Manipulation - with actual examples

[Code Snippets] Image Manipulation - with actual examples

Currently viewing this thread: 1 (0 members and 1 guests)


Page 1 out of 2
jamiem

jamiem

Flying by the Seat of my Pants.
Status: Offline!

[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$original0000$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$original0000$width$height$width$height) or die("Cant resize copy");
    
    
// Rotate the image.
    
$rotate imagerotate($original$degrees0);
    
    
// 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$original0000$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$original0000$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"120100100);
$rotate rotateImage($img$imgPath"-rotated"2701001);
$resize resizeImage($img$imgPath"-resized"2100);
$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)

Heri

Heri

Poseidon
Status: Offline!

*Waits for Phil to post a message saying "or you could do it this way..."*

- haha. Anyways, thanks for the functions.

Locke

Locke

"Refocusing" My Time
Status: Offline!

Whoa, awesome work Jamie, thanks!

___________________

The Audoptic Weblog. It's where the magic happens.

Last edited by Locke, September 22nd, 2005 11:42 PM (Edited 1 times)

JeffreyT

JeffreyT

Object-Oriented 4 LiFe
Status: Offline!

It's ok... I wouldn't say awesome though :p. Go ahead, Phil... show em how it's done ;).

___________________

There is a limit to stupidity, and people keep crossing it.

Heri

Heri

Poseidon
Status: Offline!

Phil, yer disappointing me. *sad* (j/k)

Xplozive

Xplozive

Emo Fringe
Status: Offline!

Thumbnails could come in a lot of use, thanks :D

___________________

Xplozive

jamiem

jamiem

Flying by the Seat of my Pants.
Status: Offline!

Thanks guys!

I forgot to add that these are SIMPLE functions... for BASIC manipulation!

and...

make sure that the directory you are writing to is CHMOD 0777

Jamie :)

___________________

J

Last edited by jamiem, September 23rd, 2005 07:20 AM (Edited 1 times)

BigToach

BigToach

Neversidian
Status: Offline!

Here are some more as well.
http://www.bigtoach.com/cat/image/

___________________

Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

Phil

Phil

with Mr. Jones
Status: Offline!

If you stick with gd lib, its not like there are many alternate ways of doing it. Sure, you could OOPize this with some tests, but... you'd be using the same functions and such.

You can shell to imagemick

___________________

http://www.philbrodeur.com - Expert PHP Development and Tutorials

Locke

Locke

"Refocusing" My Time
Status: Offline!

Just to make a few things clear, ImageMagik is a retarded library. It still uses GD anyways and uses more 'resources' and space than GD does for thumb generation.

With the proper tweaking and tuning GD2 outperforms IG in size and quality.

Stick with GD2, stop using IG.

___________________

The Audoptic Weblog. It's where the magic happens.

Page 1 out of 2
Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0121 seconds.