zoukankan      html  css  js  c++  java
  • PHP批量生成底部带编号二维码(二维码生成+文字生成图片+图片拼接合并)

    PHP批量生成带底部编号二维码(二维码生成+文字生成图片+图片拼接合并)

    需求:

    输入编号如 : cb05-0000001  至 cb05-0000500 批量生成 以编号为名称的下图二维码,然后压缩并下载

     思路: phpqrcode 生成 二维码  -->  编号字符串生成图片 ---> 二维码与编号图片拼接---->压缩 下载

    1  PHP生成二维码  

    下载并加载phpqrcode.php,本次需批量生成二维码,多次调用此函数,注意 include_once

        //生成二维码图片
        public function makeCodeImg($url, $product_sn = '2018**82019')
        {$path = 'upload/product_qr_code';
            if (!is_dir($path)) {
                mkdir($path, 0777, true);
            }
            include_once 'phpqrcode/phpqrcode.php';
            $value = $url;                  //二维码内容
            $errorCorrectionLevel = 'L';    //容错级别
            $matrixPointSize = 12;           //生成图片大小
    
            $filename = $path . '/' . $product_sn . '.jpg';
            QRcode::png($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
            $QR = $filename;                //已经生成的原始二维码图片文件
            $QR = imagecreatefromstring(file_get_contents($QR));
            imagejpeg($QR, $product_sn . 'jpg');
        }

    2 编号字符串生成图片 文字居中(注意字体文件下载与选择)

    若 文字为中文 字体选择不当会出现 小方框替代了文字

    //文字生成图片
        public function makeImgWithStr($filename, $text, $font_size=20,$font = 'font/Arial/Arial.ttf')
        {
            //图片尺寸
            $im = imagecreatetruecolor(444, 70);
            //背景色
            $white = imagecolorallocate($im, 255, 255, 255);
            //字体颜色
            $black = imagecolorallocate($im, 0, 0, 0);
    
            imagefilledrectangle($im, 0, 0, 444, 300, $white);
            $txt_max_width = intval(0.8 * 444);
            $content = "";
            for ($i = 0; $i < mb_strlen($text); $i++) {
                $letter[] = mb_substr($text, $i, 1);
            }
            foreach ($letter as $l) {
                $test_str = $content . " " . $l;
                $test_box = imagettfbbox($font_size, 0, $font, $test_str);
                // 判断拼接后的字符串是否超过预设的宽度。超出宽度添加换行
                if (($test_box[2] > $txt_max_width) && ($content !== "")) {
                    $content .= "
    ";
                }
                $content .= $l;
            }
    
            $txt_width = $test_box[2] - $test_box[0];
    
            $y = 70 * 0.5; // 文字从何处的高度开始
            $x = (444 - $txt_width) / 2; //文字居中
            // echo $x;die;
            //文字写入
            imagettftext($im, $font_size, 0, $x, $y, $black, $font, $content); //写 TTF 文字到图中
            //图片保存
            imagejpeg($im, $filename);
        }

    3 合并拼接二维码与文字图片(竖直拼接保证等宽,横向拼接保证等高)

      /**
         * 合并图片,拼接合并
         * @param array $image_path 需要合成的图片数组
         * @param $save_path 合成后图片保存路径
         * @param string $axis 合成方向
         * @param string $save_type 合成后图片保存类型
         * @return bool|array
         */
        public function CompositeImage(array $image_path, $save_path, $axis = 'y', $save_type = 'png')
        {
            if (count($image_path) < 2) {
                return false;
            }
            //定义一个图片对象数组
            $image_obj = [];
            //获取图片信息
            $width = 0;
            $height = 0;
            foreach ($image_path as $k => $v) {
                $pic_info = getimagesize($v);
                list($mime, $type) = explode('/', $pic_info['mime']);
                //获取宽高度
                $width += $pic_info[0];
                $height += $pic_info[1];
                if ($type == 'jpeg') {
                    $image_obj[] = imagecreatefromjpeg($v);
                } elseif ($type == 'png') {
                    $image_obj[] = imagecreatefrompng($v);
                } else {
                    $image_obj[] = imagecreatefromgif($v);
                }
            }
            //按轴生成画布方向
            if ($axis == 'x') {
                //TODO X轴无缝合成时请保证所有图片高度相同
                $img = imageCreatetruecolor($width, imagesy($image_obj[0]));
            } else {
                //TODO Y轴无缝合成时请保证所有图片宽度相同
                $img = imageCreatetruecolor(imagesx($image_obj[0]), $height);
            }
            //创建画布颜色
            $color = imagecolorallocate($img, 255, 255, 255);
            imagefill($image_obj[0], 0, 0, $color);
            //创建画布
            imageColorTransparent($img, $color);
            imagecopyresampled($img, $image_obj[0], 0, 0, 0, 0, imagesx($image_obj[0]), imagesy($image_obj[0]), imagesx($image_obj[0]), imagesy($image_obj[0]));
            $yx = imagesx($image_obj[0]);
            $x = 0;
            $yy = imagesy($image_obj[0]);
            $y = 0;
            //循环生成图片
            for ($i = 1; $i <= count($image_obj) - 1; $i++) {
                if ($axis == 'x') {
                    $x = $x + $yx;
                    imagecopymerge($img, $image_obj[$i], $x, 0, 0, 0, imagesx($image_obj[$i]), imagesy($image_obj[$i]), 100);
                } else {
                    $y = $y + $yy;
                    imagecopymerge($img, $image_obj[$i], 0, $y, 0, 0, imagesx($image_obj[$i]), imagesy($image_obj[$i]), 100);
                }
            }
            //设置合成后图片保存类型
            if ($save_type == 'png') {
                imagepng($img, $save_path);
            } elseif ($save_type == 'jpg' || $save_type == 'jpeg') {
                imagejpeg($img, $save_path);
            } else {
                imagegif($img, $save_path);
            }
            return true;
        }

    图片等宽处理参考(等高处理类似)

    public function ImgCompress($src, $out_with = 150)
        {
            // 获取图片基本信息
            list($width, $height, $type, $attr) = getimagesize($src);
            // 获取图片后缀名
            $pic_type = image_type_to_extension($type, false);
            // 拼接方法
            $imagecreatefrom = "imagecreatefrom" . $pic_type;
            // 打开传入的图片
            $in_pic = $imagecreatefrom($src);
            // 压缩后的图片长宽
            $new_width = $out_with;
            $new_height = $out_with / $width * $height;
            // 生成中间图片
            $temp = imagecreatetruecolor($new_width, $new_height);
            // 图片按比例合并在一起。
            imagecopyresampled($temp, $in_pic, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            // 销毁输入图片
            imagejpeg($temp, 'upload/merge' . time() . ".jpg");
            imagedestroy($in_pic);
            return array($temp, $new_width, $new_height);
    
        }

    5 压缩合并与下载

    //生成带编号说明的二维码 (生成二维码  文字生成图片 图片合并拼接)
        public function makeMergerImg($sn_product){
            $this->makeCodeImg('dev2.lystrong.cn',$sn_product);
            $this->makeImgWithStr('upload/sn_str_img/'.$sn_product.'.jpg',$sn_product,30);
            $this->CompositeImage(['upload/product_qr_code/'.$sn_product.'.jpg','upload/sn_str_img/'.$sn_product.'.jpg'],'upload/pin_code/'.$sn_product.'.png');
            unlink('upload/sn_str_img/'.$sn_product.'.jpg');
            unlink('upload/product_qr_code/'.$sn_product.'.jpg');
        } 
      //生成压缩zip文件 $file_name 最终生成的文件名,包含路径   $file_list,用来生成file_name的文件数组
      public function makeZip($file_name, $file_list)
        {
            if (file_exists($file_name)) {
                unlink($file_name);
            }
            //重新生成文件
            $zip = new ZipArchive();
            if ($zip->open($file_name, ZIPARCHIVE::CREATE) !== TRUE) {
                exit('无法打开文件,或者文件创建失败');
            }
            foreach ($file_list as $val) {
                if (file_exists($val)) {
                    $zip->addFile($val);
                }
            }
            $zip->close();//关闭
            if (!file_exists($file_name)) {
                exit('无法找到文件'); //即使创建,仍有可能失败
            }
        }
      //下载
        public function download($file){
            if ( file_exists ( $file )) {
                header ( 'Content-Description: File Transfer' );
                header ( 'Content-Type: application/octet-stream' );
                header ( 'Content-Disposition: attachment; filename=' . basename ( $file ));
                header ( 'Content-Transfer-Encoding: binary' );
                header ( 'Expires: 0' );
                header ( 'Cache-Control: must-revalidate' );
                header ( 'Pragma: public' );
                header ( 'Content-Length: ' . filesize ( $file ));
                ob_clean ();
                flush ();
                readfile ( $file );
                exit;
            }
        }

    验证调用:

    $product_str_start = 'cb05-00000155'; $product_str_end = 'cb05-00000160';
    $press = explode('-',$product_str_start)[0];
    $product_sn_start = explode('-',$product_str_start)[1];
    $product_sn_end = explode('-',$product_str_end)[1];
    $count = $product_sn_end - $product_sn_start;
    for ($i=0;$i<=$count;$i++){
        $product_sn = $press.'-'.substr($product_sn_start+$i+1000000,1,7);
        $Img->makeMergerImg($product_sn);
        $img_arr[$i] = 'upload/pin_code/'.$product_sn.'.png';
    }
    $Img->makeZip('upload/pin_code-0007.zip',$img_arr);
    $Img->download('upload/pin_code-0007.zip');

       

    完整代码:https://github.com/wanggang826/php-do-image

  • 相关阅读:
    js事件
    增量
    文本文件输入(忽略行)
    当数据库的字段为date类型时候
    枚举的使用
    input输入框用el对数字格式化
    图片提交按钮各浏览器不兼容问题
    js对数字的校验
    时间控件
    ymPrompt消息提示组件4.0版 演示及使用简介
  • 原文地址:https://www.cnblogs.com/yimingwang/p/10027612.html
Copyright © 2011-2022 走看看