安装
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);
}