PHP Image Library

A PHP library of functions for easy image manipulation with the GD function set.
Posted by Paul Whitrow, 27th May, 2009 | Permalink | 14 Comments
Whenever I'm working on a project that requires image manipulation, I always include this useful file. It's something that that's grown over time and includes functions both created by me and or found and modified on my travels through the web.
I've been able to produce pretty much everything I need with these, especially through function stacking.
Some of the functions in this library were used in creating xFlow! : A Javascript and PHP based Coverflow style gallery.
Anyway, I find it a useful thing to have, so thought I would share. I would of course welcome any suggestions for changes / improvements.
Feel free to grab it for yourself (download as a .txt file and rename as .php): Download Image Library
Remember, these are GD image functions, NOT ImageMagick!
Image Functions Library
Functions list:
- _imageGreyScale()
- _imageSepia()
- _imageTint()
- _imageRotate()
- _imageResize()
- _imageThumbPart()
- _imageRoundCorners()
- _imageWaterMark()
- _imageMergeXY()
- _imageShadow()
- _imageFlip()
- _imageReflect()
- _imageTextWrite()
- _imageFromString()
- _imageBorder()
- _imageCrop()
- _imageSkew()
- _imagePerspective()
- _imageBlur()
- _imageCopyToType()
Functions Descriptions:
Greyscale
_imageGreyScale('original.jpg', 'greyscale.jpg')
_imageGreyScale (str $source, str $target)
Converts an image ($source) to a grey scaled version.
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Sepia
_imageSepia('original.jpg', 'sepia.jpg')
_imageSepia (str $source, str $target)
Converts an image ($source) to a sepia coloured version.
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Tint
_imageTint('original.jpg', 'tint.jpg', 'FFCC00')
_imageTint (str $source, str $target, str $tint)
Converts an image ($source) to a color tinted ($tint) version.
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Rotate
_imageRotate('original.jpg', 'rotate.jpg', 45)
_imageRotate (str $source, str $target, int $degrees)
Rotates an image ($source) by the given degree ($degrees)
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Resize
_imageResize('original.jpg', 'resize.jpg', 100, 100, 'w')
_imageResize (str $source, str $target, int $w, int $h, str $aspect)
Resizes by values entered ($w, $h) and maintains the aspect of an image ($source). Aspect ($aspect) can be 'h' or 'w', for resizing by 'height' or 'width' respectively.
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Thumbnail (part)
_imageThumbPart('original.jpg', 'thumbpart.jpg', 75, 75)
_imageThumbPart (str $source, str $target, int $tw, int $th)
Creates a thumbnail image of the sizes entered ($tw, $th), from the center of the image ($source)
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.
Use function _imageResize to create a full thumbnail

Round Corners
_imageRoundCorners('original.jpg', 'corners.jpg', 20, 'ffffff')
_imageRoundCorners (str $source, str $target, int $radius, str $bgcolor)
Rounds the corners of an image ($source) by the given radius ($radius), by creating PNG images of colour ($bgcolor) and placing those corner images over the source image in the four corner positions.
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Watermark
_imageWaterMark('original.jpg', 'watermark.jpg', 'watermark.gif', 25, 10)
_imageWaterMark (str $source, str $target, str $wmFile, int $transparency, str $margin)
Places a watermark image ($wmFile) on top of the source image ($source) in the bottom right location of the source image, with a given transparency ($transparency) and a given margin ($margin)
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Image Merge Co-Ordinates
_imageMergeXY('original.jpg', 'merge.jpg', 'marker.gif', 210, 180, 100)
_imageMergeXY (str $source, str $target, str $merge, str $x, str $y, int $transparency)
Places an image ($merge) on top of the source image ($source) in the position specified ($x, $y) , with a given transparency ($transparency).
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Shadow
_imageShadow('original.jpg', 'shadow.jpg', 5, 10, 1, 'ffffff')
_imageShadow (str $source, str $target, int $offset, int $steps, str $spread, str $bgcolor)
Places a (drop) shadow to the bottom right of the source image ($source). The shadow is offset ($offset) from the image, can be spread over a desired distance ($spread), and also scaled by steps ($steps).
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Flip
_imageFlip('original.jpg', 'flip.jpg', 'h')
_imageFlip (str $source, str $target, str $mode)
Flips an image ($source) by vertical axis ($mode = 'v'), horizontal axis ($mode = 'h') or both axis ($mode = 'both')
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Reflect
_imageReflect('original.jpg', 'reflect.jpg', 'ffffff', 1, 80, 10)
_imageReflect (str $source, str $target, str $colorfade, int $fadeheight, int $fadestart)
Creates a reflection (also know as 'wet floor') effect for the source image ($source). The background colour of the fade ($colorfade) can be set to match the document background colour. The height of the reflection ($fadeheight) can be set as desired, as can the starting transparency ($fadestart) of the reflection.
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Text
_imageTextWrite('original.jpg', 'text.jpg', "Leaf", 'AUGUSTUS.TTF', 40, 'FFCC00', '484848')
_imageTextWrite (str $source, str $target, str $text, str $font, int $fontsize, str $color, str $bgcolor)
Place centralised (vertical and horizontal) text onto an image ($source). Set the text string ($text), the font (this is done by calling in any True Type font file ($font)), the font size ($fontsize), and the text color ($color). A shadow can be applied to the text by setting a dark color ($bgcolor) for the shadow text. This text will be the same as the input text, but will be placed 2 (two) pixels to the right and down, and then behind the original text.
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Strings
_imageFromString('string.jpg', "Strings!", 'AUGUSTUS.TTF', 40, 'FF6600', 'FFFFCC')
_imageFromString (str $target, str $text, str $font, int $fontsize, str $color, str $bgcolor)
Create an image ($target) centralised (vertical and horizontal) of the input text ($text). Set the font (this is done by calling in any True Type font file ($font)), the font size ($fontsize), the text color ($color) and the background color ($bgcolor).

