Image functions Graphics 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 &url.gd;) you will also be able to create and manipulate images. 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. 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 &url.iptc; information in the APP13 marker. You can use the iptcparse function to parse the binary APP13 marker into something readable. 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 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 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 with its upper-left at 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 (with higher numbers corresponding to larger fonts). See also imageloadfont. 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 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. $white = ImageColorAllocate($im, 255,255,255); $black = ImageColorAllocate($im, 0,0,0); 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 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 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 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 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 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 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. 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 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 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 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. imagecreatefromgif returns an empty string on failure. It also outputs an error message, which unfortunately displays as a broken link in a browser. To ease debugging the following example will produce an error GIF: Example to handle an error during creation (courtesy vic@zymsys.com ) function LoadGif($imgname) { $im = @imagecreatefromgif($imgname); /* Attempt to open */ if ($im == "") { /* See if it failed */ $im = ImageCreate(150,30); /* Create a blank image */ $bgc = ImageColorAllocate($im,255,255,255); $tc = ImageColorAllocate($im,0,0,0); ImageFilledRectangle($im,0,0,150,30,$bgc); ImageString($im,1,5,5,"Error loading $imgname",$tc); /* Output an errmsg */ } return $im; } Since all GIF support was removed from the GD library in version 1.6, this function is not available if you are using that version of the GD library. 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 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 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 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 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 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 get font height Description int imagefontheight int font Returns the pixel height of a character in the specified font. See also imagefontwidth and imageloadfont. ImageFontWidth get font width Description int imagefontwidth int font Returns the pixel width of a character in font. See also imagefontheight and imageloadfont. 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 header, you can create a PHP script that outputs GIF images directly. Since all GIF support was removed from the GD library in version 1.6, this function is not available if you are using that version of the GD library. 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 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 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. 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 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. ImagePSBBox give the bounding box of a text rectangle using PostScript Type1 fonts Description array imagepsbbox string text int font int size int space int width float angle size is expressed in pixels. space allows you to change the default value of a space in a font. This amount is added to the normal value and can also be negative. tightness allows you to control the amount of white space between characters. This amount is added to the normal character width and can also be negative. angle is in degrees. Parameters space and tightness are expressed in character space units, where 1 unit is 1/1000th of an em-square. Parameters space, tightness and angle are optional. The bounding box is calculated using information available from character metrics, and unfortunately tends to differ slightly from the results achieved by actually rasterizing the text. If the angle is 0 degrees, you can expect the text to need 1 pixel more to every direction. This function returns an array containing the following elements: 0 lower left x-coordinate 1 lower left y-coordinate 2 upper right x-coordinate 3 upper right y-coordinate See also imagepstext. ImagePSEncodeFont change the character encoding vector of a font Description int imagepsencodefont string encodingfile Loads a character encoding vector from from a file and changes the fonts encoding vector to it. As a PostScript fonts default vector lacks most of the character positions above 127, you'll definitely want to change this if you use an other language than english. The exact format of this file is described in T1libs documentation. T1lib comes with two ready-to-use files, IsoLatin1.enc and IsoLatin2.enc. If you find yourself using this function all the time, a much better way to define the encoding is to set ps.default_encoding in the configuration file to point to the right encoding file and all fonts you load will automatically have the right encoding. ImagePSFreeFont free memory used by a PostScript Type 1 font Description void imagepsfreefont int fontindex See also imagepsloadfont. ImagePSLoadFont load a PostScript Type 1 from file Description int imagepsloadfont string filename In the case everything went right, a valid font index will be returned and can be used for further purposes. Otherwise the function returns false and prints a message describing what went wrong. See also imagepsfreefont. ImagePSText to draw a text string over an image using PostScript Type1 fonts Descriptiom array imagepstext int image string text int font int size int foreground int background int x int y int space int tightness float angle int antialias_steps size is expressed in pixels. foreground is the color in which the text will be painted. background is the color to which the text will try to fade in with antialiasing. No pixels with the color background are actually painted, so the background image does not need to be of solid color. The coordinates given by x, y will define the origin (or reference point) 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. Refer to PostScipt documentation about fonts and their measuring system if you have trouble understanding how this works. space allows you to change the default value of a space in a font. This amount is added to the normal value and can also be negative. tightness allows you to control the amount of white space between characters. This amount is added to the normal character width and can also be negative. angle is in degrees. antialias_steps allows you to control the number of colours used for antialiasing text. Allowed values are 4 and 16. The higher value is recommended for text sizes lower than 20, where the effect in text quality is quite visible. With bigger sizes, use 4. It's less computationally intensive. Parameters space and tightness are expressed in character space units, where 1 unit is 1/1000th of an em-square. Parameters space, tightness, angle and antialias are optional. This function returns an array containing the following elements: 0 lower left x-coordinate 1 lower left y-coordinate 2 upper right x-coordinate 3 upper right y-coordinate See also imagepsbbox. 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 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 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 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 get image width Description int imagesx int im ImageSX returns the width of the image identified by im. See also imagecreate and imagesy. 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 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 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: &#123; ) 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. 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: &#937;"); ImageGif($im); ImageDestroy($im); ?> This function requires both the GD library and the FreeType library. See also ImageTTFBBox.