thinkphp生成海报 Grafika

使用的是Grafika组件,下载及手册

<?php
namespace app\api\controller;

use Grafika\Color;
use Grafika\Grafika;

class Poster
{
	/**
    * 生成海报
    * @param $openid      推荐者用户id
    * @param $qrcodeUrl   二位码将要跳转的链接
    */
	public function create($openid='', $qrcodeUrl='')
	{
		// 背景
        $backgroudImg = "./resource/background_default_567.png";

        // 头像
        $user = db('wx_user')->field('nickname,headimgurl')->where('openid', $openid)->find();
        $headimgCir = './resource/headimg/'.$openid.'_c.jpg';
        if (!file_exists($headimgCir)) {
        	
	        $headimg = './resource/headimg/'.$openid.'.jpg';
	        if (!file_exists($headimg)) {
	            // 较快速保存用户头像
	            $ch = curl_init($user['headimgurl']); 
		        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		        $content = curl_exec($ch);
		        file_put_contents($headimg, $content);
	        }

	        // 切圆
	        $circleHeadimgObj = circle($headimg);
	        imagepng($circleHeadimgObj, $headimgCir);
        }

        $filename = date('Y_m_d_H_i_s',time()).mt_rand(111,999); 
        //二维码图片
        $qrcodePath = "./resource/qrcode/".$filename.".png";
        qrcode($qrcodeUrl, 8, $filename);

        //生成图片
        $editor = Grafika::createEditor();
        $editor->open($bg, $backgroudImg);          //背景图
        $editor->open($qrcode, $qrcodePath);        //二维码图片
        $editor->open($head, $headimgCir);          //头像

        //设置图片大小
        $editor->resizeFit($qrcode, 210, 210);
        $editor->resizeFit($head, 98, 98);

        //绑定二维码
        $editor->blend($bg, $qrcode , 'normal', 0.9, 'bottom-right', -32, -21);
        $editor->blend($bg, $head , 'normal', 0.9, 'top-left', 30,30);
        
        // 默认地址不支持中文
        // $font = Grafika::fontsDir() . DIRECTORY_SEPARATOR . 'MSYH.TTC';
        $font = "/static/MSYH.TTF";
        $editor->text($bg ,$user['nickname'], 22, 32, 800, new Color("#FE8A24"), $font);

        $posterPath = "./resource/poster/".$filename.".png";
        $editor->save($bg, $posterPath);

        unlink($qrcodePath);      //删除二维码

        return $posterPath;
    }
}

切圆方法在common.php中,如下

/**
* GD库将正方形图片切为圆形
* @param 图片相对路径
*/
function circle($imgpath='')
{
    $ext = pathinfo($imgpath);

    switch ($ext['extension']) {
        case 'jpg':
            $src_img = imagecreatefromjpeg($imgpath);
            break;
        case 'png':
            $src_img = imagecreatefrompng($imgpath);
            break;
        default:
            $src_img = null;
            break;
    }

    $temp = getimagesize($imgpath);
    $w = $temp[0];
    $h = $temp[1];
    $w = min($w, $h);
    $h = $w;

    $img = imagecreatetruecolor($w, $h);
    imagesavealpha($img, true);
    $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
    imagefill($img, 0, 0, $bg);

    $r = $w / 2;      // 半径
    $y_x = $r;      // 圆心X坐标
    $y_y = $r;      // 圆心Y坐标

    for ($x=0; $x < $w; $x++) { 
        for ($y=0; $y < $h; $y++) { 
            $rgbColor = imagecolorat($src_img, $x, $y);
            if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                imagesetpixel($img, $x, $y, $rgbColor);
            }
        }
    }

    return $img;
}

生成二维码这里是用了phpqrcode,也在common.php中,这里不作生成二维码详解了

/**
 * 生成二维码
 * @param  string  $url  url链接
 * @param  integer $size 尺寸 纯数字
 * @param  string  $filename 要保存的文件名称
 */
function qrcode($url,$size=9,$filename){
    include "../extend/phpqrcode/phpqrcode.php";
    // 如果没有http 则添加
    if (strpos($url, 'http')===false) {
        $url='http://'.$url;
    }
    $filePath = "./resource/qrcode/".$filename.".png";
    QRcode::png($url,$filePath,QR_ECLEVEL_L,$size,2,false,0xFFFFFF,0x000000);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值