Introduction of imagecreate and imagedestroy Functions in PHP Image Processing

  • 2021-08-05 09:02:01
  • OfStack

When using PHP's GD library to process images, canvas must be managed. Creating canvas is to open up a storage area in memory, and all operations on images in PHP are based on this cloth, which is an image resource. In PHP, you can use the imagecrete () and imageCreateTrueColor () functions to create the specified canvas. These two functions have the function of 1, which is to create a canvas of a specified size. Their prototype is as follows:


   resource imagecreate(int $x_size,int $y_size)              // New 1 Palette-based images
   resource imagecreatetruecolor(int $x_size,int $y_size)             // New 1 True color image

Although both functions can create a new canvas, the total number of colors each can hold is different. The imageCreate () function creates an image based on a normal palette and typically supports 256 colors. The imageCreateTrueColor () function can create a true color image, but it cannot be used in the GIF file format. When the canvas is created, it returns an image identifier representing a blank image reference handle with a width of $x_size and a height of $y_size. In the subsequent drawing process, you need to use the handle of this resource type. For example, you can get the size of an image by calling imagesx () and imagesy (). The code looks like this:

<?php
$img = imagecreatetruecolor(300,200);// Create 1 A 300*200 Canvas of
echo imagesx($img);// Output canvas width 300
echo imagesy($img);// Output canvas height 200
?>

In addition, if the canvas reference handle is no longer used, 1 must destroy this resource and free up memory and storage units for the image. The process of destroying canvas is very simple, which can be realized by calling imagedestroy () function. Its syntax format is as follows:

   bool imagedestroy(resource $image)                  // Destruction 1 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.


Related articles: