# Creating JPEG, PNG, and WBMP Images PHP

All the functions in this section require the GD library. If you haven't compiled it as part of your PHP module, either load it automatically by editing php3.ini, or use the dl function. Some of these functions also require other libraries, which allow you to use font files.

To get started you can use either imagecreate to start with a blank graphic, or a function such as imagecreatefrompng to load a PNG from a file. Coordinates in these functions treat (0, 0) to be the top-left corner and refer to pixels. Likewise, any size arguments refer to pixels.

When creating images with these functions, you can't simply decide to output an image in the middle of a script that outputs HTML. You must create a separate script that sends a content-type header. All the examples illustrate this idea.

For functions that use fonts, there are five built-in fonts numbered 1, 2, 3, 4, and 5. You may also load fonts, which will always have identifiers greater than five.

boolean imagearc(integer image, integer center_x, integer center_y, integer width, integer height, integer start, integer end, integer color)
Use imagearc to draw a section of an ellipse. The first argument specifies a valid image. The ellipse is centered at center_x and center_y. The height and width are set by the respective arguments in pixels. The start and end points of the curve are given in degrees. Zero degrees is at 3 o'clock and proceeds counterclockwise.

<?
/*
** cut out a circular view of an image
*/
//attempt to open image, suppress error messages
if(!($image = @imagecreatefrompng("php.png"))) { //error, so create an error image and exit$image = imagecreate(200,200);
$colorWhite = imagecolorallocate($image, 255, 255, 255);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0,$colorWhite);
imagestring($image, 4, 10, 10, "Couldn't load image!",$colorBlack);
imagejpeg($image); exit(); } //create a color to be transparent, hopefully //not already in the image$colorMagenta = imagecolorallocate($image, 255, 0, 255); //draw a circle imagearc($image,
100, 50,
100, 100,
0, 360,
$colorMagenta); //fill outside of circle with Magenta imagefilltoborder($image, 0, 0, $colorMagenta,$colorMagenta);
//turn magenta transparent
imagecolortransparent($image,$colorMagenta);
//send image to browser
imagepng($image); ?> boolean imagechar(integer image, integer font, integer x, integer y, string character, integer color) The imagechar function draws a single character at the given pixel. The font argument can be a loaded font or one of the five built-in fonts. The character will be oriented horizontally—that is, left to right. The x and y coordinates refer to the top-left corner of the letter. boolean imagecharup(integer image, integer font, integer x, integer y, string character, integer color) The imagecharup function operates identically to imagechar, except that the character is oriented vertically, bottom to top. imagecharup integer imagecolorallocate(integer image, integer red, integer green, integer blue) The imagecolorallocate function allocates a color in the given image. The color is specified by the amount of red, green, and blue. An identifier is returned for referring to this color in other functions. <? /* ** Draw Red, Green, Blue circles */ //create white square$image = imagecreate(200,200);
$colorWhite = imagecolorallocate($image, 255,255,255);
$colorRed = imagecolorallocate($image, 255, 0, 0);
$colorGreen = imagecolorallocate($image, 0, 255, 0);
$colorBlue = imagecolorallocate($image, 0, 0, 255);
imagefill($image, 0, 0,$colorWhite);
//make red circle
imagearc($image, 50, 50, 100, 100, 0, 360,$colorRed);
imagefilltoborder($image, 50, 50,$colorRed, $colorRed); //make green circle imagearc($image, 100, 50, 100, 100, 0, 360, $colorGreen); imagefilltoborder($image, 100, 50, $colorGreen,$colorGreen);
//make blue circle
imagearc($image, 75, 75, 100, 100, 0, 360,$colorBlue);
imagefilltoborder($image, 75, 75,$colorBlue, $colorBlue); //send image header("Content-type: image/jpeg"); imagejpeg($image);
?>

integer imagecolorat(integer image, integer x, integer y)
The imagecolorat function returns the index of the color at the specified pixel. All images have a palette of arbitrary colors referred to by integers.

