Grafika处理图像,显示推广码

安装
github

composer require kosinix/grafika

使用

use Grafika\Color;
use Grafika\Grafika;
    /**
     * 获取用户的推广图片
     * @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_url  = $code_url;
        $code_path = ROOT_PATH . 'public/static/' . $uid . 'code . jpg';
        $this->download($code_url, $code_path);
        $head_url  = $head_url;
        $head_path = ROOT_PATH . 'public/static/' . $uid . 'head . jpg';
        $this->download($head_url, $head_path);
        $text   = $text;
        $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  [下载链接包含协议]
     * @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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值