PHP Image Processing: Image background and canvas operations,

Source: Internet
Author: User
Tags image identifier

PHP Image Processing: Image background and canvas operations,

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.

Copy codeThe 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:

Copy codeThe 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 directly used for marking. As follows:

Copy codeThe 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:

Copy codeThe 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 ");
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.