<?
/*
** Change a color
*/
//attempt to open image, suppress error messages
if(!($image = @imagecreatefromjpeg("php_lang.jpg"))) { //error, so create an error image and exit$image = imagecreate(200,200);
$colorWhite = imagecolorallocate($image, 255, 255, 255);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0,$colorWhite);
imagestring($image, 4, 10, 10, "Couldn't load image!",$colorBlack);
imagejpeg($image); exit(); } //get the color at (10,10)$colorIndex = imagecolorat($image, 10, 10); //change that color to red imagecolorset($image, $colorIndex, 255, 0, 0); //send image header("Content-type: image/jpeg"); imagejpeg($image);
?>

integer imagecolorclosest(integer image, integer red, integer green, integer blue)
The imagecolorclosest function returns the index of the color in the given image closest to the given color. Colors are treated as three-dimensional coordinates, and closeness is defined by the distance between two points.

<?
/*
** Compare closest color to real color
*/
//attempt to open image, suppress error messages
if(!($image = @imagecreatefromjpeg("php_lang.jpg"))) { //error, so create an error image and exit$image = imagecreate(200,200);
$colorWhite = imagecolorallocate($image, 255, 255, 255);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0,$colorWhite);
imagestring($image, 4, 10, 10, "Couldn't load image!",$colorBlack);
imagejpeg($image); exit(); } //find index of color closest to pure magenta$magentaIndex = imagecolorclosest($image, 255, 0, 255); //get RGB values$colorArray = imagecolorsforindex($image,$magentaIndex);
//allocate closest color
$colorMagenta = imagecolorallocate($image,
$colorArray["red"],$colorArray["green"],
$colorArray["blue"]); //draw a square imagefilledrectangle($image, 10, 10, 100, 100, $colorMagenta); //send image header("Content-type: image/jpeg"); imagejpeg($image);
?>
***

integer imagecolorexact(integer image, integer red, integer green, integer blue)
Use the imagecolorexact function to find the index of the color in the given image that matches the given color exactly. If the color doesn't exist, negative one (-1) is returned.

?<
/*
** Check that an image contains black
** If so, change all black to cyan.
*/
//attempt to open image, suppress error messages
if(!($image = @imagecreatefromjpeg("php_lang.jpg"))) { //error, so create an error image and exit$image = imagecreate(200,200);
$colorWhite = imagecolorallocate($image, 255, 255, 255);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0,$colorWhite);
imagestring($image, 4, 10, 10, "Couldn't load image!",$colorBlack);
imagejpeg($image); exit(); } //find index of black$blackIndex = imagecolorexact($image, 0, 0, 0); if($blackIndex >= 0)
{
//make all black areas cyan
imagecolorset($image,$blackIndex, 0, 255, 255);
}
//send image
imagejpeg($image); ?> integer imagecolorresolve(integer image, integer red, integer green, integer blue) The imagecolorresolve function returns a color identifier based on a specified color. If the color does not exist in the image's palette, it will be added. In the event that the color cannot be added, an identifier for the closest color will be returned. ?< /* ** Attempt to draw a magenta square */ //attempt to open image, suppress error messages if(!($image = @imagecreatefromjpeg("php_lang.jpg")))
{
//error, so create an error image and exit
$image = imagecreate(200,200);$colorWhite = imagecolorallocate($image, 255, 255, 255);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $colorWhite); imagestring($image, 4, 10, 10, "Couldn't load image!",
$colorBlack); header("Content-type: image/jpeg"; imagejpeg($image);
exit();
}
$colorMagenta = imagecolorresolve($image, 255, 0, 255);
// draw a square
imagefilledrectangle($image, 10, 10, 50, 50,$colorMagenta);
//send ima
imagejpeg($image); ?> boolean imagecolorset(integer image, integer index, integer red, integer green, integer blue) The imagecolorset function sets the color at the given index to the specified color. For an example of use, see the example for the imagecolorat function. array imagecolorsforindex(integer image, integer index) The imagecolorsforindex function returns an associative array with the red, green, and blue elements of the color for the specified color index. <? /* ** Show RGB values for a color */ //attempt to open image, suppress error messages if(!($image = @imagecreatefromjpeg("php_lang.jpg")))
{
//error, so create an error image and exit
$image = imagecreate(200,200);$colorWhite = imagecolorallocate($image, 255, 255, 255);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $colorWhite); imagestring($image, 4, 10, 10, "Couldn't load image!",
$colorBlack); header("Content-type: image/jpeg"); imagejpeg($image);
exit();
}
//get the color at (100,100)
$colorIndex = imagecolorat($image, 100, 100);
//get RGB values
$colorParts = imagecolorsforindex($image, $colorIndex); //display RGB values printf("RGB: " .$colorParts["red"] . ", " .
$colorParts["green"] . ", " .$colorParts["blue"]);
?>

