The function of generating pictures is often used in web applications. Creating pictures in PHP tutorials requires the support of the gd library to create graphics. With this graphics function, we can easily generate thumbnails. Verification code, watermarking pictures, etc.
In order to run the image creation function normally, you need to install the gd library in php. The method is as follows. In the win system, find php.ini
;The ";" in front of extension=php_gd2.dll is gone. Restart apache and it will be OK.
Let’s take a look at an example
PHP’s gd library can generate a variety of image files, such as gif, png, jpg, wbmp, xpm, etc. Let’s look at a file that generates a square.
$height = 300;
$width = 300;
//Create background image
$im = ImageCreateTrueColor($width, $height);
//Assign color
$white = ImageColorAllocate ($im, 255, 255, 255);
$blue = ImageColorAllocate ($im, 0, 0, 64);
//Draw color to image
ImageFill($im, 0, 0, $blue);
//Draw string: Hello,PHP
ImageString($im, 10, 100, 120, 'Hello,PHP', $white);
//Output image, define header
Header ('Content-type: image/png');
//Send image to browser
ImagePng($im);
//Clear resources
ImageDestroy($im);
?>
To view the results, just browse the php file. If you want an image, call
Example 2, using basic functions to create images imagecreate()
resource imagescreate(int x,inty)
imagedestroy is the memory space occupied by the image
int ingaedestroy(image)
imagecopy()
int imagecopy(dst_im,sr_im,int x,int y,int x,int y,)
1 2 3 4