grafika製作分享二維碼圖片
阿新 • • 發佈:2018-12-16
<?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 { /** * 獲取使用者的推廣圖片 * @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 [帶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); } }