Home >Backend Development >PHP Tutorial >Introduction to imagecreate and imagedestroy functions in PHP image processing
When using PHP's GD library to process images, the canvas must be managed. Creating a canvas is to open up a storage area in the memory. All future operations on images in PHP will be based on this canvas, which is an image resource. In PHP, you can use the imagecrete() and imageCreateTrueColor() functions to create a specified canvas. The functions of these two functions are consistent. They both create a canvas of a specified size. Their prototypes are as follows:
resource imagecreate(int $x_size,int $y_size) //新建一个基于调色板的图像 resource imagecreatetruecolor(int $x_size,int $y_size) //新建一个真彩色图像
Although both functions can create a new canvas, each can accommodate The total number of colors is different. The imageCreate() function can create an image based on a common palette, usually supporting 256 colors. The imageCreateTrueColor() function can create a true color image, but this function cannot be used in the GIF file format. When the canvas is created, an image identifier is returned, representing a blank image refnum with a width of $x_size and a height of $y_size. In subsequent drawing processes, you need to use the handle of this resource type. For example, you can get the size of an image by calling the imagesx() and imagesy() functions. The code is as follows:
<?php $img = imagecreatetruecolor(300,200);//创建一个300*200的画布 echo imagesx($img);//输出画布宽度300 echo imagesy($img);//输出画布高度200 ?>
In addition, if the reference handle of the canvas is no longer used, this resource must be destroyed to release the memory and the storage unit of the image. The canvas destruction process is very simple and can be achieved by calling the imagedestroy() function. The syntax format is as follows:
bool imagedestroy(resource $image) //销毁一图像
If the method is called successfully, the memory associated with the parameter $image will be released. The parameter $image is the image identifier returned by the image creation function.
For more articles related to the imagecreate and imagedestroy functions of PHP image processing, please pay attention to the PHP Chinese website!