PHP picture processing picture background, canvas operation _php Skills

Source: Internet
Author: User
Tags image identifier

such as validation code or the generation of statistical icons based on Dynamic Data, as well as some of the GD library operations described earlier, belong to the dynamic drawing image. In web development, however, you will often be able to handle images that already exist on the server. For example, according to some requirements of the image scaling, watermark, cropping, flipping and rotation, such as the operation of the map. In Web applications, frequently used picture formats include one or more of the GIF, JPEG, and PNG, and of course the GD Library can handle pictures in other formats, but it's rarely used. So when you install the GD library, install at least one of the three formats for GIF, JPEG, or PNG.

In the canvas management described earlier, use the Imagecreate () and Imagecreatetruecolor () two functions to create canvas resources. But if you need to deal with the image you already have, just use the picture as a canvas resource, which is what we call creating a picture background. You can open GIF, JPEG, and PNG images that already exist in a server or network file by using several of the functions described below, returning an image identifier that represents an image taken from a given file name as the background resource for the operation. Their prototypes are as follows, they return an empty string when they fail, and output an error message.

Copy Code code as follows:

Resource Imagecreatefromjpeg (string $filename)//Create a new image from a JPEG file or URL
Resource Imagecreatefrompng (string $filename)//Create a new image from a PNG file or URL
Resource Imagecreatefromgif (string $filename)//Create a new image from a GIF file or URL

No matter which function you use to create an image resource, you need to use the Imagedestroy () function to destroy it. Then there is the picture format corresponding to the problem, any way to open the picture resources can be saved in the same format. For example, for a picture resource 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 open a picture of which format, it is saved to the corresponding picture format. If you want to do this, we also need to first know the getimagesize () function, through the name of the picture can get the type, width and height of the picture. The prototype of the function looks like this:

Copy Code code as follows:

Array getimagesize (string Filename[,array &imageinfo])//Get picture size and type

If the image specified by filename cannot be accessed or is not a valid image, the function returns false and generates a e_warning-level error. If not, getimagesize () returns an array of four cells, index 0 contains the pixel value of the image width, index 1 contains the index value of the image height, index 2 is the image type tag: 1=gif,2=jpg,3=png,4=swf, and Index 3 is a text string, The content is "height=" yyy "width=" "xxx", which can be used directly for tag. As shown below:

Copy Code code as follows:

<?php
List ($width, $height, $type, $attr) = getimagesize ("image/brophp.jpg");
echo " ";
?>

The following example declares an image () function that opens a picture in any format in GIF, JPG, and PNG, and then after adding a string in the middle of the picture, it is saved in the original format (text watermark). In future development, if you need the same action (which format of the picture is open, also saved in the corresponding format of the file), you can participate in this example pattern, the code looks like this:

Copy Code code as follows:

<?php
Draw a string (also a text watermark) to the middle of a picture in a different format
function image ($filename, $string) {
Gets the properties of the picture, the first width, the second height, the type 1=>gif,2=>jpeg,3=>png
List ($width, $height, $type) = getimagesize ($filename);
Types of pictures you can work with
$types = Array (1=> "GIF",2=> "JPEG",3=> "PNG");
Through the combination of image types, you can create a corresponding picture format, create picture Resources GD Library function
$createfrom = "Imagecreatefrom". $types [$type];
By "variable function" to play the corresponding function to create the resources of the picture
$image = $createfrom ($filename);
Set the x-axis position of the centered font
$x = ($width-imagefontwidth (5) *strlen ($string))/2;
Set the y-coordinate position of the centered font
$y = ($height-imagefontheight (5))/2;
Set the color of the font to red
$textcolor = Imagecolorallocate ($image, 255, 0, 0);
Draw a specified string to the picture
Imagestring ($image, 5, $x, $y, $string, $textcolor);
To combine the image function of the corresponding format by a picture type
$output = "image". $types [$type];
To save a picture of a corresponding format by using a variable function
$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.