Image functions
You can use the image functions in PHP to get the size of JPEG, GIF, and PNG images, and if you have the GD library (available at http://www.boutell.com/gd/) you will also be able to create and manipulate GIF images.
Image functions
GetImageSize
Name
GetImageSize — get the size of a GIF, JPG or PNG image
Description
array getimagesize(string filename, array [imageinfo]);
The GetImageSize function will determine the size of any GIF, JPG or PNG image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag.
Returns an array with 4 elements. Index 0 contains the width of the image in pixels. Index 1 contains the height. Index 2 a flag indicating the type of the image. 1 = GIF, 2 = JPG, 3 = PNG. Index 3 is a text string with the correct "height=xxx width=xxx" string that can be used directly in an IMG tag.
Example 1. GetImageSize
<?php $size = GetImageSize("img/flag.jpg"); ?>
<IMG SRC="img/flag.jpg" <?php echo $size[3]; ?>>
The optional imageinfo parameter allows you to extract some extended information from the image file. Currently this will return the diffrent JPG APP markers in an associative Array. Some Programs use these APP markers to embedd text information in images. A very common one in to embed IPTC http://www.xe.net/iptc/ information in the APP13 marker. You can use the iptcparse function to parse the binary APP13 marker into something readable.
Example 2. GetImageSize returning IPTC
<?php
$size = GetImageSize("testimg.jpg",&$info); if (isset($info["APP13"])) {
$iptc = iptcparse($info["APP13"]); var_dump($iptc);
}
?>
This function does not require the GD image library.
ImageArc
Name
ImageArc — draw a partial ellipse
Description
int imagearc(int im, int cx, int cy, int w, int h, int s, int e, int col);
ImageArc draws a partial ellipse centered at cx, cy (top left is 0,0) in the image represented by im. w and h specifies the ellipse's width and height respectively while the start and end points are specified in degrees indicated by the s and e arguments.
ImageChar
Name
ImageChar — draw a character horizontally
Description
int imagechar(int im, int font, int x, int y, string c, int col);
ImageChar draws the first character of c in the image identified by id at coordinates x, y (top left is 0,0) with the color col. If font is 1, 2, 3, 4 or 5, a built-in font is used.
See also imageloadfont.
ImageCharUp
Name
ImageCharUp — draw a character vertically
Description
int imagecharup(int im, int font, int x, int y, string c, int col);
ImageCharUp draws the character c vertically in the image identified by im at coordinates x, y (top left is 0, 0) with the color col. If font is 1, 2, 3, 4 or 5, a built-in font is used.
See also imageloadfont.
ImageColorAllocate
Name
ImageColorAllocate — allocate a color for an image
Description
int imagecolorallocate(int im, int red, int green, int blue);
ImageColorAllocate returns a color identifier representing the color composed of the given RGB components. The im argument is the return from the imagecreate function. ImageColorAllocate must be called to create each color that is to be used in the image represented by im.
ImageColorTransparent
Name
ImageColorTransparent — define a color as transparent
Description
int imagecolortransparent(int im, int [col]);
ImageColorTransparent sets the transparent color in the im image to col. im is the image identifier returned by imagecreate and col is a color identifier returned by imagecolorallocate.
The identifier of the new (or current, if none is specified) transparent color is returned.
ImageCopyResized
Name
ImageCopyResized — copy and resize part of an image
Description
int imagecopyresized(int dst_im, int src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);
ImageCopyResized copies a rectangular portion of one image to another image. dst_im is the destination image, src_im is the source image identifier. If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if dst_im is the same as src_im) but if the regions overlap the results will be unpredictable.
ImageCreate
Name
ImageCreate — create a new image
Description
int imagecreate(int x_size, int y_size);
ImageCreate returns an image identifier representing a blank image of size x_size by y_size.
ImageCreateFromGif
Name
ImageCreateFromGif — create a new image from file or URL
Description
int imagecreatefromgif(string filename);
ImageCreateFromGif returns an image identifier representing the image obtained from the given filename.
ImageDashedLine
Name
ImageDashedLine — draw a dashed line
Description
int imagedashedline(int im, int x1, int y1, int x2, int y2, int col);
ImageLine draws a dashed line from x1,y1 to x2,y2 (top left is 0,0) in image im of color col. See also imageline.
ImageDestroy
Name
ImageDestroy — destroy an image
Description
int imagedestroy(int im);
ImageDestroy frees any memory associated with image im. im is the image identifier returned by the
imagecreate function.
ImageFill
Name
ImageFill — flood fill
Description
int imagefill(int im, int x, int y, int col);
ImageFill performs a flood fill starting at coordinate x, y (top left is 0,0) with color col in the image im.
ImageFilledPolygon
Name
ImageFilledPolygon — draw a filled polygon
Description
int imagefilledpolygon(int im, array points, int num_points, int col);
ImageFilledPolygon creates a filled polygon in image im. points is a PHP array containing the polygon's vertices, ie. points[0] = x0, points[1] = y0, points[2] = x1, points[3] = y1, etc. num_points is the total number of vertices.
ImageFilledRectangle
Name
ImageFilledRectangle — draw a filled rectangle
Description
int imagefilledrectangle(int im, int x1, int y1, int x2, int y2, int col);
ImageFilledRectangle creates a filled rectangle of color col in image im starting at upper left coordinates x1, y1 and ending at bottom right coordinates x2, y2. 0, 0 is the top left corner of the image.
ImageFillToBorder
Name
ImageFillToBorder — flood fill to specific color
Description
int imagefilltoborder(int im, int x, int y, int border, int col);
ImageFillToBorder performs a flood fill whose border color is defined by border. The starting point for the fill is x,y (top left is 0,0) and the region is filled with color col.
ImageFontHeight
Name
ImageFontHeight — get font height
Description
int imagefontheight(int font);
Returns the pixel width of a character in font.
See also imagefontwidth and imageloadfont.
ImageFontWidth
Name
ImageFontWidth — get font width
Description
int imagefontwidth(int font);
Returns the pixel width of a character in font.
See also imagefontheight and imageloadfont.
ImageGif
Name
ImageGif — output image to browser or file
Description
int imagegif(int im, string filename);
ImageGif creates the GIF file in filename from the image im. The im argument is the return from the
imagecreate function.
The image format will be GIF87a unless the image has been made transparent with
imagecolortransparent, in which case the image format will be GIF89a.
The filename argument is optional, and if left off, the raw image stream will be output directly. By sending an image/gif content-type using the header function, you can create a PHP script that outputs GIF images directly.
ImageInterlace
Name
ImageInterlace — enable or disable interlace
Description
int imageinterlace(int im, int [interlace]);
ImageInterlace turns the interlace bit on or off. If interlace is 1 the im image will be interlaced, and if interlace is 0 the interlace bit is turned off.
This functions returns whether the interlace bit is set for the image.
ImageLine
Name
ImageLine — draw a line
Description
int imageline(int im, int x1, int y1, int x2, int y2, int col);
ImageLine draws a line from x1,y1 to x2,y2 (top left is 0,0) in image im of color col. See also imagecreate and imagecolorallocate.
ImageLoadFont
Name
ImageLoadFont — load a new font
Description
int imageloadfont(string file);
ImageLoadFont loads a user-defined bitmap font and returns an identifier for the font (that is always greater than 5, so it will not conflict with the built-in fonts).
The font file format is currently binary and architecture dependent. This means you should generate the font files on the same type of CPU as the machine you are running PHP on.
Table 1. Font file format
byte position |
C data type |
description |
---|---|---|
byte 0-3 |
int |
number of characters in the font |
byte 4-7 |
int |
value of first character in the font (often 32 for space) |
byte 8-11 |
int |
pixel width of each character |
byte 12-15 |
int |
pixel height of each character |
byte 16- |
char |
array with character data, one byte per pixel in each character, for a total of (nchars*width*height) bytes. |
See also ImageFontWidth and ImageFontHeight.
ImagePolygon
Name
ImagePolygon — draw a polygon
Description
int imagepolygon(int im, array points, int num_points, int col);
ImagePolygon creates a polygon in image id. points is a PHP array containing the polygon's vertices, ie. points[0] = x0, points[1] = y0, points[2] = x1, points[3] = y1, etc. num_points is the total number of vertices.
See also imagecreate.
ImageRectangle
Name
ImageRectangle — draw a rectangle
Description
int imagerectangle(int im, int x1, int y1, int x2, int y2, int col);
ImageRectangle creates a rectangle of color col in image im starting at upper left coordinate x1,y1 and ending at bottom right coordinate x2,y2. 0,0 is the top left corner of the image.
ImageSetPixel
Name
ImageSetPixel — set a single pixel
Description
int imagesetpixel(int im, int x, int y, int col);
ImageSetPixel draws a pixel at x,y (top left is 0,0) in image im of color col. See also imagecreate and imagecolorallocate.
ImageString
Name
ImageString — draw a string horizontally
Description
int imagestring(int im, int font, int x, int y, string s, int col);
ImageString draws the string s in the image identified by im at coordinates x,y (top left is 0,0) in color col. If font is 1, 2, 3, 4 or 5, a built-in font is used.
See also imageloadfont.
ImageStringUp
Name
ImageStringUp — draw a string vertically
Description
int imagestringup(int im, int font, int x, int y, string s, int col);
ImageStringUp draws the string s vertically in the image identified by im at coordinates x,y (top left is 0,0) in color col. If font is 1, 2, 3, 4 or 5, a built-in font is used.
See also imageloadfont.
ImageSX
Name
ImageSX — get image width
Description
int imagesx(int im);
ImageSX returns the width of the image identified by im. See also imagecreate and imagesy.
ImageSY
Name
ImageSY — get image height
Description
int imagesy(int im);
ImageSY returns the height of the image identified by im. See also imagecreate and imagesx.
ImageTTFBBox
Name
ImageTTFBBox — give the bounding box of a text using TypeType fonts
Description
array ImageTTFBBox(int size, int angle, string fontfile, string text);
This function calculates and returns the bounding box in pixels a TrueType text.
text
The string to be measured.
size
The font size.
fontfile
The name of the TrueType font file. (Can also be an URL.)
angle
Angle in degrees in which text will be measured.
ImageTTFBBox returns an array with 8 elements representing four points making the bounding box of the text:
0 |
lower left corner, X position |
---|---|
1 | lower left corner, Y position |
2 | lower right corner, X position |
3 | lower right corner, Y position |
4 | upper right corner, X position |
5 | upper right corner, Y position |
6 | upper left corner, X position |
7 | upper left corner, Y position |
The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner seeing the text horizontallty.
This function requires both the GD library and the Freetype library. See also ImageTTFText.
ImageTTFText
Name
ImageTTFText — write text to the image using a TrueType fonts
Description
array ImageTTFText(int im, int size, int angle, int x, int y, int col, string
fontfile, string text);
ImageTTFText draws the string text in the image identified by im, starting at coordinates x,y (top left is 0,0), at an angle of angle in color col, using the TrueType font file identified by fontfile.
The coordinates given by x,y will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the ImageString, where x,y define the upper-right corner of the first character.
angle is in degrees, with 0 degrees being left-to-right reading text (3 o'clock direction), and higher values representing a counter-clockwise rotation. (i.e., a value of 90 would result in bottom-to-top reading text).
fontfile is the path to the TrueType font you wish to use.
text is the text string which may include UTF-8 character sequences (of the form: { ) to access characters in a font beyond the first 255.
col is the color index. Using the negative of a color index has the effect of turning off antialiasing.
ImageTTFText returns an array with 8 elements representing four points making the bounding box of the text. The order of the points is upper left, upper right, lower right, lower left. The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner when you see the text horizontallty.
This example script will produce a black GIF 400x30 pixels, with the words "Testing..." in white in the font Arial.
Example 1. ImageTTFText
<?php
Header("Content-type: image/gif");
$im = imagecreate(400,30);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
ImageTTFText($im, 20, 0, 10, 20, $white, "/path/arial.ttf", "Testing... Omega: Ω");
ImageGif($im);
ImageDestroy($im);
?>
This function requires both the GD library and the Freetype library. See also ImageTTFBBox.
ImageColorAt
Name
ImageColorAt — get the index of the color of a pixel
Description
int imagecolorat(int im, int x, int y);
Returns the index of the color of the pixel at the specified location in the image. See also imagecolorset and imagecolorsforindex.
ImageColorClosest
Name
ImageColorClosest — get the index of the closest color to the specified color
Description
int imagecolorclosest(int im, int red, int green, int blue);
Returns the index of the color in the palette of the image which is "closest" to the specified RGB value.
The "distance" between the desired color and each color in the palette is calculated as if the RGB values represented points in three-dimensional space.
See also imagecolorexact.
ImageColorExact
Name
ImageColorExact — get the index of the specified color
Description
int imagecolorexact(int im, int red, int green, int blue);
Returns the index of the specified color in the palette of the image. If the color does not exist in the image's palette, -1 is returned.
See also imagecolorclosest.
ImageColorResolve
Name
ImageColorResolve — get the index of the specified color or its closest possible alternative
Description
int imagecolorresolve(int im, int red, int green, int blue);
This function is guaranteed to return a color index for a requested color, either the exact color or the closest possible alternative.
See also imagecolorclosest.
ImageColorSet
Name
ImageColorSet — set the color for the specified palette index
Description
bool imagecolorset(int im, int index, int red, int green, int blue);
This sets the specified index in the palette to the specified color. This is useful for creating flood-fill-like effects in paletted images without the overhead of performing the actual flood-fill.
See also imagecolorat.
ImageColorsForIndex
Name
ImageColorsForIndex — get the colors for an index
Description
array imagecolorsforindex(int im, int index);
This returns an associative array with red, green, and blue keys that contain the appropriate values for the specified color index.
See also imagecolorat and imagecolorexact.
ImageColorsTotal
Name
ImageColorsTotal — find out the number of colors in an image's palette
Description
int imagecolorstotal(int im);
This returns the number of colors in the specified image's palette. See also imagecolorat and imagecolorsforindex.