Who Needs Photoshop? PHP GD Images and Your Online Store

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

Many web masters will be familiar with Photoshop by Adobe. This highly featured photo editing and graphics application is incredibly powerful, but also incredibly expensive. If you find that the main thing you do with photoshop is basic product image cropping, resizing, trimming, rotating and watermarking then there is a better way!

If your server uses PHP and has GD functionality enabled, you can use the power of the GD library to automatically do all of these things for you. Furthermore the GD library is faster than using photoshop and as the process actually runs on your server, you do not have to worry about uploading files. Your employees can directly manipulate product photos from your web store administration screens.

Chances are the main thing you will need to do is to take one hi res source image and create one or more resized versions of it to be displayed as thumbnails or product info page images. Your Hi Res images can then be displayed as a pop up or lightbox.

There are many PHP GD thumbnail classes etc out there and you will easily find one by searching the major search engines.

Here are the basic concepts though:

1. Grab an image into memory from an external jpeg file Before we can do anything we have to get an image into the memory. This then gives us something to work with.

//grab image into memory,  $img = full url to jpeg image
if(!$jpeg = imagecreatefromjpeg($img)){
echo '
<h2 style="color: red">Failed to Grab Image ' . $img . '</h2>
';
exit;
}

2. Manipulate Image however you want There are all kinds of things you can do with the image you have loaded using the GD library.

3. Display the Image or Save to Disk Once you have manipulated the image and have it looking the way you want, you can either serve it up or save it to the disk. Of course you can also do both if you prefer.

//save image
$save_path = 'Your Save Path';
imagejpeg($jpeg,$save_path);

//display image
header ("Content-Type: image/jpeg");
imagejpeg($jpeg);

//destroy image to free memory
imagedestroy($jpeg);

Related Resources http://uk3.php.net/gd http://www.phpclasses.org/browse/package/1365.html http://www.phpit.net/article/image-manipulation-php-gd-part1/