<?php
namespace app\common\service\qrcode;
use Grafika\Color;
use Grafika\Grafika;
use app\common\model\dealer\Setting;
class Poster extends Base
{
private $dealer;
private $config;
public function __construct($dealer)
{
parent::__construct();
$this->dealer = $dealer;
$this->config = Setting::getItem('qrcode', $dealer['wxapp_id']);
}
public function getImage()
{
if (file_exists($this->getPosterPath())) {
return $this->getPosterUrl();
}
$wxappId = $this->dealer['wxapp_id'];
$backdrop = $this->saveTempImage($wxappId, $this->config['backdrop']['src'], 'backdrop');
$avatarUrl = $this->saveTempImage($wxappId, $this->dealer['user']['avatarUrl'], 'avatar');
$qrcode = $this->saveQrcode($wxappId, 'uid:' . $this->dealer['user_id']);
return $this->savePoster($backdrop, $avatarUrl, $qrcode);
}
private function getPosterPath()
{
$tempPath = WEB_PATH . 'temp' . DS . $this->dealer['wxapp_id'] . DS;
!is_dir($tempPath) && mkdir($tempPath, 0755, true);
return $tempPath . $this->getPosterName();
}
private function getPosterName()
{
return md5('poster_' . $this->dealer['user_id']) . '.png';
}
private function getPosterUrl()
{
return \base_url() . 'temp/' . $this->dealer['wxapp_id'] . '/' . $this->getPosterName() . '?t=' . time();
}
private function savePoster($backdrop, $avatarUrl, $qrcode)
{
$editor = Grafika::createEditor(['Gd']);
$editor->open($backdropImage, $backdrop);
$this->config['avatar']['style'] === 'circle' && $this->circular($avatarUrl, $avatarUrl);
$editor->open($avatarImage, $avatarUrl);
$avatarWidth = $this->config['avatar']['width'] * 2;
$editor->resizeExact($avatarImage, $avatarWidth, $avatarWidth);
$avatarX = $this->config['avatar']['left'] * 2;
$avatarY = $this->config['avatar']['top'] * 2;
$editor->blend($backdropImage, $avatarImage, 'normal', 1.0, 'top-left', $avatarX, $avatarY);
$this->config['qrcode']['style'] === 'circle' && $this->circular($qrcode, $qrcode);
$editor->open($qrcodeImage, $qrcode);
$qrcodeWidth = $this->config['qrcode']['width'] * 2;
$editor->resizeExact($qrcodeImage, $qrcodeWidth, $qrcodeWidth);
$qrcodeX = $this->config['qrcode']['left'] * 2;
$qrcodeY = $this->config['qrcode']['top'] * 2;
$editor->blend($backdropImage, $qrcodeImage, 'normal', 1.0, 'top-left', $qrcodeX, $qrcodeY);
$fontSize = $this->config['nickName']['fontSize'] * 2 * 0.76;
$fontX = $this->config['nickName']['left'] * 2;
$fontY = $this->config['nickName']['top'] * 2;
$Color = new Color($this->config['nickName']['color']);
$fontPath = Grafika::fontsDir() . DS . 'st-heiti-light.ttc';
$editor->text($backdropImage, $this->dealer['user']['nickName'], $fontSize, $fontX, $fontY, $Color, $fontPath);
$editor->save($backdropImage, $this->getPosterPath());
return $this->getPosterUrl();
}
private function circular($imgpath, $saveName = '')
{
$srcImg = imagecreatefromstring(file_get_contents($imgpath));
$w = imagesx($srcImg);
$h = imagesy($srcImg);
$w = $h = min($w, $h);
$newImg = imagecreatetruecolor($w, $h);
imagesavealpha($newImg, true);
$bg = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefill($newImg, 0, 0, $bg);
$r = $w / 2; for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($srcImg, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($newImg, $x, $y, $rgbColor);
}
}
}
imagepng($newImg, $saveName);
imagedestroy($srcImg);
imagedestroy($newImg);
}
}