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 .= "\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 <= 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
转载于:https://www.cnblogs.com/yimingwang/p/10027612.html
PHP批量生成底部带编号二维码(二维码生成+文字生成图片+图片拼接合并)相关推荐
- java生成word 带表格_【java】Freemarker 动态生成word(带图片表格)
1.添加freemarker.jar 到java项目. 2.新建word文档. 3.将文档另存为xml 格式. 4.将xml格式化后打开编辑(最好用notepad,有格式),找到需要替换的内容,将内容 ...
- matlab voronoi 多晶体程序,【干货】二维及三维voronoi泰森多边形生成及其批量cohesive的插入...
原标题:[干货]二维及三维voronoi泰森多边形生成及其批量cohesive的插入 目前voronoi晶粒模型已经被广泛应用,材料的穿晶断裂和沿晶断裂是失效的两种主要表现形式,建立晶粒模型是分析材料 ...
- (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
场景:移动支付需要对二维码的生成与部署有所了解,掌握目前主流的二维码生成技术. 1 ZXing 生成二维码 首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老 ...
- asp微信会员积分上下级团队注册,带参数推荐人的二维码,分销等级会员生成二维码海报系统
昨天一个好友来电话让帮忙做一个程序,他的老板让他做一个能扫码注册的会员系统,注册会员必须要推荐人的二维码扫才可以注册,这就是需要带参数的二维码了,还要生成宣传海报,还要生成会员自已的二维码,还要带积分 ...
- 2023最新在线批量生成二维码网站源码+全开源/UI简约
正文: 在线批量生成二维码网站源码,直接拖服务器就能运行php7.3,程序采用本地接口支持生成接口,生成后自动保存,生成后压缩保存,具体功能可以自己测试,源码无加密. 程序: wwxhes.lanzo ...
- 批量生成二维码系统源码 电脑+手机自适应代码 含安装搭建教程
分享一个批量生成二维码系统源码,一键批量生成包括网址,数字,文字,视频等各种形式的二维码,自动生成压缩包,一键下载.电脑+手机自适应代码,含安装搭建教程. 批量生成二维码系统源码帮助用户快速生成二维码 ...
- TP5使用二维码PHP QR Code生成带LOGO和不带LOGO的二维码
TP5使用二维码PHP QR Code生成带LOGO和不带LOGO的二维码 1.下载二维码插件Phpqrcode,地址 https://sourceforge.net/projects/phpqrco ...
- vue使用qrcodejs2生成带log的二维码图片,vue生成二维码图片中间带log,自定义log
安装插件 npm install qrcodejs2 --save 在页面中引入 import QRcode from 'qrcodejs2' 普通的二维码 此处的id就是页面中要展示二维码容器的id ...
- ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老美开发的,barcode4j对一维条形码处理的很好,而且支持的格式很多,当然也可以对二维码进行处理,效果个人 ...
最新文章
- 572. Subtree of Another Tree
- Data Guard出现gap sequence修复
- 数据库Mysql的学习(六)-子查询和多表操作
- python url中传递中文_Python编程:URL网址链接中的中文编码与解码
- TreeView递归系统目录
- 【软考高项】信息系统项目管理师 论文写作技巧分享 (上)
- windows下将磁盘脱机,并在我的电脑下显示
- 我会回来的!我很想念大家!
- 值类型与引用类型传递的艺术
- 颜色空间直方图matlab,使用Matlab绘制图像的rgb颜色空间和Lab颜色空间分量图和分量直方图 | 学步园...
- Vue3.x 推荐使用 mitt.js
- TouchJSON的简单使用
- 基于Vue3在线商城(Vue3+VueCLI+VueRouter+vuex+axios+Bootstrap)
- Mac Book文件夹加密
- 密度图与等高线图——Note_5
- mysql干嘛的_mysql和sql是干什么的?
- 嵌入式岗位Makefile常见面试题(1)
- 基于人工智能推理的英特尔® 精选解决方案
- 【论文笔记】Encoding cloth manipulations using a graph of states and transitions
- 真正的操盘手,应该集军人、商人、诗人、僧人的特质于一身