【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);
  • 标签图片
相关推荐
s_little_monster3 小时前
【Linux】socket网络编程基础
linux·运维·网络·笔记·学习·php·学习方法
筱宇***17 小时前
Mac的web服务器
mysql·nginx·macos·php
iangyu1 天前
【windows server脚本每天从网络盘复制到本地】
开发语言·windows·php
YUJIANYUE1 天前
PHP黑白胶卷底片图转彩图功能 V2025.05.15
php
为自己_带盐1 天前
浅聊一下数据库的索引优化
开发语言·数据库·php
Lucky小小吴2 天前
木马查杀篇—Opcode提取
php·opcode·木马查杀
邪恶的贝利亚2 天前
《ffplay 读线程与解码线程分析:从初始化到 seek 操作,对比视频与音频解码的差异》
ffmpeg·php·音视频
廖圣平2 天前
美团核销 第三方接口供应商 (含接口文档)
开发语言·数据库·php
sunsineq2 天前
[超级简单]讲解如何用PHP实现LINE Pay API!
开发语言·php·linepay
新老农2 天前
php数据导出pdf,然后pdf转图片,再推送钉钉群
pdf·php·钉钉