integer imagecolorstotal(integer image)
The imagecolorstotal function returns the number of colors in the given image.

<?
/*
** Find number of colors in an image
*/
//attempt to open image, suppress error messages
if(!($image = @imagecreatefromjpeg("php_lang.jpg"))) { //error, so print error message print("Couldn't load image!"); } else { print("Total Colors: " . imagecolorstotal ($image));
}
?>

integer imagecolortransparent (integer image, integer color)
The imagecolortransparent function sets the given color transparent. The color argument is as returned by the imagecolorallocate functions.

<?
/*
** Create a red image with a transparent
** square cut out of it.
*/
//create red square
$image = imagecreate(200,200);$colorRed = imagecolorallocate($image, 255, 0, 0);$colorBlue = imagecolorallocate($image, 0, 0, 255); imagefill($image, 0, 0, $colorRed); //draw a smaller blue square imagefilledrectangle($image, 30, 30, 70, 70, $colorBlue); //make blue transparent imagecolortransparent($image, $colorBlue); //send image header("Content-type: image/png"); imagepng($image);
?>

integer imagecopyresized(integer destination, integer source, integer destination_x, integer destination_y, integer source_x, integer source_y, integer destination_width, integer destination_height, integer source_width, integer source_height)
The imagecopyresized function copies a portion of the source image into the destination image. If the destination width and height are different thanthe source width and height, the clip will be stretched or shrunk. It is possible to copy and paste into the same image, but if the destination and source overlap, there will be unpredictable results.

<?
/*
** Put PHP logo into field of red
** and resize it to 180x180
*/
//create red square
$image = imagecreate(200,200);$colorRed = imagecolorallocate($image, 255, 0, 0); imagefill($image, 0, 0, $colorRed); //attempt to open image, suppress error messages if(!($image2 = @imagecreatefromjpeg("php_lang.jpg")))
{
//error, so create an error image and exit
$image = imagecreate(200,200);$colorWhite = imagecolorallocate($image, 255, 255, 255);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $colorWhite); imagestring($image, 4, 10, 10, "Couldn't load image!",
$colorBlack); header("Content-type: image/jpeg"); imagejpeg($image);
exit();
}
//drop image2 into image, and stretch or squash it
imagecopyresized($image,$image2, 10, 10, 0, 0,
180, 180, imagesx($image2), imagesy($image2));
//send image
imagejpeg($image); ?> integer imagecreate(integer width, integer height) The imagecreate function returns an image identifier of the specified width and height. This identifier is used in many of the other image functions. <? /* ** Create a red square */ //create red square$image = imagecreate(200,200);
$colorRed = imagecolorallocate($image, 255, 0, 0);
imagefill($image, 0, 0,$colorRed);
//send image
imagejpeg($image); ?> integer imagecreatefromjpeg(string filename) Use imagecreatefromjpeg to load a JPEG image from a file. <? //attempt to open image, suppress error messages if(!($image = @imagecreatefromjpeg("php_lang.jpg")))
{
//error, so create an error image and exit
$image = imagecreate(200,200);$colorWhite = imagecolorallocate($image, 255, 255, 255);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $colorWhite); imagestring($image, 4, 10, 10, "Couldn't load image!",
$colorBlack); header("Content-type: image/jpeg"); imagejpeg($image);
exit();
}
//send image
imagejpeg($image); ?> integer imagecreatefrompng(string filename) Use imagecreatefrompng to load a PNG image from a file. <? //load an image and display it$image = imagecreatefrompng("php.png");
imagepng($image); ?> boolean imagedashedline(integer image, integer start_x, integer start_y, integer end_x, integer end_y, integer color) The imagedashedline function draws a dashed line from the start point to the end point. The color argument is a color identifier returned by imagecolorallocate. Use imageline to draw a solid line. imagesdashedline boolean imagedestroy(integer image) Use the imagedestroy function to clear memory associated with the specified image. Most of the time you will not need this function. PHP will clean up when your script ends. <? /* ** Create an image, then free its memory */ //create blue square$image = imagecreate(200,200);
$colorBlue = imagecolorallocate($image, 128, 128, 255);
imagefill($image, 0, 0,$colorBlue);
//send image
imagejpeg($image); //free memory associated with image imagedestroy($image);
?>

