PHP image processing picture background, canvas operation, _php tutorial

Source: Internet
Author: User
Tags image identifier

PHP image processing picture background, canvas operation,


Like verification code or generate statistical icons based on Dynamic Data, as well as some of the GD library operations described above are dynamic drawing images. In web development, images that already exist on the server are often processed. For example, the image is scaled, watermarked, cropped, flipped, rotated, and manipulated according to some requirements. In the Web application, often used in the picture format has GIF, JPEG and PNG one or several, of course, the GD library can also handle other formats of pictures, but rarely used. So when installing the GD library, install at least one of the GIF, JPEG, or PNG three formats.

In the canvas management described earlier, use the Imagecreate () and Imagecreatetruecolor () two functions to create a canvas resource. But if you need to process the image you already have, just use this image 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 functions described below, returning an image identifier that represents the image taken from the given file name as the background resource for the operation. Their prototypes are as follows, and they will return an empty string when they fail, and output an error message.

Copy the 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

Regardless of which function you use to create an image resource, you will need to use the Imagedestroy () function to destroy it later. Then there is the picture format corresponding to the problem, any one 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 an image to a browser or file in PNG format. Of course, it is best to open the picture in which format, it is saved into the corresponding image format. If we want to do this, we also need to know the getimagesize () function, the image name can be obtained by the type, width and height of the picture. The prototype of the function is as follows:

Copy the Code code as follows:
Array getimagesize (string Filename[,array &imageinfo])//Get the size and type of picture

If the image specified by the filename cannot be accessed or is not a valid image, the function returns false and produces an e_warning-level error. If there is no error, 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, index 2 is the tag of the image type: 1=gif,2=jpg,3=png,4=swf, index 3 is a text string, The content is "height=" yyy "width=" xxx ", which can be used directly for tagging. As shown below:

Copy the Code code as follows:
<?php
List ($width, $height, $type, $attr) = getimagesize ("image/brophp.jpg");
echo "";
?>

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

Copy CodeThe code is 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 a 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",);
By the combination of picture type, you can create the corresponding image format, create the image resources of the GD library function
$createfrom = "Imagecreatefrom". $types [$type];
Use "variable function" to play the corresponding function to create a picture of the resources
$image = $createfrom ($filename);
Set the x-axis coordinate position of the center font
$x = ($width-imagefontwidth (5) *strlen ($string))/2;
Set the y-coordinate position of the center 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 save the image function of the corresponding format by the picture type
$output = "image". $types [$type];
Use variable functions to save images in the corresponding format
$output ($image, $filename);
Imagedestroy ($image);
}
Image ("Brophp.gif", "gif");
Image ("Brophp.jpg", "JPEG");
Image ("Brophp.png", "PNG");
?>

http://www.bkjia.com/PHPjc/914049.html www.bkjia.com true http://www.bkjia.com/PHPjc/914049.html techarticle PHP image processing image background, canvas operations, such as verification code or statistics based on dynamic Data icons, as well as some of the previous introduction of the GD Library operations are dynamic drawing images. And in ...

  • 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.