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手册

猜你喜欢

转载自blog.51cto.com/11016194/2327966