<?php
namespace qrcode;
class Image
{//生成二维码图片public function makeCodeImg($url, $product_sn = '2018**82019'){$url = $url . '/' . $product_sn . '?code_sn=' . $product_sn . '&code_type=product';$path = './upload/product_qr_code';if (!is_dir($path)) {mkdir($path, 0777, true);}// include_once 'phpqrcode/phpqrcode.php';Vendor('phpqrcode.phpqrcode');$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');}//文字生成图片public function makeImgWithStr($filename,$text,$font_size=20,$font = './font/Arial/arial.ttf'){//var_dump($filename);var_dump($text);exit;     //图片尺寸$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; //文字居中//  var_dump($x);var_dump($y);//   var_dump($content);exit;// echo $x;die;//文字写入imagettftext($im, $font_size, 0, $x, $y, $black, $font, $content); //写 TTF 文字到图中//图片保存imagejpeg($im, $filename);}//图片加文字书印public function addTxetForImg($path, $text = '加棉', $size = '15'){//字体类型$font = "font/Arial/simsun.ttc";$img = imagecreatefromjpeg($path);// 加载已有图像//给图片分配颜色// imagecolorallocate($img, 0xff, 0xcc, 0xcc);//设置字体颜色$black = imagecolorallocate($img, 255, 0, 0);//将ttf文字写到图片中imagettftext($img, $size, 0, 15, 15, $black, $font, html_entity_decode($text));// ImagePNG($img, "upload/documents/new".time().".jpg");imagejpeg($img, "upload/new" . time() . ".jpg");}//合并图片,融合合并public function merageImg($file_1, $file_2, $re_file){// $file_1 = "upload/product_qr_code/cb05-000002.jpg";// $file_2 = "upload/product_qr_code/cb05-1311.jpg";//将两张图片分别取到两个画布中$image_1 = imagecreatefrompng($file_1);$image_2 = imagecreatefromjpeg($file_2);//创建一个和大图一样大小的真彩色画布(ps:只有这样才能保证后面copy装备图片的时候不会失真)$image_3 = imageCreatetruecolor(imagesx($image_1), imagesy($image_1));//为真彩色画布创建白色背景,再设置为透明$color = imagecolorallocate($image_3, 255, 255, 255);imagefill($image_3, 0, 0, $color);imageColorTransparent($image_3, $color);//首先将大图画布采样copy到真彩色画布中,不会失真imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));//再将小图图片copy到已经具有人物图像的真彩色画布中,同样也不会失真imagecopymerge($image_3, $image_2, 150, 150, 0, 0, imagesx($image_2), imagesy($image_2), 100);//将画布保存到指定的gif文件// imagegif($image_3);imagejpeg($image_3, $re_file . time() . ".jpg");}//获取拼接图片高度public function allImgHeight($arr, $width){$height = 0;if (count($arr) == count($arr, 1)) {  //一位数组的计算foreach ($arr as $key => $value) {$info = getimagesize($value);$height += $width / $info[0] * $info[1];}} else {foreach ($arr as $key => $value) {  //二维数组的计算foreach ($value as $k => $v) {$info = getimagesize($v);$height += $width / $info[0] * $info[1];}}}return $height;}//图片等宽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);}/*** 合并图片,拼接合并* @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 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的文件数组// makeZip('upload/product_qr_code/product_qr_code.zip',['upload/product_qr_code/cb01-000001-.jpg','upload/product_qr_code/cb01-000002-.jpg']);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;}}}

验证下载合并二维码

<?php
namespace app\admin\controller;
use think\Loader;
use think\Controller;
class Image extends Controller
{public  function  index(){//include 'Image.php';//发送校验码Loader::import('qrcode\Image', EXTEND_PATH);$Img=new \qrcode\Image();// $Img = new Image();cb05-000155$product_str_start = 'cb05-00000155'; $product_str_end = 'cb05-00000156';$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');} }

验证结果

注意服务器要有对应的arial.ttf字体才可以显示二维码下方的文字

php拼接二维码,文字和二维码进行合并相关推荐

  1. java生成二维码 推广海报添加二维码 文字水印 二维码添加LOGO

    前言 场景: 一.推广海报贴上二维码,用户扫码跳转             二.二维码中间贴logo   eg:这里使用展示第一种场景 一.使用工具 Google开源项目ZXing(二维条码编解码). ...

  2. PHP多文字,二维码(动态、非动态)生成海报方式

    PHP多文字,二维码(动态(支持带logo).非动态)生成海报方式 1.下载二维码插件Phpqrcode,地址 [https://sourceforge.net/projects/phpqrcode/ ...

  3. 附带有背景图、文字的二维码

    前端生成附带有背景图.可添加文字的二维码 下载 npm install jr-qrcod --save 引入 const qrcode = require('jr-qrcode'); 使用 <i ...

  4. C#生成带背景和文字的二维码图片

    /// <summary>         /// 生成带背景和文字的二维码图片         /// </summary>         /// <param na ...

  5. python查找文字在图片中的位置_图片转文字、二维码互转链接、查找不懂写的字...

    我们是一个为大家搜罗一些简单.常用又免费的办公应用的公众号,主要有识别图片中的字体转文字:二维码转链接,链接生成二维码:以及当你不懂写某个字时,通过查词组的方式找出那个字. 1.图片转文字 有时候我们 ...

  6. 视频图片加文字的二维码怎么做?教你在线制作二维码

    想要把视频.图片以及文字做成二维码的时候,要怎么操作呢?其实,方法非常的简单,只需要使用操作简单的二维码生成器(https://www.jzx.com/)就能够快速完成二维码制作的操作.下面,给大家分 ...

  7. java生成文字二维码、url二维码

    java生成文字二维码.url二维码 pom: 1)生成文字二维码 java工具类: 2)url地址生成二维码 java工具类: pom: <dependency><groupId& ...

  8. java将一个url链接或者文字生成二维码并且转成base64

    我们在开发的时候,肯定有这样的需求.把一个url链接转成二维码图片.提供给用户扫描,然后跳转到相应的页面. 三个问题(前提:没有用统一的图片服务器如:fastdfs.) (1):把url链接转成图片保 ...

  9. 微信小程序开发:文字转二维码

    使用开源组件:weapp-qrcode-canvas-2d 亲测渲染性能,生成速度等均优于其他小程序实现方式,虽然star较少,但是目前没发现什么问题 以下内容摘自github项目readme.md文 ...

  10. 二维码学习笔记(二) | 数据分析与数据编码

    唠唠闲话 二维码笔记系列(原文地址): 『二维码学习笔记(一) | 二维码概述』 『二维码学习笔记(二) | 数据分析与数据编码』 『二维码学习笔记(三) | 纠错编码』 『二维码学习笔记(四) | ...

最新文章

  1. 面试必备:Java线程池解析
  2. 一个关于解决序列化问题的编程技巧
  3. 核PCA——从理论到实现
  4. maven添加子工程_重量级!Maven史上最全教程,看了必懂
  5. 如何在 ASP.Net Core 中使用 NCache
  6. 3.5. Ticket
  7. 团队开发——用户需求报告
  8. TextView常用属性设置
  9. 《金字塔原理》学习笔记 | 第1篇—表达的逻辑
  10. ab网站压力测试命令的参数、输出结果的中文注解
  11. 中文文本分析(matplotlib的库的应用)
  12. python数据挖掘母亲和孩子身高预测_孩子身高预测公式 靠谱指数高达80分哦!
  13. 2013年12月安徽省广播电台网络影响力排名
  14. 基于深度卷积神经网络的图像超分辨率重建(SRCNN) 学习笔记
  15. ArcGIS API for javascript创建二维度地图
  16. Gremlin 基础知识
  17. 通过两个实例来理解 devtool: 'source-map' 是什么意思
  18. Android APP启动时出现白屏或者黑屏怎么办?
  19. [渝粤教育] 四川大学 模拟电子技术基础(Ⅰ) 参考 资料
  20. Java 每年节假日获取

热门文章

  1. Java的第20年:Java和我的故事
  2. 漏洞分析C#反编译软件Reflector 11.1.0.2167(最新版)(附补丁下载)
  3. 服务器bios 虚拟化,hp服务器bios开启虚拟化(hp主板开启虚拟化)
  4. java非主流火星文输入法_火星文输入法
  5. php网站开发实例教程源码,PHP+MYSQL网站开发全程实例
  6. Foobar2000自用插件
  7. \t\t超星pdg转PDF文档之虚拟打印法
  8. 引用 CSS+DIV/Ul+LI/dl+dt+dd/tabale+css 样式
  9. 分享80个贺卡图片PHP源码,总有一款适合你
  10. Silverlight实现文件的下载[很简单]