When working with images using the GD Library of PHP, you must manage the canvas. Creating a canvas is in memory to open a piece of storage area, later in PHP in the image of all operations are based on this cloth processing, Parliament is an image resource. In PHP, you can use the Imagecrete () and Imagecreatetruecolor () two functions to create the specified canvas. The function of the two functions is consistent, is to create a specified size of the canvas, their prototype is as follows:
Copy Code code as follows:
Resource imagecreate (int $x _size,int $y _size)//Create a palette-based image
Resource Imagecreatetruecolor (int $x _size,int $y _size)//Create a true color image
Although both functions can create a new canvas, the total number of colors that can be accommodated is different. The Imagecreate () function can create an image based on a normal palette, which typically supports 256 colors. The Imagecreatetruecolor () function can create a true color image, but the function cannot be used in the GIF file format. When the canvas is created, an image identifier is returned, representing a blank image reference handle with a width of $x_size and a height of $y_size. You will need to use the handle of this resource type during the subsequent drawing process. For example, you can get the size of an image by calling Imagesx () and Imagesy () two functions. The code looks like this:
Copy Code code as follows:
<?php
$img = Imagecreatetruecolor (300,200);//Create a 300*200 canvas
echo Imagesx ($img);//output canvas width 300
echo Imagesy ($img);//output Canvas height 200
?>
In addition, if the reference handle of the canvas is no longer in use, be sure to destroy the resource, freeing memory and the image's storage unit. The process of destroying the canvas is very simple, and the Imagedestroy () function can be implemented. The syntax format looks like this:
Copy Code code as follows:
BOOL Imagedestroy (Resource $image)//Destroy an image
If the method call succeeds, the memory associated with the parameter $image is freed. Where the parameter $image is the image identifier returned by the image creation function.