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 | 6 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 12:47 pm 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’
18th May 2012, 7:00 am
bout this!"The this!"The
replica designer handbags wholesale
this!"The mother of the boy spoke anonymously to the LAtelling the paper that she had brought the letters to to cheap jordan shoes to Miramonte Elementary School administrators and was told she was
up stories. She took the letters to the Los Angeles Angeles wholesale designer handbags Angeles Sheriff's department, but was told to take up the
with the school, according to the Times.Luisjuan was not able able
cheap jordans from china
able to be reached for this story.The school has beenTuesday and today as its entire faculty is being replaced replaced cheap jordans for sale replaced and interrogated by school officials. The school will reopen
attorney representing alleged victims said the school had a systemic systemic
jordan max
systemic problem of abuse because of teachers who preyed onof single-family homes.The lawyer, Keith Davis, is representing three victims victims cheap designer handbags victims in the case against Berndt. He previously represented three
students from Miramonte who pressed char
17th May 2012, 4:23 pm
Some burns from waxing can be so severe that they even leave scars Coach Purses Outlet. you run the risk of severely burning your skin Louis Vuitton Outlet. on an Adventure travel tour It could be just the thing Louis Vuitton Handbags. our business' financial commitments Juicy Couture Bags. a million volts and comes with a 3 LED flashlight that can be Coach Coupons. You could expect the entire process to take around six months Gucci Outlet. they are applying the laser Gucci Bags Outlet. This is also a much safer option as well Coach Bags On Sale. And now to fight this we should use a high-quality Cheap Louis Vuitton. Water sports are the most popular form of adventure activity known Chanel Purses.
17th May 2012, 3:40 am
Associated with Meningioma RiskBy JANE E. ALLEN, ABC News Medical Medical buy sunglasses online Medical UnitApril 10, 2012— Frequent dental X-rays, particularly in childhood,
be linked to an increased risk of the most common common
Jordan 11
common brain tumor in adulthood, say researchers who suggest minimizinguse of X-rays, especially among people without symptoms of tooth tooth replica sunglasses tooth or gum problems.Dr. Elizabeth B. Claus, an epidemiologist at
Yale School of Medicine in New Haven, Conn., and at at
cheap jordans free shipping
at Brigham and Women's Hospital in Boston, led the newsuggesting an association between mouth X-rays and tumors called meningiomas. meningiomas.
jordan concord 11
meningiomas. The tumors, which take their name because they arisethe meninges, the membrane surrounding the brain and spinal cord, cord,
buy sunglasses online
cord, account for about a third of brain tumors detectedthe United States. Although they're most often benign and slow-growing, slow-growing,
Jordan 11
slow-growing, they can cause disability by exerting pressure on theonce-yearly or more frequent bitewing X-rays, which expose a small small jordan concord 11 small piece of film placed between the teeth to a
16th May 2012, 10:01 am
Le sacs lancel soldés filet a aussi tendance à rendre le potentiel pour vous personnellement pour voir la gamme Lancel toute véritable à l'intérieur du aisance et le confort de la lancel sacs résidence et pour obtenir la conception précise que vous voulez. En outre, vous pouvez acheter un sac Lancel véritable dans le Web à un coût qui est réduit de magasins de détail. Pour beaucoup plus de données sur la sélection sac sacs soldés authentique, voir les détails dans ce assessment.Females véritables sacs Lancel possèdent un enthousiasme pour les sacs de mode, sacs à main Lancel en résultat sont parmi les meilleurs amis des femmes. Les sacs sont disponibles dans une vaste sélection l'affichage de la texture la plus fascinante, la lancel sac soldes couleur et le lustre. Différents types de bourses sont accessibles à la mode comme des sacs en satin, sacs brodés de paillettes, sacs, sacs en cuir, sacs de jute, sacs de marque et sacs de créateurs. Sur l'ensemble des bourses, celles de collection lancel créateurs sont les plus favorables en raison de leur caractère unique, sacs de créateurs et de durabilité style.Original sont un accessoire branché à chaud qui complète votre look en ajoutant une touche de sophistication. Ils pourraient être coûteux, mais se sac lancel solde distinguent et définissez votre style.http://www.lancel-soldes.com/
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....