grafika制作分享二维码图片

<?php
namespace app\index\controller;

use Grafika\Color;
use Grafika\Grafika;
use think\Controller;

/**
 * composer require kosinix/grafika
 * https://github.com/kosinix/grafika/
 */
class Sharecode extends Controller
{
    public function get_share_code()
    {
        $uid      = '1000';
        $text     = '冷xx';
        $code_url = ROOT_PATH . 'runtime/qrcode/qrcode' . $uid . '.png';
        $head_url = ROOT_PATH . 'runtime/head/head' . $uid . '.png';
        $this->make_image_local($uid, $text, $code_url, $head_url);
    }
    /**
     * 获取用户的推广图片
     * @param  integer $uid      [UID]
     * @param  string  $text     [姓名]
     * @param  string  $code_url [绝对路径二维码地址]
     * @param  string  $head_url [绝对路径头像地址]
     * @return [str]             [本地保存绝对路径]
     */
    public function make_image_local($uid, $text, $code_url, $head_url)
    {
        // 背景图片
        $base   = ROOT_PATH . 'public/static/base.jpg';
        $code   = $code_url;
        $head   = $head_url;
        $editor = Grafika::createEditor();
        $editor->open($image1, $base); // 背景
        $editor->open($image2, $code); // 二维码
        $editor->open($image3, $head); // 头像
        $editor->blend($image1, $image2, 'normal', 0.9, 'center', 0, 300);
        $editor->blend($image1, $image3, 'normal', 0.9, 'top-left', 80, 100);
        // 字体文件
        $ttf = ROOT_PATH . '/vendor/topthink/think-captcha/assets/zhttfs/1.ttf';
        $editor->text($image1, $text, 30, 250, 150, new Color("#000000"), $ttf, 0);
        $absolute_path = ROOT_PATH . 'public/static/' . $uid . 'shareimg.jpg';
        $editor->save($image1, $absolute_path);
        return $absolute_path;
    }
    /**
     * 获取用户的推广图片
     * @param  integer $uid      [UID]
     * @param  string  $text     [姓名]
     * @param  string  $code_url [带http的二维码地址]
     * @param  string  $head_url [带http的头像地址]
     * @return [str]             [本地保存绝对路径]
     */
    public function make_image($uid, $text, $code_url, $head_url)
    {
        $code_path = ROOT_PATH . 'public/static/' . $uid . "code.jpg";
        $this->download($code_url, $code_path);
        $head_path = ROOT_PATH . 'public/static/' . $uid . "head.jpg";
        $this->download($head_url, $head_path);
        // 背景图片
        $base   = ROOT_PATH . 'public/static/base.jpg';
        $code   = $code_path;
        $head   = $head_path;
        $editor = Grafika::createEditor();
        $editor->open($image1, $base); // 背景
        $editor->open($image2, $code); // 二维码
        $editor->open($image3, $head); // 头像
        $editor->blend($image1, $image2, 'normal', 0.9, 'center', 0, 300);
        $editor->blend($image1, $image3, 'normal', 0.9, 'top-left', 80, 100);
        // 字体文件
        $ttf = ROOT_PATH . '/vendor/topthink/think-captcha/assets/zhttfs/1.ttf';
        $editor->text($image1, $text, 30, 250, 150, new Color("#000000"), $ttf, 0);
        $absolute_path = ROOT_PATH . 'public/static/' . $uid . 'shareimg.jpg';
        $editor->save($image1, $absolute_path);
        unlink($code);
        unlink($head);
        return $absolute_path;
    }
    /**
     * 文件下载
     * @param  [type] $url           [带http的文件地址]
     * @param  [type] $absolute_path [保存的本地绝对路径带扩展名]
     * @return [type]                [description]
     */
    public function download($url, $absolute_path = '')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        $file = curl_exec($ch);
        curl_close($ch);
        $resource = fopen($absolute_path, 'a');
        fwrite($resource, $file);
        fclose($resource);
    }
}

猜你喜欢

转载自blog.csdn.net/HD2killers/article/details/83181328
今日推荐