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

您所在的位置:网站首页 吃撑图片带文字图片 PHP批量生成底部带编号二维码(二维码生成+文字生成图片+图片拼接合并)

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

2024-04-20 09:44| 来源: 网络整理| 查看: 265

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 .= "\n"; } $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 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;$imakeMergerImg($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



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3