<?php
|
|
namespace app\common\service\business;
|
|
use app\common\model\plus\business\Template;
|
use app\common\service\qrcode\Base;
|
use Grafika\Color;
|
use Grafika\Grafika;
|
|
/**
|
* 生成名片
|
* Class Qrcode
|
* @package app\common\service
|
*/
|
class Poster extends Base
|
{
|
/* @var array $dealer 用户信息 */
|
private $dealer;
|
|
/* @var array $config 名片设置 */
|
private $config;
|
public $template;
|
|
/**
|
* 构造方法
|
* Poster constructor.
|
* @param $dealer
|
* @throws \Exception
|
*/
|
public function __construct($dealer)
|
{
|
parent::__construct();
|
// 用户信息
|
$this->dealer = $dealer;
|
$this->template = (new Template())->detail($dealer['template_id']);
|
// 名片设置
|
$this->config = json_decode($this->template['style'], true);
|
}
|
|
/**
|
* 获取用户名片
|
* @return string
|
* @throws \app\common\exception\BaseException
|
* @throws \think\exception\DbException
|
* @throws \Exception
|
*/
|
public function getImage($isType = true)
|
{
|
if (file_exists($this->getPosterPath('business')) && $isType) {
|
return $this->getPosterUrl('business');
|
}
|
// 小程序id
|
$appId = $this->dealer['app_id'];
|
// 1. 下载背景图
|
$backdrop = $this->saveTempImage($appId, $this->config['backdrop']['src'], 'backdrop');
|
if ($this->dealer['file_path']) {
|
// 2. 下载用户头像
|
$avatarUrl = $this->saveTempImage($appId, $this->dealer['file_path'], 'avatar');
|
} else {
|
// 2. 下载用户头像
|
$avatarUrl = $this->saveTempImage($appId, $this->dealer['user']['avatarUrl'], 'avatar');
|
}
|
$logo = '';
|
if ($this->dealer['logoImage']['file_path']) {
|
// 2. 下载logo
|
$logo = $this->saveTempImage($appId, $this->dealer['logoImage']['file_path'], 'logo');
|
}
|
// 4. 拼接名片
|
return $this->savePoster($backdrop, $avatarUrl, 'business', $logo);
|
}
|
|
/**
|
* 名片图文件路径
|
* @return string
|
*/
|
private function getPosterPath($name)
|
{
|
// 保存路径
|
$tempPath = $_SERVER['DOCUMENT_ROOT']. '/temp/'.$this->template['app_id'] . '/'.$name.'/';
|
!is_dir($tempPath) && mkdir($tempPath, 0755, true);
|
return $tempPath . $this->getPosterName();
|
}
|
|
/**
|
* 名片文件名称
|
* @return string
|
*/
|
private function getPosterName()
|
{
|
return md5('poster_' . $this->dealer['business_card_id']) . '.png';
|
}
|
|
/**
|
* 名片url
|
* @return string
|
*/
|
private function getPosterUrl($name)
|
{
|
return base_url() . '/temp/' . $this->template['app_id'] . '/' .$name . '/' . $this->getPosterName() . '?t=' . time();
|
}
|
|
/**
|
* 拼接名片
|
* @param $backdrop
|
* @param $avatarUrl
|
* @param $imageUrl
|
* @param $logo
|
* @return string
|
* @throws \Exception
|
*/
|
private function savePoster($backdrop, $avatarUrl, $imageUrl, $logo)
|
{
|
|
// 创建画布
|
list($width, $height) = getimagesize($backdrop);
|
$newImage = imagecreatetruecolor($width, $height);
|
$backdropIm = $this->imagEcr($backdrop);
|
imageantialias($newImage, true);
|
// 将第一张图片覆盖在新图像上
|
imagecopy($newImage, $backdropIm, 0, 0, 0, 0, $width, $height);
|
if (!empty($this->config['icon'])) {
|
foreach ($this->config['icon'] as $key => $value) {
|
$this->addImagecopy($newImage, $value);
|
}
|
}
|
|
// 写入地址
|
//$this->addText($newImage, 'address', '地址:', 0, false, $width);
|
if ($this->config['avatar']['display'] == 1) {
|
// 生成圆形用户头像
|
$this->config['avatar']['style'] === 'circle' && $this->circular($avatarUrl, $avatarUrl);
|
// 重设用户头像宽高
|
$avatarWidth = $this->config['avatar']['width'];
|
//重设图片大小
|
$this->resizeExact($avatarUrl,$avatarWidth,$avatarWidth);
|
// 用户头像添加到背景图
|
$avatarX = $this->config['avatar']['left'];
|
$avatarY = $this->config['avatar']['top'];
|
// 打开用户头像
|
$avatarIm = $this->imagEcr($avatarUrl);
|
imagecopy($newImage, $avatarIm, $avatarX, $avatarY, 0, 0, $avatarWidth, $avatarWidth);
|
}
|
if (is_file($logo) && $this->config['logo']['display'] == 1) {
|
// 生成圆形logo
|
$this->config['logo']['style'] === 'circle' && $this->circular($logo, $logo);
|
//调整后大小
|
$logoWidth = round($this->config['logo']['width']);
|
$logoHeight = round($this->config['logo']['height']);
|
//重设图片大小
|
$this->resizeExact($logo,$logoWidth,$logoHeight);
|
// 用户logo添加到背景图
|
$logoX = $this->config['logo']['left'];
|
$logoY = $this->config['logo']['top'];
|
// 打开用户logo
|
$logoImage = $this->imagEcr($logo);
|
imagecopy($newImage, $logoImage, $logoX, $logoY, 0, 0, $logoWidth, $logoHeight);
|
}
|
|
// 写入用户昵称
|
$this->addText($newImage, 'name');
|
foreach ($this->dealer['unit'] as $key => $value) {
|
// 写入公司
|
$this->addText($newImage, 'unit', '', $key, true);
|
}
|
foreach ($this->dealer['address'] as $key => $value) {
|
// 写入地址
|
$this->addText($newImage, 'address', '', $key, true);
|
}
|
foreach ($this->dealer['duties'] as $key => $value) {
|
// 写入职务
|
$this->addText($newImage, 'duties', '', $key, true);
|
}
|
// 写入职位
|
//$this->addText($newImage, 'duties');
|
if (!empty($this->dealer['position']) && !empty($this->config['position'])) {
|
foreach ($this->dealer['position'] as $key => $value) {
|
// 写入公司
|
$this->addText($newImage, 'position', '', $key, true);
|
}
|
}
|
// 写入手机号
|
$this->addText($newImage, 'mobile', '手机:');
|
|
// 写入微信
|
if ($this->dealer['mailbox']) {
|
// 写入邮箱
|
$this->addText($newImage, 'mailbox', '邮箱:');
|
}
|
if ($this->dealer['phone']) {
|
$this->addText($newImage, 'phone', '电话:');
|
}
|
// 保存图片
|
imagejpeg($newImage, $this->getPosterPath($imageUrl),100); // 根据需要选择合适的函数(如imagepng、imagegif等)
|
// 清理内存
|
imagedestroy($newImage);
|
return $this->getPosterUrl($imageUrl);
|
}
|
|
/**
|
* 打开图片
|
* @param $image
|
* @return false|resource
|
*/
|
public function imagEcr($image)
|
{
|
// 创建画布
|
return imagecreatefromstring(file_get_contents($image));
|
}
|
|
/**
|
* 重设图片大小
|
* @param $image
|
* @param $newWidth
|
* @param $newHeight
|
* @return void
|
*/
|
public function resizeExact($imageUrl, $newWidth, $newHeight )
|
{
|
$image=imagecreatefromstring(file_get_contents($imageUrl));
|
// 调整图片大小
|
$targetImage = imagecreatetruecolor($newWidth, $newHeight);
|
imageantialias($targetImage, true);
|
//获取图片大小
|
$width = imagesx($image);
|
$height = imagesy($image);
|
// 这一句一定要有
|
imagesavealpha($targetImage, true);
|
$bg = imagecolorallocatealpha($targetImage, 255, 255, 255, 127);
|
imagefill($targetImage, 0, 0, $bg);
|
imagecopyresampled($targetImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
imagepng($targetImage, $imageUrl,0); // 根据需要选择合适的函数(如imagepng、imagegif等)
|
// 清理内存
|
imagedestroy($targetImage);
|
}
|
|
/**
|
* 添加图标
|
* @param $newImage
|
* @param $data
|
* @return void
|
*/
|
public function addImagecopy($newImage,$data){
|
$src=$this->saveTempImage($this->template['app_id'], $data['src'], 'icon');
|
$this->resizeExact($src,$data['width'], $data['height']);
|
$backdropIm = $this->imagEcr($src);
|
// 将第一张图片覆盖在新图像上
|
imagecopy($newImage, $backdropIm, $data['left'], $data['top'], 0, 0, $data['width'], $data['height']);
|
}
|
|
/**
|
* 写入文字
|
* @param $editor
|
* @param $backdropImage
|
* @param $name
|
* @param $text
|
* @param $key
|
* @param $type
|
* @param $width
|
* @return array|bool
|
*/
|
public function addText($editor, $name, $text = '', $key = 0, $type = false, $width = 0)
|
{
|
$fontPath = $_SERVER['DOCUMENT_ROOT']. '/fonts/MSYH.TTC';
|
if ($type) {
|
list($fontSize, $fontX,$fontY) = self::SizeLeftTop($this->config[$name][$key]['fontSize'], $this->config[$name][$key]['left'],$this->config[$name][$key]['top']);
|
$colorResource=self::colorResource($this->config[$name][$key]['color'],$editor);
|
return imagettftext($editor, $fontSize, 0, $fontX, $fontY, $colorResource, $fontPath, $text . $this->dealer[$name][$key]);
|
} else if ($name == 'mobile') {
|
list($fontSize, $fontX,$fontY) = self::SizeLeftTop($this->config[$name]['fontSize'], $this->config[$name]['left'],$this->config[$name]['top']);
|
$text = $text . $this->dealer['mobile'];
|
if (!empty($this->dealer['mobile_phone'])) {
|
$text = $text . ' / ' . $this->dealer['mobile_phone'];
|
}
|
$colorResource=self::colorResource($this->config[$name]['color'],$editor);
|
return imagettftext($editor, $fontSize, 0, $fontX, $fontY, $colorResource, $fontPath, $text);
|
} /*else if ($name == 'duties') {
|
list($fontSize, $fontX,$fontY) = self::SizeLeftTop($this->config[$name][$key]['fontSize'], $this->config[$name][$key]['left'],$this->config[$name][$key]['top']);
|
$duties = $this->dealer['duties'][$key];
|
if (!empty($this->dealer['duties'][1])) {
|
$duties = $duties . ' / ' . $this->dealer['duties'][1];
|
}
|
if (!empty($this->dealer['duties'][2])) {
|
$duties = $duties . ' / ' . $this->dealer['duties'][2];
|
}
|
$colorResource=self::colorResource($this->config['duties'][$key]['color'],$editor);
|
return imagettftext($editor, $fontSize, 0, $fontX, $fontY, $colorResource, $fontPath, $text . $duties);
|
}*/ else if ($name == 'address') {
|
list($fontSize, $fontX,$fontY) = self::SizeLeftTop($this->config[$name][$key]['fontSize'], $this->config[$name][$key]['left'],$this->config[$name][$key]['top']);
|
$title = $this->dealer['address'][$key];
|
$strlen = mb_strlen($title, 'utf-8');
|
$left = $width - $this->config['address'][$key]['left'] - $fontSize;
|
$titleNum = bcdiv($left, $this->config['address'][$key]['fontSize']);
|
$the_box = $this->config['address'][$key]['fontSize'] * 3;
|
while ($width < ($titleNum * $this->config['address'][$key]['fontSize'] + $the_box + $fontX)) {
|
$titleNum--;
|
};
|
if ($strlen > $titleNum && $titleNum) {
|
$strArr = self::mbStrSplit($title, $titleNum);
|
}
|
$colorResource=self::colorResource($this->config['address'][$key]['color'],$editor);
|
if ($strlen > $titleNum && $titleNum) {
|
$y = $fontY + 10;
|
foreach ($strArr as $k => $v) {
|
if ($k == 0) {
|
imagettftext($editor, $fontSize, 0, $fontX, $fontY, $colorResource, $fontPath, $text . $v);
|
} else {
|
$y = $y + ($fontSize * $k);
|
imagettftext($editor, $fontSize, 0, $fontX + $the_box, $y, $colorResource, $fontPath, $v);
|
}
|
}
|
return true;
|
} else {
|
return imagettftext($editor, $fontSize, 0, $fontX, $fontY, $colorResource, $fontPath, $text . $title);
|
}
|
|
} else {
|
list($fontSize, $fontX,$fontY) = self::SizeLeftTop($this->config[$name]['fontSize'], $this->config[$name]['left'],$this->config[$name]['top']);
|
$colorResource=self::colorResource($this->config[$name]['color'],$editor);
|
return imagettftext($editor, $fontSize, 0, $fontX, $fontY, $colorResource, $fontPath, $text . $this->dealer[$name]); // path/to/font.ttf为自定义字体路径
|
}
|
|
}
|
|
/**
|
* 分割字符串为数组模式
|
* @param $string
|
* @param $len
|
* @return mixed
|
*/
|
private function mbStrSplit($string, $len = 1)
|
{
|
$start = 0;
|
$strlen = mb_strlen($string);
|
while ($strlen) {
|
$array[] = mb_substr($string, $start, $len, "utf8");
|
$string = mb_substr($string, $len, $strlen, "utf8");
|
$strlen = mb_strlen($string);
|
}
|
return $array;
|
}
|
|
/**
|
* 生成圆形图片
|
* @param static $imgpath 图片地址
|
* @param string $saveName 保存文件名,默认空。
|
*/
|
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);
|
// 拾取一个完全透明的颜色,最后一个参数127为全透明
|
$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,0);
|
// 释放空间
|
imagedestroy($srcImg);
|
imagedestroy($newImg);
|
}
|
|
/**
|
* 获取名片模板
|
* @return string
|
* @throws \app\common\exception\BaseException
|
* @throws \think\exception\DbException
|
* @throws \Exception
|
*/
|
public function getImageE($config, $template_id = '')
|
{
|
$model = new Template();
|
$this->config = $config;
|
// 小程序id
|
$appId = $this->template['app_id'];
|
// 1. 下载背景图
|
$backdrop = $this->saveTempImage($appId, $this->config['backdrop']['src'], 'backdrop');
|
// 2. 下载用户头像
|
$avatarUrl = $this->saveTempImage($appId, $model::$base_url."/image/agent/avatar.jpg", 'avatar', $template_id);
|
// 2. 下载logo
|
$logo = $this->saveTempImage($appId, $this->config['logo']['src'], 'logo');
|
// 4. 拼接名片
|
return $this->savePoster($backdrop, $avatarUrl, 'template', $logo);
|
}
|
|
/**
|
* 获取颜色
|
* @param $color
|
* @param $editor
|
* @return false|int
|
*/
|
private function colorResource($color, $editor)
|
{
|
$Color = new Color($color);
|
list($r, $g, $b, $alpha) = $Color->getRgba();
|
$scale = round(127 * $alpha);
|
$invert = 127 - $scale;
|
return imagecolorallocatealpha(
|
$editor,
|
$r, $g, $b,
|
$invert
|
);
|
}
|
|
/**
|
*计算字体大小和顶部跟左边的距离
|
* @param $fontSize
|
* @param $left
|
* @param $top
|
* @return array
|
*/
|
private function SizeLeftTop($fontSize,$left,$top){
|
$data[0] = $fontSize * 0.76;
|
$data[1] = $left;
|
$data[2] = $top + $fontSize;
|
return $data;
|
}
|
|
}
|