boolean imagefill(integer image, integer x, integer y, integer color)
The imagefill function performs a flood fill at the given point with the given color. The color argument must be as returned by imagecolorallocate. Starting at the given point, pixels are changed to the specified color. The coloring spreads out, continuing until a color different from the one at the specified point is encountered. See the description of imagearc for an example of use. See imagefilltoborder for an alternative.

boolean imagefilledpolygon(integer image, array points, integer number, integer color)
The imagefilledpolygon function creates a polygon with its inside filled with the specified color. The points argument is an array of x and y values for each point: Each point uses two array elements. The number argument reports how many points to use from the array.

imagefileledpolygon3

boolean imagefilledrectangle(integer image, integer top_left_x, integer top_left_y, integer bottom_right_x, integer bottom_right_y, integer color)
The imagefilledrectangle function draws a filled rectangle based on the top-left and bottom-right corners.

imagefileledrectangle

boolean imagefilltoborder(integer image, integer x, integer y, integer border_color, integer color)
The imagefilltoborder function will flood-fill an area bounded by the border_color argument. The flood fill will begin at the given coordinate. See imagecolorallocate for an example.

integer imagefontheight(integer font)
The imagefontheight function returns the height in pixels of the specified font, which may be a built-in font (1–5) or a font loaded with imagefontload.

<?
/*
** Create image just the right size for text
*/
$Text = "Core PHP Programming";$Font = 5;
$Width = imagefontwidth($Font) * strlen($Text);$Height = imagefontheight($Font); //create green square$image = imagecreate($Width,$Height);
$colorGreen = imagecolorallocate($image, 128, 255, 128);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0,$colorRed);
imagestring($image,$Font, 0, 0, $Text,$colorBlack);
//send image
imagejpeg($image); ?> integer imagefontwidth(integer font) The imagefontwidth function returns the width in pixels of the specified font, which may be a built-in font (1–5) or a font loaded with imagefontload. See imagefontheight for an example. boolean imagegammacorrect(integer image, double original, double new) The imagegammacorrect function changes the gamma for an image. Video display hardware is given a gamma rating that describes relatively how bright images appear. Identical images appear lighter on Macintosh hardware than on the typical Wintel clone. Each color in the palette of the image will be adjusted to the new gamma. At the time of this writing, imagegammacorrect was not part of PHP 4, although it was part of PHP 3. <? //attempt to open image, suppress error messages if(!($image = @imagecreatefromjpeg("php_lang.jpg")))
{
//error, so create an error image and exit
$image = imagecreate(200,200);$colorWhite = imagecolorallocate($image, 255, 255, 255);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $colorWhite); imagestring($image, 4, 10, 10, "Couldn't load image!",
$colorBlack); header("Content-type: image/jpeg"); imagejpeg($image);
exit();
}
imagegammacorrect($image, 2.2, 1.571); //send image header("Content-type: image/jpeg"); imagejpeg($image);
?>

boolean imageinterlace(integer image, boolean on)
Use imageinterlace to set an image as interlaced or not. If the change is successful, TRUE is returned. Interlaced images are stored so that they appear progressively rather than all at once. JPEGs marked as interlaced are called progressive JPEGs, in fact. While most browsers support interlaced product of the number of characters, the width, and the height. GIFs, many do not support interlaced PNGs or progressive JPEGs. You can read more on the subject in the GD library's manual.

