【php】GD库生成属性标签图

  • 标签图生成方法
php 复制代码
   public static function createLabelImage($labelList,$fileName=""){

        $initImageFun = function($width,$height){
            $image = imagecreatetruecolor($width, $height);
            $backgroundColor = imagecolorallocate($image, 255, 255, 255);
            imagefilledrectangle($image, 0, 0, $width, $height, $backgroundColor);
            return $image;
        };

        //是否包含中文字符
        $containsChineseFun = function ($str) {
            if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $str)) {
                return true;
            } else {
                return false;
            }
        };
		//属性描述填充 超出长度自动换行
        $decFillImageFun = function($image,$textDesc2,&$yHeight,$textColor,$maxlen=15,$xDecStart=185,$fontSize=25){
            $fontDec = "./static/admin/font/msyh.ttc";
            $length2 = mb_strlen($textDesc2, 'UTF-8');
            $lenNum = ceil($length2/$maxlen);
            for($i=0;$i<$lenNum;$i++){
                $yHeightLen = $yHeight+50*$i;
                imagettftext($image, $fontSize, 0, $xDecStart, $yHeightLen, $textColor, $fontDec, mb_substr($textDesc2,$i*$maxlen,$maxlen));
                if($i == $lenNum-1){
                    $yHeight = $yHeightLen;
                }
            }
        };

        //计算画布总高度
        $calculateHeightFun = function($labelList,$yHeightStart,$lengHeight){
            $totalHeight = $yHeightStart;
            foreach ($labelList as $row){
                $length2 = mb_strlen($row['desc'], 'UTF-8');
                $maxlen = 15;
                $lenNum = ceil($length2/$maxlen);
                $totalHeight += ($lenNum-1)*50 + $lengHeight;
            }
            return $totalHeight;
        };
		
        $fontSize = 25;
        $xTitleStart = 10;
        $lenHeight = 80;
        $yHeightStart = 50;
        $width = 700;
        $totalHeight = $calculateHeightFun($labelList,$yHeightStart,$lenHeight);
        $image = $initImageFun($width,$totalHeight);//创建画布handle
        $textColor = imagecolorallocate($image, 0, 0, 0);
        //字体文件 微软雅黑加粗
        $fontTitle = "./static/admin/font/msyhbd.ttc";

        $yHeight = $yHeightStart;
        foreach ($labelList as $row){
            $textTitle = $row['title'].":";
            imagettftext($image, $fontSize, 0, $xTitleStart, $yHeight, $textColor, $fontTitle, $textTitle);

            if($containsChineseFun($row['desc'])){
                $decFillImageFun($image,$row['desc'],$yHeight,$textColor);
            }else{
                $decFillImageFun($image,$row['desc'],$yHeight,$textColor,30);
            }
            $yHeight = $yHeight+$lenHeight;
        }
		
		//标签保存目录
        $imagePath = './static/storage/goods_label/';
        if (!file_exists($imagePath)) {
            mkdir($imagePath, 0777, true);
        }
        if(empty($fileName)){
            $fileName = time().'_'.rand(1000,9999);
        }
        $imageFullName = $imagePath.$fileName.'.jpg';
        imagepng($image, $imageFullName);

        imagedestroy($image);
        unset($yHeight);

        return $imageFullName;
    }
  • 调用示例
php 复制代码
		$goodsLabel = [];
        $goodsLabel[] = [
            "title" => "IMEI/SN",
            "desc"  => $info['is_rand_ship']?"随机发货":$info['barcode']
        ];
        $goodsLabel[] = [
            "title" => "质检报告",
            "desc"  => $checkList["verdict"]
        ];
        $goodsLabel[] = [
            "title" => "包装清单",
            "desc"  => $checkList["packlist"]
        ];

        $fileName = $info['barcode'] ? $info['barcode'] : $info['goods_id'];
        \app\Common::createLabelImage($goodsLabel,$fileName);
  • 标签图片
相关推荐
yanwushu4 小时前
10分钟搭建 PHP 开发环境教程
php·laravel
车载测试工程师12 小时前
车载以太网网络测试-29【SOME/IP-SD】-SD状态机
网络·网络协议·tcp/ip·车载系统·php
还鮟21 小时前
CTF Web PHP弱类型与进制绕过(过滤)
php·ctf
zorro_z21 小时前
PHP语法基础篇(八):超全局变量
php
九分源码1 天前
基于PHP+MySQL组合开发开源问答网站平台源码系统 源码开源可二次开发 含完整的搭建指南
mysql·开源·php
RainSerein1 天前
Laravel8中使用phpword生成word文档
word·php·laravel
pltrue1 天前
Go 重构案例分享:订单创建逻辑重构
go·php
RainSerein1 天前
Laravel8中调取腾讯云文字识别OCR
ocr·php·腾讯云·laravel
杰哥技术分享2 天前
PHP Yii2 安装SQL Server扩展-MAC M4 Pro芯片
开发语言·php
wuk99811 天前
深入理解PHP中的生成器(Generators)
开发语言·php