Article

Image Manipulation with PHP - The GD Libraries

Page: 1 2 3 Next

Syntax Explanations

Let's now step through the script, and explore what happens at each point.

  • Session - is simply there for security. You could do session_start(); $allow_thumbs = "yes"; session_register("allow_thumbs"); in your admin page to activate the thumbnailing.

    Alternatively, you could remove the session parts and put the thumbnailing script in a folder with a safeguarding .htaccess file. Either way, just make sure people cannot access the script by typing in the URL.

  • header() - to output to a page or file, a header type must be sent. We chose jpeg due to its compression ability.

  • $palette_image - the path (absolute from root, or relative to this script) and name of the square blank image that is to be used as a thumbnail base. I emphasized "square" because you must use a square image with the above script. To create a nice truecolour image, simply:

  • open your graphics program,

  • make a new square (have I mentioned that it should be square?) image,

  • increase colour depth to maximum (if it isn't already), and

  • save it as a .jpg.

    You might also like to fill it with the same colour as the background of your page before you save. Lastly, upload it and change the value of $palette_image to reference this new file.

  • default values - to avoid always having to pass every variable=value pair through the URL call, we just set some default values for use if the variable isn't set. For instance, if we called an image with img src="thumb.php?img_ref=blat.png", the script would automatically set to create=no, basepath='' and compress=100.

  • establish draw area - using GetImageSize(), we find the height and width of both our resource image, and thumbnail base. We check which is bigger on the resource image (whether it is portrait or landscape) and reduce that to mimic the space available on the thumbnail base. Some reasonably easy mathematics can then be used to deduce the other dimension and the top left pixel of our draw area. We have...

  • $to_h - height of the area to draw to

  • $to_w - width of the area to draw to

  • $to_x - horizontal position of first pixel, counted from the left

  • $to_y - vertical position of first pixel, counted from the top

  • create some images - for GD to do any image manipulations it needs to create a copy of the designated image and work from that. We need it to create two images, the thumbnail base (which we called $thumb), and a copy of the resource image (which we called $from). Our earlier use of GetImageSize on the resource image also yielded an index that holds the filetype. A quick bit of value testing will reveal that we can create our copy of that image -- be it a .gif, .png or .jpg -- which means our script is a bit more versatile now.

  • // $set_bg_colour - if you didn't fill your thumbnail base image with the same colour as your page background, you should un-comment this line and the $fill_bg_colour line. You should insert rgb values into the ImageColorAllocate($thumb, red, green, blue) call to mimic your page colour. You could even pass these in as variables in the calling URL.

  • ImageCopyResized - using all the mathematically deduced values, we use this function to take the entire area of our resource image, shrink it and copy it to the rigidly defined area of our thumbnail base.

  • /* image manipulation scripting */ - if we wanted to perform any further manipulations on our thumbnail, we would do so in this area. Further along in this tutorial are a couple of code snippets that you could plug in here.

  • ImageJPEG - this is the call that outputs an image to the browser.

  • if (create = "yes") - to save a copy of the thumbnail we've created onto the server, we need to use the middle parameter of the ImageJPEG function. We test if our variable create has explicitly been set to "yes" and, if so, we rip apart the resource image name to remove the extension and create a file based upon [that image name]_thumb.jpg.

    Note that you will need the directory that you save your thumbnails into to be chmodded to pretty high permissions. You might also want to put the thumbnails in their own seperate directory, in which case you'll want to amend the path - eg. to $HTTP_GET_VARS['basepath']. 'thumbnail_folder/' .substr($....

  • ImageDestroy - because GD had to create images to work from, we end all manipulation scripts by destroying those temporary images from the server.

    I've tested this script using GD1.6.2 and GD1.8.4, which both performed admirably -- certainly an improvement on the 256 colours allowed in the use of ImageCreate().

    Now we've got the basics, let's take a look at how we can use further image manipulation functions...

    If you liked this article, share the love:
    Print-Friendly Version Suggest an Article
  • Sponsored Links