>?
/*
** Create interlaced image
*/
//create red square
$image = imagecreate(200,200);$colorRed = imagecolorallocate($image, 255, 0, 0); imagefill($image, 0, 0, $colorRed); //set as interlaced imageinterlace($image, TRUE);
//send image
imagejpeg($image); ?> boolean imagejpeg(integer image, string filename, integer quality) The imagejpeg function either sends an image to the browser or writes it to a file. If a filename is provided, a JPEG file is created. Otherwise, the image is sent directly to the browser. The optional quality argument determines the compression level used in the image, and should range from 0 (lowest quality) to 10 (highest quality). <? /* ** create a blue square, save to disk */ //create image if it doesn't exist, //or it's older than an hour if(!file_exists("blue_square.jpg") OR (filectime("blue_square.jpg") (time() - 3600))) { //send debugging info print("!<-creating image->n"); //create a blue square$image = imagecreate(200, 100);
$colorBlue = imagecolorallocate($image, 128, 128, 255);
$colorWhite = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0,$colorBlue);
//add file creation time to image
imagestring($image, 4, 10, 10, date("Y-m-d H:i:s"),$colorWhite);
//write it to a file
imagejpeg($image, "blue_square.jpg"); } //print image tag that show image print("IMG SRC= "blue_square.jpg " " . "HEIGHT= "100 " WIDTH= "200 " BORDER="0">"); ?> boolean imageline(integer image, integer start_x, integer start_y, integer end_x, integer end_y, integer color) Like imagedashedline, imageline draws a line from the starting point to the ending point. In this case, the line is solid. imageline integer imageloadfont(string filename) The imageloadfont function loads a font and returns a font identifier that may be used with the other font functions. The fonts are stored as bitmaps in a special format. The PHP 3 distribution contained a Perl script for converting X11 .bdf files to PHP's format. The code that loads fonts is architecture dependent. Use this if you wish to create your own font files. PHP Font File Formate boolean imagepng(integer image, string filename) The imagepng function either sends an image to the browser or writes it to a file. If a filename is provided, a PNG file is created. Otherwise, the image is sent directly to the browser. This latter method is used in most of the examples in this section. boolean imagepolygon(integer image, array points, integer number, integer color) The imagepolygon function behaves identically to the imagefilledpolygon function with the exception that the polygon is not filled. The points argument is an array of integers, two for each point of the polygon. A line will be drawn from each point in succession and from the last point to the first point. imagepolygon array imagepsbbox(string text, integer font_identifier, integer size, integer spacing, integer letting, double angle) The imagepsbbox function returns an array containing a pair of coordinates that specify a bounding box that would surround a theoretical string of text. The first two numbers are the x and y values of the lower-left corner. The second pair of numbers specify the upperright corner. The font_identifier is an integer returned by imagepsloadfont. The size argument is in pixels. The spacing argument controls vertical spacing between lines of text. The letting argument controls horizontal spacing between characters. Both are expressed in units of 1/1000th of an em-square, and are added to the default spacing or leading for a font. They may be positive or negative. The angle argument specifies a number of degrees to rotate from normal left-to-right orientation. See imagepstext, below, for an example. integer imagepscopyfont(integer font_identifier) The imagepscopyfont function copies a font loaded with imagepsloadfont into another font identifier. This allows you to have an original version of the font in memory and another copy you've stretched or slanted. <? /* ** Draw text using a Postscript font ** Draw normal, stretched and slanted */ //set parameters for text$font_file = "ComputerModern-Roman";
$size = 20;$angle = 0;
$text = "PHP";$antialias_steps = 16;
$spacing = 0;$letting = 0;
//create red square
$image = imagecreate(300, 300);$colorRed = imagecolorallocate($image, 0xFF, 0x00, 0x00);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 10, 10, $colorRed); //Load font if(!($myFont = imagepsloadfont($font_file))) { print("Unable to load font!"); exit(); } //make extended font$myFontExtended = imagepscopyfont($myFont); imagepsextendfont($myFont, 1.5);
//make slanted font
$myFontSlanted = imagepscopyfont($myFont);
imagepsslantfont($myFont, 1.5); //write normal text imagepstext($image, $text,$myFont, $size,$colorBlack, $colorRed, 0, 0,$spacing, $letting,$angle, $antialias_steps); //write extended text imagepstext($image, $text,$myFont, $size,$colorBlack, $colorRed, 0,$size, $spacing,$letting,
$angle,$antialias_steps);
//write slanted text
imagepstext($image,$text, $myFont,$size,
$colorBlack,$colorRed,
0, $size*2,$spacing, $letting,$angle, $antialias_steps); //unload fonts imagepsfreefont($myFont);
imagepsfreefont($myFontExtended); imagepsfreefont($myFontSlanted);
//send image
imagejpeg($image); ?> imagepsencodefont(string filename) Use imagepsencodefont to change the encoding vector used to match ASCII characters to PostScript font images. By default, PostScript fonts only have characters for the first 127 ASCII values. See imagepstext, below, for an example. imagepsextendfont(integer font_identifier, double extension_factor) The imagepsextendfont function stretches or compresses a PostScript font. The normal width of the font will be multiplied by the extension_factor. See imagepscopyfont for an example. Multiple calls to this function are not cumulative, they just change the extension. If you want to set the font back to normal width, use a factor of one. imagepsfreefont(integer font_identifier) The imagepsfreefont function removes a PostScript font from memory. Generally you do not need to do this. PHP will unload fonts when your script ends. See imagepstext, below, for an example. imagepsslantfont(integer font_identifier, double slant_factor) Use imagepsslantfont to pitch the font forward or backwards. Sometimes this is referred to as italics. The font_identifier is an integer returned by imagepsloadfont. The slant_factor operates similarly to the extension_factor in the imagepsextendfont function. Values greater than one will cause the top of the font to pitch to the right. Values less than one will cause the top of the font to pitch to the left. integer imagepsloadfont(string filename) Use imagepsloadfont to load a PostScript font. A font identifier will be returned for use in with the other PostScript functions. If the load fails, FALSE is returned. See imagepstext, below, for an example. array imagepstext(integer image, string text, integer font_identifer, integer size, integer foreground, integer background, integer x, integer y, integer spacing, integer letting, double angle, integer antialias_steps) The imagepstext function draws a string of text into an image using a PostScript font. The image argument is an integer as returned by imagecreate or imagecreatefrompng. The font_identifier argument is a value returned by the imagepsloadfont function. The size argument specifies the height in number of pixels. The foreground and background arguments are color identifiers. The x and y arguments specify the bottomleft corner from where to begin drawing. The spacing argument controls vertical spacing between lines of text. The letting argument controls horizontal spacing between characters. Both are expressed in units of 1/1000th of an em-square and are added to the default spacing or leading for a font. They may be positive or negative. The angle argument specifies a num- ber of degrees to rotate from normal left-to-right orientation. The antialias_steps argument specifies how many colors to use when antialiasing, or smoothing. Two values are valid: 4 and 16. The last four arguments are optional. The returned array contains two pairs of coordinates specifying the lower-left corner and upper-right corner of the bounding box, respectively. <? /* ** Draw text using a Postscript font ** Make the image the correct size */ //set parameters for text$font_file = "ComputerModern-Roman";
$size = 20;$angle = 0;
$text = "PHP";$antialias_steps = 16;
$spacing = 0;$letting = 0;
if(!($myFont = imagepsloadfont($font_file)))
{
exit();
}
//set encoding
imagepsencodefont("IsoLatin1.enc");
//get bounding box
$Box = imagepsbbox($text, $myFont,$size, $spacing,$letting,
$angle); //create an image with ten extra pixels$image = imagecreate($Box[1]+10,$Box[3]+10);
$colorRed = imagecolorallocate($image, 0, 0, 0);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 10, 10,$colorRed);
//write the text
imagepstext($image,$text, $myFont,$size,
$colorBlack,$colorRed,
0, 0, $spacing,$letting,
$angle,$antialias_steps);
imagepsfreefont($myFont); //send image header("Content-type: image/jpeg"); imagejpeg($image);
?>

