PHP Graphics
From Pigbert Wiki
| Table of contents |
Include
- <image src='script.php' />
- <image src='script.php?<?php echo time(); ?>' />
- (each time the page is accessed, the src for the <img> tag will be different, thus forcing the browser to get the latest version of the image.
Creating/ReadIn Image
- image <= imagecreate(int width, int height) //256 color palette
- image <= imagecreatetruecolor(int width, int height)
- imagecreatefromjpeg/png/gif/wbmp/xbm/xpm/gd/gd2(string filename)
- imagecreatefromstring(string filename)
- imagecreatefromgd2part(string filename, int startX, int startY, int width, int height)
Save/Send Image
- To specify an output quality, but not save the image to disk, the filename should be set to NULL.
- To send an image, send the header header('Content-type: image/png(jpeg)') first
- imagejpeg($image[,string filename|NULL [,int(0-95|default=75) quality]])
- imagepng($image[, string filename])
- imagewbmp($image[, string filename [,$foregroundColour]])
Colour
- int <= imagecolorallocate(image,int red, int green, int blue)
- colours needed to be allocated to an image before it can be used.
- the first allocated colour is used as the background of the image.
- imagecolorclosest($image, int r, int g, int b)
- returns the colour identifier of the color closest to the RGB value passed in.
Retrieving Image Information
- imagesx($image) returns the width of the image
- imagesy($image) returns the height of the image
- getimagesize(imageFilePath) => resultArray:
- resultArray[0] = int width;
- resultArray[1] = int height;
- resultArray[2] = int type; (1=GIF, 2=JPG, 3=PNG, 4=SWF, 5=PSD, 6=BMP, ...
- resultArray[3] = string that can be used in <img> tag (e.g. width="413" height="271")
- resultArray[bits] = int colorDepth; (e.g. 8)
- resultArray[channels] = int numberOfColorChannel; (e.g. 1)
- resultArray[mime] = string mimeHeader; (e.g. image/jpeg)
Drawing Functions
- imagerectangle($image, int startX, int startY, int endX, int endY, $colour)
- imagesetpixel($image, int x, int y, $colour)
- imageline($image, int startX, int startY, int endX, int endY, $colour)
- imagearc($image, int centerX, int centerY, int width, int height, int start, int end, $colour)
- imageellipse($image, int centerX, int centerY, int width, int height, $colour)
- imagepolygon($image, array[] points, int numPoints, $colour)
- imagefilledarc($image, int centerX, int centerY, int width, int height, int start, int end, $colour, $int style)
- imagefilledellipse($image, int centerX, int centerY, int width, int height, $colour)
- imagefilledrectangle($image, int startX, int startY, int endX, int endY, $colour)
- imagefilledpolygon($image, array[] points, int numPoints, $colour)
buffering
$buffer = join('',file("art.jpg"));
header("Content-type: image/jpeg");
echo $buffer;
destroy
imagedestroy($image)
