【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);
  • 标签图片
相关推荐
网安_秋刀鱼2 小时前
PHP代码审计 - SQL注入
sql·web安全·网络安全·php·1024程序员节
B20080116刘实2 小时前
CTF攻防世界小白刷题自学笔记13
开发语言·笔记·web安全·网络安全·php
数据小小爬虫8 小时前
如何用Java爬虫“偷窥”淘宝商品类目API的返回值
java·爬虫·php
汤米粥10 小时前
小皮PHP连接数据库提示could not find driver
开发语言·php
fakaifa11 小时前
CRMEB Pro版v3.1源码全开源+PC端+Uniapp前端+搭建教程
前端·小程序·uni-app·php·源码下载
万岳软件开发小城15 小时前
外卖跑腿APP开发实战:如何基于同城O2O系统源码搭建平台
php·app开发·同城o2o系统源码·同城外卖跑腿系统源码·外卖平台开发·外卖app
数勋API17 小时前
银行卡归属地查询API接口如何用PHP调用
开发语言·云计算·php
多客软件佳佳17 小时前
校园交友系统的设计与实现(开源版+三端交付+搭建+售后)
小程序·前端框架·uni-app·开源·php·交友
网络安全-海哥18 小时前
【VLANPWN】一款针对VLAN的安全研究和渗透测试工具
开发语言·javascript·安全·ecmascript·php·网安入门
豆豆19 小时前
如何选择企业网站模版来搭建网站?
服务器·开发语言·前端·php·软件构建