imagerectangle(integer image, integer top_left_x, integer top_left_y, integer bottom_right_x, integer bottom_right_y, integer color)
The imagerectangle function draws a rectangle based on the top-left and bottom-right corners. The inside of the rectangle will not be filled as it is with the imagefilledrectangle function.

<?
/*
** Draw a hollow black rectangle
*/
//create yellow square
$image = imagecreate(200,200);$colorYellow = imagecolorallocate($image, 255, 255, 0);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $colorYellow); //draw a black rectangle imagerectangle($image, 10, 10, 150, 150, $colorBlack); //send image header("Content-type: image/jpeg"); imagejpeg($image);
?>

boolean imagesetpixel(integer image, integer x, integer y, integer color)
The imagesetpixel function sets a single pixel to the specified color.

<?
/*
** Draw 100 black dots
*/
//create yellow square
$image = imagecreate(100, 100);$colorYellow = imagecolorallocate($image, 255, 255, 0);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $colorYellow); //draw 100 random black dots srand(time()); for($i=0; $i 100;$i++)
{
imagesetpixel($image, rand(0, 99), rand(0, 99),$colorBlack);
}
//send image
imagejpeg($image); ?> boolean imagestring(integer image, integer font, integer x, integer y, string text, integer color) The imagestring function draws the given text at the specified point. The top-left part of the string will be at the specified point. The font argument may be a built-in font or one loaded by imageloadfont. imagestring boolean imagestringup(integer image, integer font, integer x, integer y, string text, integer color) The imagestringup function draws a string oriented vertically instead of horizontally. Otherwise it works identically to imagestring. <? /* ** Write "Hello, World!" vertically */ //create blue square$image = imagecreate(50, 200);
$colorBlue = imagecolorallocate($image, 128, 128, 255);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0,$colorYellow);
//draw some text with loaded font
imagestringup($image, 4, 10, 150, "Hello World!",$colorBlack);
//send image
imagejpeg($image); ?> integer imagesx(integer image) The imagesx function returns the width in pixels of the specified image. <? /* ** Put a rectangle in the center of any image */ //attempt to open image, suppress error messages if(!($image = @imagecreatefromjpeg("php_lang.jpg")))
{
//error, so create an error image and exit
$image = imagecreate(200,200);$colorWhite = imagecolorallocate($image, 255, 255, 255);$colorBlack = imagecolorallocate($image, 0, 0, 0); imagefill($image, 0, 0, $colorWhite); imagestring($image, 4, 10, 10, "Couldn't load image!",
$colorBlack); header("Content-type: image/jpeg"); imagejpeg($image);
}
//find center
$centerX = intval(imagesx($image)/2);
$centerY = intval(imagesy($image)/2);
$colorGreen = imagecolorallocate($image, 0, 255, 0);
//draw a green rectangle in center
imagefilledrectangle($image, ($centerX-15), ($centerY-15), ($centerX+15), ($centerY+15),$colorGreen);
//send image
imagejpeg($image); ?> integer imagesy(integer image) The imagesy function returns the height in pixels of the specified image. array imagettfbbox(integer point_size, integer angle, string font, string text) The imagettfbbox function returns an array of points that describe a bounding box around text to be drawn by the imagettftext function. The points are relative to the leftmost point on the baseline. The array elements correspond to the lower-left, lowerright, upper-right, and upper-left corners, in that order. This function may not be available, depending on the libraries available when PHP was compiled. boolean imagettftext(integer image, integer point_size, integer angle, integer x, integer y, integer color, string font, string text) The imagettftext function uses a TrueType font to draw a string of text. The x and y arguments refer to the leftmost position of the baseline. The text will radiate from that point at the given angle, which should be from 0 to 360. An angle of zero represents normal right-to-left text. The font argument is the full path to a .ttf file. This function may not be available, depending on the libraries available when PHP was compiled. <? /* ** Draw text using a TrueType font ** Also, draw a box around the text. */ //set parameters for text$size = 40;
$angle = 30;$startX = 50;
$startY = 100; //create red square$image = imagecreate(200, 200);
$colorRed = imagecolorallocate($image, 255, 0, 0);
$colorBlack = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 10, 10,$colorRed);
//get bounding box
$Box = imagettfbbox($size, $angle, "comic.ttf", "PHP"); //move bounding box to starting point (100,100) for($index = 0; $index count($Box); $index += 2) {$Box[$index] +=$startX;
$Box[$index+1] += $startY; } //draw bounding box imagepolygon($image, $Box, count($Box)/2, $colorBlack); //write the text imagettftext($image, $size,$angle,
$startX,$startY, $colorBlack, "comic.ttf", "PHP"); //send image header("Content-type: image/jpeg"); imagejpeg($image);
?>

boolean imagewbmp(integer image, string filename)
The imagewbmp function either sends an image to the browser or writes it to a file. If a filename is provided, a WAP (Wireless Application Protocol) bitmap file is created. Otherwise, the image is sent directly to the browser. This function is similar to imagepng and imagejpeg.