GD2+ Thumbnail Class Distro


This thumbnailing class works using the GD libraries from boutell.com and is specifically for versions 2 and upwards. The range of manipulations include Bevel, Motion Blur, Merge, GreyScale, Frame, Round Edges, Drop Shadow and Ellipse which can be stacked together to provide almost infinite permutations.

Though it has comments in the download file specifying how to call each manipulation, there are additional notes below the download link.

Download the GD2+ Thumbnail Class Now
downloaded 9785 times from this site.


This class is freely available for use in any non-profit applications, though a note to say you are using it would be appreciated. If you require it for a more commercial project, please contact me.

Changelog:

24th July '03 - added frame_colour variable to the frame() function. Calls using the 3 parameter syntax will still work fine and result in the frame being assigned a colour half way between the light and dark values. Calls using the new fourth parameter will have the frame coloured that colour.

GD2+ Thumbnail Class Usage Notes

Warning Version 2.0.12 (and possibly 2.0.10 - 2.0.14) of the GD libraries (or maybe just PHP4.3.2) has major snags with this class file and produces inadequate results. I am endeavouring to find work-around methods for fixing the problems encountered.

The problem seems to be in using numbers below 100 for the opacity values of the ImageCopyMerge function - they are all being returned as black for some reason.
I could (probably) write a php user function to emulate the same effect though am pretty sure it would be quite a resource hog so am delaying that tactic until I've had more time to tinker.

This glitch is fixed in PHP4.3.3rc3 which uses GD2.0.15 - builds greater than that should not have problems.

If you are not overly familiar with object orientated programming, you will do well by reading the few notes below.
The class has been designed to save a manipulated version of a named image onto the server and is not meant to be used for on-the-fly image generation. Upon calling the class for a particular image, an object reference of that image is created and any further manipulation of that object only effects that particular instance. Thus, it will work perfectly well if used within a directory scan iterator that seeks to create thumbnails of every image encountered. It will not work effectively if called through pointing the src of an <img tag at it, due to not outputting to browser and only saving locally.

The image to the left was created using the class with a few calls stacked in sequence, namely Greyscale, then Ellipse, then Bevel and finally RoundEdges. In order to give an impression of usable calling syntax, I will go through the coding used in the creation of that image.
You may like to copy/paste all the text below and save it as a helpfile until you become conversant in using the class.

<?php
include_once('folder/thumbnail_create.php');
$variable = new Thumbnail('images/resource_name.jpg',120,120,'thumbs/output_name.jpg',85,'
    "greyscale()",
    "ellipse(\'C3DDEE\')",
    "bevel(7,\'FFFFFF\',\'111144\')",
    "round_edges(4,\'C3DDEE\',1)"'
);
?>

Firstly the class file is included into the processing script, then an object 'Thumbnail' is created with the reference $variable.
$variable = new Thumbnail(.....)
For every image you process concurrently, you should use a different variable name, thus instantiating a new object.
The parameters passed to the constructor are entered directly within the initial call and are, in order:

new Thumbnail(
resource image reference, [path and name of input file]
maximum width, [integer - measured in pixels]
maximum height, [integer - measured in pixels]
output image reference, [path and name of output file]
compression of jpeg output, [integer - measured in percent quality]
string array of manipulations *see below*
)

The manipulations parameter may contain any number of calls formed as array parts, as long as they are readable as an unbroken string. You could either encase the full string in single quotes (as I have done) or in double quotes, as long as you assure the string is not terminated too early.

Each manipulation can accept values to govern the output, though these are optional in every case. Leaving them blank will tend to default toward an image based on a white background with minimal changes. The functions and their allowable input are listed below.

bevel( edge width, hex light colour, hex dark colour ) // shaded bevelled edges
greyscale( int red, int green, int blue ) // basic black and white
ellipse( hex background colour ) // ellipse on bg colour
round_edges( edge_radius, background colour, anti-alias width ) // corner trimming
merge( merge image, x start [neg = from right], y start [neg = from base], opacity, transparent colour on merge image ) // overlay merge image
frame( hex light colour, hex dark colour, int width of mid bit ) // plain raised border
drop_shadow( shadow width, hex shadow colour, hex background colour ) // more like a dodgy motion blur [semi buggy]
motion_blur( int number of lines, hex background colour ) // fading parallel lines

There is also a slow way of calling the manipulations that might appeal more, though you should note that the error suppression isn't as effective this way.

<?php
$variable
= new Thumbnail('resource.jpg',200,150,'output_image.jpg',85,'');
$variable->bevel(8,'FFCCCC','330000');
$variable->merge('overlay.png',5,-35,65,'FF0000');
$variable->create();
?>


As with the fast way of calling on the class, the parameters are the same, only the last one is left as an empty string. Each manipulation can then be called seperately on the object reference prior to a create() call being sent. Certainly is a bit more readable that way.
To simply resize an image, you could call the above and just omit the manipulation calls. If you were happy with the 80% quality/compression default you could omit the last two parameters and just use (resource, width, height, output) and finish with a call to create().


SiteMap