DiverseYouthNightSchool/extend/bw/poster/template/Base.php

128 lines
3.8 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------
namespace bw\poster\template;
use bw\Common;
/** 捐款证书绘制工具基类
* Class Common
* @package app\bwmall\model
*/
abstract class Base
{
protected $binaryData = null;
protected $temp_url = 'uploads/qrcode';//临时,目录
protected $path = '';
protected $fileName = '';
protected $data = [];//替换数据
//抽象方法
abstract public function draw($data = []);
/**绘制图片方法
* @param array $config
* @param string $path
* @param string $fileName
* @return array
* @throws \app\common\exception\UploadException
*/
public function out()
{
if(!$this->binaryData)$this->draw();
if(!$this->binaryData)throw new \Exception('没有二进制流文件');
ob_start();
imagejpeg($this->binaryData);
imagedestroy($this->binaryData);
$res = ob_get_contents();//文件二进制流
ob_end_clean();
$outfile = $this->temp_url . '/'; //本地缓存地址
if (!file_exists('./' . $outfile . $this->path)) mkdir('./' . $outfile . $this->path, 0777, true);
$filepath = './' . $outfile . $this->path . '/' . $this->fileName;
file_put_contents($filepath, $res);
//保存到fastadmin框架
$attachment = Common::setFastAdminFile($filepath, $this->fileName);
// TODO: 生成后删除源文件
@unlink($filepath);
return ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)];
}
/**
* 设置模板保存路径
*/
public function path($path){
$this->path = $path;
return $this;
}
public function fileName($fileName){
$this->fileName = $fileName;
return $this;
}
public function imageStream($imageStream){
$this->binaryData = $imageStream;
return $this;
}
/**
* @param $data
* @param null $value
* @return $this
*/
public function setData($data,$value=null){
if(is_array($data)) $this->data = array_merge($this->data,$data);
if(is_string($data))$this->data[$data] = $value;
return $this;
}
/**绘制图片方法DEMO
* @param array $config
* @param string $path
* @param string $fileName
* @return array
* @throws \app\common\exception\UploadException
*/
// public function draw($data = [])
// {
// if($data)$this->setData($data);
//
// //得到动态数据
// $background_image_url = config("site.fundraising_cert_tpl"); //背景图片
// if(!$background_image_url)throw new \Exception("缺少证书模板");
// $name = $this->data['name'];//人名
// $org_name = $this->data['org_name'];//组织名
// $createtime = $this->data['createtime'];//捐款时间
// $desc = $this->data['desc'];//描述
// $code = $this->data['code'];//捐款代码
// // 调用绘制方法
// //$background_base = xxxx
// //返回自身
// return $this->imageStream($background_base);
// }
}