<?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;
use bw\poster\GdUtil;

/** 捐款证书绘制工具类
 * Class Common
 * @package app\bwmall\model
 */
class FundraisingCertTpl extends Base
{

    /**绘制图片方法
     * @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 = Common::getImagesFullUrl(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'];//捐款代码
//imagecreate系列函数用于从文件或 URL 载入一幅图像，成功返回图像资源，失败则返回一个空字符串。
        list($pic_w, $pic_h, $pic_type) = getimagesize($background_image_url);
        $background_image = imagecreatefromstring(file_get_contents($background_image_url));//加载背景图

//①创建一块与背景图一样大的画布，填充背景颜色白色
//建立的是一幅大小为 x和 y的黑色图像(默认为黑色)
        $background_base = imageCreatetruecolor(imagesx($background_image), imagesy($background_image));//获取图像宽度函数：imagesx() 获取图像高度函数：imagesy()
        $color = imagecolorallocate($background_base, 255, 255, 255);//imagecolorallocate:为一幅图像分配颜色，它的返回值将作为填充颜色函数imagefill($img,0,0,$color)的$color参数
        imagefill($background_base, 0, 0, $color);//imagefill:改变一幅图像的背景颜色

//②将背景图片同比例嵌入到画布里
        imagecopyresampled($background_base, $background_image, 0, 0, 0, 0, imagesx($background_image), imagesy($background_image), imagesx($background_image), imagesy($background_image));

//③插入捐款代码到海报
        $with_offset = $pic_w / 2.2; //宽偏移量
        $high_offset = $pic_h / 3.25;  //高偏移量
        $title_with = $pic_w / 2; //字体宽度范围
//字体长度
        $font_size = $pic_w / 45;
//使用字体
        $temp = array("color" => array(115, 81, 36), "fontsize" => $font_size, "width" => $title_with, "left" => $with_offset, "top" => $high_offset, "hang_size" => 40);
//绘制文字，超出
        GdUtil::draw_txt_to($background_base, $temp, $code);

//④插入人名到海报
        $with_offset = $pic_w / 2.4; //宽偏移量
        $high_offset = $pic_h / 2.2;  //高偏移量
        $title_with = $pic_w / 3.5; //字体宽度范围
//字体长度
        $font_size = $pic_w / 25;
//使用字体
        $temp = array("color" => array(115, 81, 36), "fontsize" => $font_size, "width" => $title_with, "left" => $with_offset, "top" => $high_offset, "hang_size" => 120);
//绘制文字，超出
        GdUtil::draw_txt_to($background_base, $temp, $name);

//⑤插入描述到海报
        $with_offset = $pic_w / 3.8; //宽偏移量
        $high_offset = $pic_h / 1.85;  //高偏移量
        $title_with = $pic_w / 2; //字体宽度范围
//字体长度
        $font_size = $pic_w / 35;
//使用字体
        $temp = array("color" => array(129, 129, 129), "fontsize" => $font_size, "width" => $title_with, "left" => $with_offset, "top" => $high_offset, "hang_size" => 120);
//绘制文字，超出
        GdUtil::draw_txt_to($background_base, $temp, $desc);

//⑥插入组织名到海报
        $with_offset = $pic_w / 1.44; //宽偏移量
        $high_offset = $pic_h / 1.27;  //高偏移量
        $title_with = $pic_w / 2; //字体宽度范围
//字体长度
        $font_size = $pic_w / 70;
//使用字体
        $temp = array("color" => array(129, 129, 129), "fontsize" => $font_size, "width" => $title_with, "left" => $with_offset, "top" => $high_offset, "hang_size" => 120);
//绘制文字，超出
        GdUtil::draw_txt_to($background_base, $temp, $org_name);


//⑦插入捐款时间到海报
        $with_offset = $pic_w / 1.44; //宽偏移量
        $high_offset = $pic_h / 1.23;  //高偏移量
        $title_with = $pic_w / 2; //字体宽度范围
//字体长度
        $font_size = $pic_w / 65;
//使用字体
        $temp = array("color" => array(129, 129, 129), "fontsize" => $font_size, "width" => $title_with, "left" => $with_offset, "top" => $high_offset, "hang_size" => 120);
//绘制文字，超出
        GdUtil::draw_txt_to($background_base, $temp, $createtime);
        //返回自身
        return $this->imageStream($background_base);
    }




}