PHP Image Processing: image background and canvas operations ,. The background and canvas operations of PHP image processing, such as verification codes or statistical icons generated based on dynamic data, and some operations on the GD library described earlier are dynamic image rendering. Image background and canvas operations in PHP image processing,
Such as a verification code or a statistical icon generated based on dynamic data, and some GD library operations described earlier are dynamic rendering images. In web development, images already exist on the server are often processed. For example, you can scale, add watermarks, crop, flip, and rotate images as needed. In web applications, the commonly used image formats include GIF, JPEG, and PNG. of course, the GD library can also process images in other formats, but they are rarely used. Therefore, when installing the GD library, you must install at least one of the GIF, JPEG, or PNG formats.
In canvas management, you can use the imagecreate () and imageCreateTrueColor () functions to create canvas resources. If you need to process your existing image, you only need to use the image as the canvas resource, that is, to create the image background. You can use the following functions to open an existing GIF, JPEG, and PNG image in a server or network file and return an image identifier, represents the image obtained from the given file name as the background resource for operations. Their prototype is as follows. when they fail, an empty string is returned and an error message is output.
The code is as follows:
Resource imagecreatefromjpeg (string $ filename) // create an image from a JPEG file or URL
Resource imagecreatefrompng (string $ filename) // create an image from a PNG file or URL
Resource imagecreatefromgif (string $ filename) // create an image from a GIF file or URL
No matter which function is used to create image resources, you must use the imagedestroy () function to destroy them after use. There is also a problem with the corresponding image format. The 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 images to browsers or files in PNG format. Of course, it is best to open an image in which format, and save it as the corresponding image format. To do this, we also need to first understand the getimagesize () function and obtain the type, width, and height of the image by using the image name. The function is prototype as follows:
The code is as follows:
Array getimagesize (string filename [, array & imageinfo]) // gets the image size and type.
If you cannot access the image specified by filename or it is not a valid image, this function returns FALSE and generates an E_WARNING error. If no error occurs, getimagesize () returns an array with four units. index 0 contains the pixel value of the image width, and index 1 contains the index value of the image height, index 2 is the mark of the image type: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, etc., index 3 is a text string, the content is "height =" yyy "width =" xxx ", which can be used directly for marking. As follows:
The code is as follows:
<? Php
List ($ width, $ height, $ type, $ attr) = getimagesize ("image/brophp.jpg ");
Echo "";
?>
The following example declares an image () function. you can open images in any format in GIF, JPG, and PNG, and add a string to the image, save it to the original format (text watermark ). In future development, if you need the same operation (in which format the image is opened and saved as a file in the corresponding format), you can participate in the mode of this example. the code is as follows:
The code is as follows:
<? Php
// Draw a string (also a text watermark) between images of different formats)
Function image ($ filename, $ string ){
// Obtain the image attributes, the first width, the second height, type 1 => gif, 2 => jpeg, 3 => png
List ($ width, $ height, $ type) = getimagesize ($ filename );
// Types of images that can be processed
$ Types = array (1 => "gif", 2 => "jpeg", 3 => "png ",);
// Combine the image types to create the GD library function for image resources in the corresponding image format
$ Createfrom = "imagecreatefrom". $ types [$ type];
// Use the variable function to create image resources.
$ Image = $ createfrom ($ filename );
// Set the x axis coordinate position of the Center font
$ X = ($ width-imagefontwidth (5) * strlen ($ string)/2;
// Set the coordinate position of the y axis of the Center font
$ Y = ($ height-imagefontheight (5)/2;
// Set the font color to red.
$ Textcolor = imagecolorallocate ($ images, 255, 0, 0 );
// Draw a specified string to the image
Imagestring ($ image, 5, $ x, $ y, $ string, $ textcolor );
// Saves the image functions in the corresponding format by combining the image types
$ Output = "image". $ types [$ type];
// Use the variable function to save images in the corresponding format
$ Output ($ image, $ filename );
Imagedestroy ($ image );
}
Image ("brophp.gif", "GIF ");
Image ("brophp.jpg", "JPEG ");
Image ("brophp.png", "PNG ");
?>
Token, such as a verification code or a statistical icon generated based on dynamic data, and some GD library operations described earlier are dynamic rendering images. And in...