1. 程式人生 > >php GD庫生成分享海報

php GD庫生成分享海報

php生成影象,效果如圖
php GD庫生成分享海報
程式碼如下

<?php
/**
 * 圖片處理
 * @author Administrator
 *
 */
class ImgGD{
    /**
     * 建立分享圖片
     */
    public function createShareImg(){
        // 1 獲取背景圖尺寸
        list($bg_w,$bg_h) = getimagesize("../img/bg.jpg");
        // 2 建立畫圖
        $img = @imagecreatetruecolor($bg_w,$bg_h);
        // 3 填充畫布背景顏色
        $img_bg_color = imagecolorallocate($img,255,255,255);
        imagefill($img,0,0,$img_bg_color);
        // 4 將背景圖填充到畫布
        $bg_img = $this->getImgReource("../img/bg.jpg");
        imagecopyresized($img,$bg_img,0,0,0,0,$bg_w,$bg_h,$bg_w,$bg_h);
        // 5 填空使用者二維碼
        $qrcode = $this->getImgReource("../img/qrcode.png");
        // 計算使用者二維碼所在x軸位置
        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 填充使用者資訊
        $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);
        // 填空使用者名稱
        $user_name = "王小明";
        $font_color = ImageColorAllocate($img,253,254,255); //字型顏色
        $font_ttf = "../ziti/HYTangMeiRenJ-2.ttf";
        imagettftext($img,23,0,90,50,$font_color,$font_ttf,$user_name);
        // 7 設定提示語
        $tip_text = "邀請你立即關注";
        imagettftext($img,17,0,90,80,$font_color,$font_ttf,$tip_text);
        // 8 輸出圖片
        header("Content-type: image/png");
        imagepng($img);
        // 9 釋放空間
        imagedestroy($img);
        imagedestroy($bg_img);
        imagedestroy($qrcode);
        imagedestory($user_img);
    }
    /**
     * 獲取影象檔案資源
     * @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;
    }
    /**
     * 縮放圖片
     * @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){
        //給影象加大1畫素的邊框
        $new_th_h = $th_h + 4;
        $new_th_w = $th_w + 4;
        // 獲取大圖資源及影象大小
        list($max_w,$max_h) = getimagesize($img);
        if($max_w < $th_w || $max_h < $th_h) return false;
        $max_img = $this->getImgReource($img);
        //新建真色彩畫布

        $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);
        //輸出影象到檔案
        $min_img_path = $file . 'thunm_'.time().'.png';
        imagepng($min_img,$min_img_path);
        if(!is_file($min_img_path)){
            return false;
        }
        //釋放空間
        imagedestroy($max_img);
        imagedestroy($min_img);
        return $min_img_path;
    }

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

使用到的函式:
使用到的GD函式:

getimagesize():該函式獲取影象大小

imagecreatetruecolor() : 建立真色彩影象   imagecolorallocate():使用這個函式建立的影象畫布圖片會丟失色彩 

 imagecolorallocate() : 該函式用來設定顏色

imagecopyresized() : 拷貝影象調整影象大小

imagettftext() : 設定文字到影象上

imagepng() : 輸出影象為png格式  imagejpeg() : 輸出為jpg格式  imagegif() : 輸出為gif格式

如果直接輸出影象要新增一句程式碼: header("Content-type: image/png");

imagedestroy() : 該函式銷燬影象釋放記憶體空間資源

imagecreatefromjpeg() : 該函式獲取jpg格式影象對應的影象資源

imagecreatefrompng() : 該函式獲取png格式影象對應的影象資源

imagecreatefromgif() : 該函式獲取gif格式影象對應的影象資源

具體函式的使用可以參考php手冊