211 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			211 lines
		
	
	
		
			7.4 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;
 | 
						||
 | 
						||
 | 
						||
 | 
						||
/** 商城GD绘制工具类
 | 
						||
 * Class Common
 | 
						||
 * @package app\bwmall\model
 | 
						||
 */
 | 
						||
class GdUtil
 | 
						||
{
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
    /**
 | 
						||
     *  将图片处理成圆图片
 | 
						||
     * 处理成圆图片,如果图片不是正方形就取最小边的圆半径,从左边开始剪切成圆形
 | 
						||
     * @param  string $imgpath [description]
 | 
						||
     * @return [type]          [description]
 | 
						||
     */
 | 
						||
    public static function yuan_img($imgpath = './tx.jpg')
 | 
						||
    {
 | 
						||
        $src_img = null;
 | 
						||
        try{
 | 
						||
            $src_img = imagecreatefromjpeg($imgpath);
 | 
						||
        }catch(\Throwable $e){
 | 
						||
            $src_img = imagecreatefrompng($imgpath);
 | 
						||
        }
 | 
						||
        $wh = getimagesize($imgpath);
 | 
						||
        $w = $wh[0];
 | 
						||
        $h = $wh[1];
 | 
						||
        $w = min($w, $h);
 | 
						||
        $h = $w;
 | 
						||
        $img = imagecreatetruecolor($w, $h);
 | 
						||
        //这一句一定要有
 | 
						||
        imagesavealpha($img, true);
 | 
						||
        //拾取一个完全透明的颜色,最后一个参数127为全透明
 | 
						||
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
 | 
						||
        imagefill($img, 0, 0, $bg);
 | 
						||
        $r = $w / 2; //圆半径
 | 
						||
        for ($x = 0; $x < $w; $x++) {
 | 
						||
            for ($y = 0; $y < $h; $y++) {
 | 
						||
                $rgbColor = imagecolorat($src_img, $x, $y);
 | 
						||
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
 | 
						||
                    imagesetpixel($img, $x, $y, $rgbColor);
 | 
						||
                }
 | 
						||
            }
 | 
						||
        }
 | 
						||
        return $img;
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    /**
 | 
						||
     *等比例缩放函数(以保存新图片的方式实现)
 | 
						||
     * @param string $picName 被缩放的处理图片源
 | 
						||
     * @param string $savePath 保存路径
 | 
						||
     * @param int $maxx 缩放后图片的最大宽度
 | 
						||
     * @param int $maxy 缩放后图片的最大高度
 | 
						||
     * @param string $pre 缩放后图片的前缀名
 | 
						||
     * @return $string 返回后的图片名称() 如a.jpg->s.jpg
 | 
						||
     *
 | 
						||
     **/
 | 
						||
    public static function scaleImg($picName, $im, $maxx = 800, $maxy = 450)
 | 
						||
    {
 | 
						||
        $info = getimageSize($picName);//获取图片的基本信息
 | 
						||
        $w = $info[0];//获取宽度
 | 
						||
        $h = $info[1];//获取高度
 | 
						||
 | 
						||
        //计算缩放比例
 | 
						||
        if (($maxx / $w) > ($maxy / $h)) {
 | 
						||
            $b = $maxy / $h;
 | 
						||
        } else {
 | 
						||
            $b = $maxx / $w;
 | 
						||
        }
 | 
						||
        //计算出缩放后的尺寸
 | 
						||
        $nw = floor($w * $b);
 | 
						||
        $nh = floor($h * $b);
 | 
						||
        //创建一个新的图像源(目标图像)
 | 
						||
        $nim = imagecreatetruecolor($nw, $nh);
 | 
						||
 | 
						||
        //透明背景变黑处理
 | 
						||
        //2.上色
 | 
						||
        $color = imagecolorallocate($nim, 255, 255, 255);
 | 
						||
        //3.设置透明
 | 
						||
        imagecolortransparent($nim, $color);
 | 
						||
        imagefill($nim, 0, 0, $color);
 | 
						||
        //执行等比缩放
 | 
						||
        imagecopyresampled($nim, $im, 0, 0, 0, 0, $nw, $nh, $w, $h);
 | 
						||
        //返回结果
 | 
						||
        return $nim;
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    /**
 | 
						||
     * 文字自动换行算法
 | 
						||
     * @param $card 画板
 | 
						||
     * @param $pos 数组,top距离画板顶端的距离,fontsize文字的大小,width宽度,left距离左边的距离,hang_size行高
 | 
						||
     * @param $str 要写的字符串
 | 
						||
     * @param $iswrite  是否输出,ture,  花出文字,false只计算占用的高度
 | 
						||
     * @return int 返回整个字符所占用的高度
 | 
						||
     */
 | 
						||
 | 
						||
    public static function draw_txt_to($card, $pos, $str, $iswrite = true, $font = "")
 | 
						||
    {
 | 
						||
        if(!$font)$font = ROOT_PATH."/public/assets/fonts/simhei.ttf";
 | 
						||
        $_str_h = $pos["top"];
 | 
						||
        $fontsize = $pos["fontsize"];
 | 
						||
        $width = $pos["width"];
 | 
						||
        $margin_lift = $pos["left"];
 | 
						||
        $hang_size = $pos["hang_size"];
 | 
						||
        $temp_string = "";
 | 
						||
        $font_file = $font;
 | 
						||
        $tp = 0;
 | 
						||
 | 
						||
        $font_color = imagecolorallocate($card, $pos["color"][0], $pos["color"][1], $pos["color"][2]);
 | 
						||
        for ($i = 0; $i < mb_strlen($str); $i++) {
 | 
						||
 | 
						||
            $box = imagettfbbox($fontsize, 0, $font_file, $temp_string);
 | 
						||
            $_string_length = $box[2] - $box[0];
 | 
						||
            $temptext = mb_substr($str, $i, 1);
 | 
						||
 | 
						||
            $temp = imagettfbbox($fontsize, 0, $font_file, $temptext);
 | 
						||
 | 
						||
            if ($_string_length + $temp[2] - $temp[0] < $width) {//长度不够,字数不够,需要
 | 
						||
 | 
						||
                //继续拼接字符串。
 | 
						||
 | 
						||
                $temp_string .= mb_substr($str, $i, 1);
 | 
						||
 | 
						||
                if ($i == mb_strlen($str) - 1) {//是不是最后半行。不满一行的情况
 | 
						||
                    $_str_h += $hang_size;//计算整个文字换行后的高度。
 | 
						||
                    $tp++;//行数
 | 
						||
                    if ($iswrite) {//是否需要写入,核心绘制函数
 | 
						||
                        imagettftext($card, $fontsize, 0, $margin_lift, $_str_h, $font_color, $font_file, $temp_string);
 | 
						||
                    }
 | 
						||
 | 
						||
                }
 | 
						||
            } else {//一行的字数够了,长度够了。
 | 
						||
 | 
						||
//            打印输出,对字符串零时字符串置null
 | 
						||
                $texts = mb_substr($str, $i, 1);//零时行的开头第一个字。
 | 
						||
 | 
						||
//            判断默认第一个字符是不是符号;
 | 
						||
                $isfuhao = preg_match("/[\\\\pP]/u", $texts) ? true : false;//一行的开头这个字符,是不是标点符号
 | 
						||
                if ($isfuhao) {//如果是标点符号,则添加在第一行的结尾
 | 
						||
                    $temp_string .= $texts;
 | 
						||
 | 
						||
//                判断如果是连续两个字符出现,并且两个丢失必须放在句末尾的,单独处理
 | 
						||
                    $f = mb_substr($str, $i + 1, 1);
 | 
						||
                    $fh = preg_match("/[\\\\pP]/u", $f) ? true : false;
 | 
						||
                    if ($fh) {
 | 
						||
                        $temp_string .= $f;
 | 
						||
                        $i++;
 | 
						||
                    }
 | 
						||
 | 
						||
                } else {
 | 
						||
                    $i--;
 | 
						||
                }
 | 
						||
 | 
						||
                $tmp_str_len = mb_strlen($temp_string);
 | 
						||
                $s = mb_substr($temp_string, $tmp_str_len - 1, 1);//取零时字符串最后一位字符
 | 
						||
 | 
						||
                if (self::is_firstfuhao($s)) {//判断零时字符串的最后一个字符是不是可以放在见面
 | 
						||
                    //讲最后一个字符用“_”代替。指针前移动一位。重新取被替换的字符。
 | 
						||
                    $temp_string = rtrim($temp_string, $s);
 | 
						||
                    $i--;
 | 
						||
                }
 | 
						||
//            }
 | 
						||
 | 
						||
//            计算行高,和行数。
 | 
						||
                $_str_h += $hang_size;
 | 
						||
                $tp++;
 | 
						||
                if ($iswrite) {
 | 
						||
 | 
						||
                    imagettftext($card, $fontsize, 0, $margin_lift, $_str_h, $font_color, $font_file, $temp_string);
 | 
						||
                }
 | 
						||
//           写完了改行,置null该行的临时字符串。
 | 
						||
                $temp_string = "";
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        return $tp * $hang_size;
 | 
						||
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    public static function is_firstfuhao($str)
 | 
						||
    {
 | 
						||
        $fuhaos = array("\\", "“", "'", "<", "《",);
 | 
						||
 | 
						||
        return in_array($str, $fuhaos);
 | 
						||
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
} |