Home > Article > Backend Development > PHP image processing image background and canvas operations, _PHP tutorial
For example, verification codes or generating statistical icons based on dynamic data, as well as some GD library operations introduced earlier, are all dynamically drawn images. In web development, images that already exist in the server are often processed. For example, you can perform image modification operations such as scaling, watermarking, cropping, flipping, and rotating images according to certain needs. In web applications, the commonly used image formats are one or more of GIF, JPEG and PNG. Of course, the GD library can also handle images in other formats, but they are rarely used. Therefore, when installing the GD library, install at least one of the three formats: GIF, JPEG or PNG.
In the canvas management introduced earlier, use the two functions imagecreate() and imageCreateTrueColor() to create canvas resources. But if you need to process an existing picture, you only need to use this picture as a canvas resource, which is what we call creating a picture background. You can use several functions introduced below to open GIF, JPEG and PNG images that already exist in the server or network file, and return an image identifier that represents the image obtained from the given file name as a background resource for the operation. Their prototypes are as follows. They all return an empty string and output an error message when they fail.
No matter which function is used to create the image resource, it needs to be destroyed using the imagedestroy() function after use. Then there is the issue of image format correspondence. Image resources opened in any way can be saved in the same format. For example, for image resources created using the imagecreatefromjpeg() function, you can use the imagepng() function to output the image to a browser or file in PNG format. Of course, it is best to save the image in the corresponding format according to which format you open. If we want to do this, we also need to know the getimagesize() function first. We can get the type, width and height of the image through the image name. The prototype of this function looks like this:
If the image specified by filename cannot be accessed or is not a valid image, this function will return FALSE and generate an E_WARNING level error. If no error occurs, getimagesize() returns an array with four cells, index 0 contains the pixel value of the image width, index 1 contains the index value of the image height, and index 2 is a tag of the image type: 1=GIF, 2=JPG, 3=PNG, 4=SWF, etc. Index 3 is a text string with the content "height="yyy" width="xxx"", which can be used directly for the tag. As shown below:
The following example declares an image() function, which can open pictures in any format among GIF, JPG and PNG, add a string in the middle of the picture, and save it in the original format (text watermark). In future development, if you need the same operation (which format of the picture is opened, it is also saved as a file in the corresponding format), you can participate in the mode of this example, the code is as follows: