php GD library generate share Poster

php generates an image, as shown in the figure

The code is as follows

<?php
/**
 * Picture processing
 * @author Administrator
 *
 */
class ImgGD{
    /**
     * Create a shared picture
     */
    public function createShareImg(){
        // 1 get background size
        list($bg_w,$bg_h) = getimagesize("../img/bg.jpg");
        // 2 create drawing
        $img = @imagecreatetruecolor($bg_w,$bg_h);
        // 3 fill canvas background color
        $img_bg_color = imagecolorallocate($img,255,255,255);
        imagefill($img,0,0,$img_bg_color);
        // 4 fill the background image into the canvas
        $bg_img = $this->getImgReource("../img/bg.jpg");
        imagecopyresized($img,$bg_img,0,0,0,0,$bg_w,$bg_h,$bg_w,$bg_h);
        // 5 fill in the blank user QR code
        $qrcode = $this->getImgReource("../img/qrcode.png");
        // Calculate the x-axis position of user QR code
        list($qr_w,$qr_h) = getimagesize("../img/qrcode.png");
        $qrcode_des_x = ceil(($bg_w - $qr_w)/2);
        imagecopyresized($img,$qrcode,$qrcode_des_x,513,0,0,$qr_w,$qr_h,$qr_w,$qr_h);
        // 6 fill in user information
        $user_img_path = $this->thumbImg("../img/logo.png");
        $user_img = $this->getImgReource($user_img_path);
        list($user_w,$user_h) = getimagesize($user_img_path);

        imagecopyresized($img,$user_img,13,20,0,0,$user_w,$user_h,$user_w,$user_h);
        // Fill in the blank user name
        $user_name = "Xiao Ming Wang";
        $font_color = ImageColorAllocate($img,253,254,255); //Font color
        $font_ttf = "../ziti/HYTangMeiRenJ-2.ttf";
        imagettftext($img,23,0,90,50,$font_color,$font_ttf,$user_name);
        // 7 set prompt
        $tip_text = "Invite you to pay attention now";
        imagettftext($img,17,0,90,80,$font_color,$font_ttf,$tip_text);
        // 8 output picture
        header("Content-type: image/png");
        imagepng($img);
        // 9 free up space
        imagedestroy($img);
        imagedestroy($bg_img);
        imagedestroy($qrcode);
        imagedestory($user_img);
    }
    /**
     * Get image file resources
     * @param string $file
     * @return resource
     */
    protected function getImgReource($file){
        $file_ext = pathinfo($file,PATHINFO_EXTENSION);
        switch ($file_ext){
            case 'jpg':
            case 'jpeg':
                $img_reources = @imagecreatefromjpeg($file);
                break;
            case 'png':
                $img_reources = @imagecreatefrompng($file);
                break;
            case 'gif':
                $img_reources = @imagecreatefromgif($file);
                break;
        }
        return  $img_reources;
    }
    /**
     * Zoom picture
     * @param string $img 
     * @param string $file
     * @param number $th_w
     * @param number $th_h
     * @return boolean|string;
     */
    protected function thumbImg($img,$file='./',$th_w=62,$th_h=62){
        //Add 1 pixel border to the image
        $new_th_h = $th_h + 4;
        $new_th_w = $th_w + 4;
        // Obtain large image resources and image size
        list($max_w,$max_h) = getimagesize($img);
        if($max_w < $th_w || $max_h < $th_h) return false;
        $max_img = $this->getImgReource($img);
        //New true color canvas

        $min_img = @imagecreatetruecolor($new_th_w,$new_th_h);
        $bg_color = ImageColorAllocate($min_img,255,255,255);
        imagefill($min_img,0,0,$bg_color);
//         imagesavealpha($min_img,true);
        imagecolortransparent($min_img,$bg_color); 
        imagecopyresampled($min_img,$max_img,2,2,0,0,$th_w,$th_h,$max_w,$max_h);
        //Output image to file
        $min_img_path = $file . 'thunm_'.time().'.png';
        imagepng($min_img,$min_img_path);
        if(!is_file($min_img_path)){
            return false;
        }
        //Release space
        imagedestroy($max_img);
        imagedestroy($min_img);
        return $min_img_path;
    }

}
//call
$gd_img = new ImgGD();
$gd_img->createShareImg();

Functions used:
GD function used:

getimagesize(): this function gets the image size

imagecreatetruecolor(): create a true color image; imagecolorallocate(): the image canvas image created with this function will lose color

imagecolorallocate(): this function is used to set the color

Imagecopied(): copy image to resize image

imagettftext(): set text to image

imagepng(): output image in png format ﹣ imagejpeg(): output in jpg format ﹣ imagegif(): output in gif format

If you output the image directly, you need to add a line of code: header ("content type: image / PNG");

imagedestroy(): this function destroys an image to free up memory resources

imagecreatefromjpeg(): this function obtains the image resources corresponding to the jpg format image

imagecreatefrompng(): this function obtains the image resource corresponding to the png format image

imagecreatefromgif(): this function obtains the image resources corresponding to the gif format image

For specific functions, please refer to the php manual

Keywords: PHP QRCode

Added by Prismatic on Mon, 09 Dec 2019 11:23:10 +0200