Border
_imageBorder('original.jpg', 'border.jpg', 8, '339900')
_imageBorder (str $source, str $target, int $borderwidth, str $bordercolor)
Places a border of desired size ($borderwidth) and color ($bordercolor) around an image ($source)
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Crop
_imageCrop('original.jpg', 'crop.jpg', 0, 0, 100, 100)
_imageCrop (str $source, str $target, int $x, int $y, int $w, int $h)
Crops an image ($source) starting at co-ordinates ($x & $y) to new size ($w & $h)
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Perspective
_imagePerspective('original.jpg', 'perspective.jpg', 15, 0, 'ffffff')
_imagePerspective (str $source, str $target, int $angle, int $dir, str $bgcolor)
Add perspective to an image ($source) by desired degrees ($angle) and desired direction ($dir) (left = 0, right = 1), with a defined background colour ($bgcolor).
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Skew
_imageSkew('original.jpg', 'skew.jpg', 0.25, 'right', 'ffffff')
_imageSkew (str $source, str $target, int $angle, int $dir, str $bgcolor)
Skews an image ($source) by desired degrees ($angle) and desired direction ($dir) (left, right), with a defined background colour ($bgcolor).
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Blur
_imageBlur('original.jpg', 'blur.jpg')
_imageBlur (str $source, str $target)
Blur an image ($source).
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Change File Type
_imageCopyToType('original.jpg', 'original.gif')
_imageCopyToType (str $source, str $target)
Changes an image ($source) to a new image type (target). Supports JPG, GIF and PNG
$target is an output file name. If you wish to ovewrite the original image, make $source and $target equal.

Function Stacking
Functions can also be stacked (run one after the other on the same image) to create a combination of effects:
$file = 'multi.jpg';
_imageBorder('original.jpg', $file, 6, '000000');
_imageTextWrite($file, $file, "Multi Effects", 'AUGUSTUS.TTF', 20, 'FFCC00', '484848');
_imageReflect($file, $file, 'ffffff');


Posted on Wednesday 27th May, 2009 at 11:47 am by Paul Whitrow, and filed under PHP
Wanna keep up to date? Subscribe to the site feeds.
Browse More Entries
Have your say:
NB: Comments are subject to administration.




Comments about ‘PHP Image Library’
6th February 2012, 7:44 am
he alarm, cheap jordan shoes jordan shoes for cheap call Cheap Jordan 6 the emergency Buy Jordan 6 phone, and keep the scene Jordan 6 Sale until the police response alarm Air Jordan Retro 7 investigat...
6th February 2012, 3:48 am
lood discount christian louboutin , blue driver immediately stepped christian louboutin outlet on the gas , all christian louboutin discount the way to gallop . As has been into the ...
6th February 2012, 1:16 am
handsome as canada goose jacket Wangjing Li donated 400 canada goose jacket sale ml of blood. Zhi-Guo Yan canadian goose jacket was taken
been discontinued...
6th February 2012, 1:15 am
handsome as canada goose jacket Wangjing Li donated 400 canada goose jacket sale ml of blood. Zhi-Guo Yan canadian goose jacket was taken
been discontinued...
6th February 2012, 1:14 am
olding christian louboutin shoes
christian louboutin heels fact that christian louboutin boots he lives in Mountain View Man Ting Fang River mouth area sixth district...
6th February 2012, 1:13 am
olding christian louboutin shoes
christian louboutin heels fact that christian louboutin boots he lives in Mountain View Man Ting Fang River mouth area sixth district...
1st February 2012, 7:51 am
In this modern and fashionable society, people are pursuing for true religion uk cool, unique, stylish and innovative. Whether it is discount true religion jeans or fashion accessories all means a lot for modern society of today. Same is the case with trendy looking pandora charms. When these are pandora bracelets sale, the excitement just gets doubled. Most chic looking pandora jewellery uk are in fashion now. These are one of the favorite fashion accessories for men and women long time ago. If you have not yet tried pandora jewellery, it's time to own one and feel the difference it can make to your personality. These are just brilliant and fabulous pandora charms sale. They are most iconic and can provide you with a new feeling and enhance confidence. The quality of pandora charms sale uk is just superior to what you have dreamt of.
1st February 2012, 6:59 am
With the P57 Hoodia Cactus Slimming Capsule of weight reduction products and services in the marketplace, it can become very confusing figuring out which type and make of products to choose when attempting to lose weight. Nevertheless, it helps a whole lot to note that every one of the fat reduction medicines available on the market today can easily be sorted into two different kinds thereby making the decision making process a little bit simpler. P57 Hoodia is usually either Pai You Ji or non-prescription dependent. Pai You Ji Plus Tea, also referred to as ethical drugs, are generally medications that can't be paid for without permission from an authorized doctor. In North America in particular, medications that require a prescription must be initially authorized by the Pai You Ji - the Pai You Ji organization accountable for safeguarding the public from Pai You Ji Plus.
11th January 2012, 2:09 am
The newspaperred soled shoes reported that the christian louboutin shoes1793 penny was sold atlouboutin shoes such a high price because itchristian louboutin online is rare and in excellent shape, showing no wear on discount christian louboutin shoesits lettering, its Lady Liberty face or the wreath on its back.
11th January 2012, 2:08 am
The newspaperred soled shoes reported that the christian louboutin shoes1793 penny was sold atlouboutin shoes such a high price because itchristian louboutin online is rare and in excellent shape, showing no wear on discount christian louboutin shoesits lettering, its Lady Liberty face or the wreath on its back.
10th January 2012, 2:57 am
Our saleauuggs.com online store offering a variety of Australia UGG Boots, it represents unique luxury qualities; feel like silk wool skin so that we make sure that when you wearing UGG brand boots would feel comfortable immediately. External are whole sheep wool, built-in the warmth degree of wool and waterproof, moisture-proof function, all goods must go through rigorous tests to ensure the most comfortable fit, so as the memory foam join, your feet and shoes can achieve the most perfect fit. Discount UGG boots is a seasonal fashion style design, with personalized, fashionable clothing, the U.S. interpretation of the New York street fashion. Cheap UGG Boots Now come into your life every day, bringing style, comfort and unique luxury.
Our company supply baby ugg boots,kids ugg boots,men ugg boots,women ugg boots,ugg handbags,ugg gloves,ugg scarf,ugg earmuffs,ugg slippers,ugg sandals.Fast shipping & Fast delivery!
10th January 2012, 2:31 am
Our saleauuggs.com online store offering a variety of Australia UGG Boots, it represents unique luxury qualities; feel like silk wool skin so that we make sure that when you wearing UGG brand boots would feel comfortable immediately. External are whole sheep wool, built-in the warmth degree of wool and waterproof, moisture-proof function, all goods must go through rigorous tests to ensure the most comfortable fit, so as the memory foam join, your feet and shoes can achieve the most perfect fit. Discount UGG boots is a seasonal fashion style design, with personalized, fashionable clothing, the U.S. interpretation of the New York street fashion. Cheap UGG Boots Now come into your life every day, bringing style, comfort and unique luxury.
Our company supply baby ugg boots,kids ugg boots,men ugg boots,women ugg boots,ugg handbags,ugg gloves,ugg scarf,ugg earmuffs,ugg slippers,ugg sandals.Fast shipping & Fast delivery!
2nd December 2009, 12:59 pm
Hi Fraz,
you can specify a left or right perspective, but not top or bottom (see documentation). Feel free to have a go at developing the code to do this, then post back here if you get it.
2nd December 2009, 9:52 am
Hey....seems great library....One question...is this limited to only JPEG images?
And do know how to make image perspective in different directions? I mean different positions for Left Right Top Bottom....