Home  >  Article  >  Backend Development  >  PHP image processing image background and canvas operations, _PHP tutorial

PHP image processing image background and canvas operations, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:47945browse

PHP image processing image background and canvas operations,

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.

Copy code The code is 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 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:

Copy code The code is as follows:

array getimagesize(string filename[,array &imageinfo]) //Get the size and type of the image

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:

Copy code The code is as follows:

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

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:

Copy code The code is as follows:

//Draw a string (also a text watermark) in the middle of pictures in different formats
Function image($filename,$string){
​​​​ //Get the attributes of the image, the first width, the second height, type 1=>gif, 2=>jpeg, 3=>png
List($width,$height,$type) = getimagesize($filename);
             // Image types that can be processed
          $types = array(1=>"gif",2=>"jpeg",3=>"png",);
​​​​ //By combining image types, you can create corresponding image formats and create GD library functions for image resources
         $createfrom = "imagecreatefrom".$types[$type];
​​​​ //Use the "variable function" to create the corresponding function to create the image resource
         $image = $createfrom($filename);
​​​​ //Set the X-axis coordinate position of the centered font
          $x = ($width-imagefontwidth(5)*strlen($string))/2;
​​​​ //Set the Y-axis coordinate position of the centered font
         $y = ($height-imagefontheight(5))/2;
​​​​ //Set the font color to red
$textcolor = imagecolorallocate($image, 255, 0, 0);
​​​​ //Draw a specified string to the picture
Imagestring($image, 5, $x, $y, $string, $textcolor);
​​​​ //Use the picture type to combine and save the picture function of the corresponding format
         $output = "image".$types[$type];
             //Use variable functions to save pictures in the corresponding format
         $output($image,$filename);
Imagedestroy($image);
}
Image("brophp.gif","GIF");
Image("brophp.jpg", "JPEG");
Image("brophp.png", "PNG");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914049.htmlTechArticlePHP image processing image background, canvas operations, like verification code or generating statistical icons based on dynamic data, as well as the previous introduction Some of the GD library operations are dynamically drawn images. And in...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn