Image Background and Canvas Operation for PHP Image Processing

  • 2021-08-05 09:04:10
  • OfStack

Such as verification code or generating statistical icons according to dynamic data, and some GD library operations introduced earlier are all dynamic rendering images. In web development, we often deal with the existing pictures in the server. For example, according to some requirements, the picture is scaled, watermarked, cropped, flipped and rotated. In web applications, one or more of GIF, JPEG and PNG are often used. Of course, GD library can also handle pictures in other formats, but they are rarely used. Therefore, when installing GD library, install at least one of GIF, JPEG or PNG formats.

In the canvas management described earlier, two functions, imagecreate () and imageCreateTrueColor (), are used to create canvas resources. However, if you need to process your existing picture, just use this 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 through the functions described below, and return an image identifier that represents the image obtained from a given file name as the background resource for the operation. Their prototypes are shown below, and they all return an empty string and output an error message on failure.


   resource  imagecreatefromjpeg(string $filename)           // From JPEG File or URL New 1 Image
   resource  imagecreatefrompng(string $filename)             // From PNG File or URL New 1 Image
   resource  imagecreatefromgif(string $filename)            // From GIF File or URL New 1 Image

No matter which function is used to create the image resource, it needs to be destroyed by using imagedestroy () function after it is used up. Then there is the problem of corresponding picture formats. Any picture resources opened in one way can be saved in the same format. For example, for an image 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 the picture in which format and save it into the corresponding picture format. If we want to do this 1, we need to know the getimagesize () function under 1 first, and we can get the type, width and height of the picture by the name of the picture. The prototype of this function is as follows:


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

If the image specified by filename cannot be accessed or is not a valid image, the function returns FALSE and generates an error of E_WARNING level. If there is no error, getimagesize () returns an array of 4 cells, Index 0 contains the pixel value of image width, Index 1 contains the index value of image height, Index 2 is the label of image type: 1=GIF, 2=JPG, 3=PNG, 4=SWF, etc. Index 3 is a text string with the content of "height=" yyy "width=" xxx ", which can be directly used < IMG > Mark. As shown below:


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

The following example declares an image () function, which can open pictures in any format in GIF, JPG and PNG, add a string in the middle of the pictures, and save them in the original format (text watermark). In future development, if you need the same operation (which format picture is opened and saved as a file in the corresponding format), you can participate in the mode of this example, and the code is as follows:


<?php
    // Draw in the middle of pictures in different formats 1 String (also text watermark)
    function image($filename,$string){
        // Gets the properties of the picture, the 1 Width, number 2 Height, type 1=>gif , 2=>jpeg,3=>png
        list($width,$height,$type) = getimagesize($filename);
        // Types of pictures that can be processed
        $types = array(1=>"gif",2=>"jpeg",3=>"png",);
        // By combining picture types, you can create corresponding picture formats and create picture resources GD Library function
        $createfrom = "imagecreatefrom".$types[$type];
        // Use "variable function" to play the corresponding function to create picture resources
        $image = $createfrom($filename);
        // Set the of the centered font X Axis coordinate position
        $x = ($width-imagefontwidth(5)*strlen($string))/2;
        // Set the of the centered font Y Axis coordinate position
        $y = ($height-imagefontheight(5))/2;
        // Set the font color to red
        $textcolor = imagecolorallocate($image, 255, 0, 0);
        // Draw to a picture 1 Specified strings
        imagestring($image, 5, $x, $y, $string, $textcolor);
        // Combine and save the picture functions of the corresponding format by the picture type
        $output = "image".$types[$type];
        // Save the pictures in the corresponding format through variable function
        $output($image,$filename);
        imagedestroy($image);
    }
    image("brophp.gif","GIF");
    image("brophp.jpg", "JPEG");
    image("brophp.png", "PNG");
?>


Related articles: