登录  /  注册
博主信息
博文 65
粉丝 1
评论 1
访问量 116075
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
tp5 生成二维码
技术宅的博客
原创
1470人浏览过

使用扩展 Endroid\qrcode;

生成二维码 

$qrCode = new QrCode();
$qrCode->setText('Life is too short to be generating QR codes') // 一般设置地址
   ->setSize(300)
   ->setPadding(10)
   ->setErrorCorrection('high')
   ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
   ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
   ->setLabel('Scan the code') // 下面的字 好像不支持中文
   ->setLabelFontSize(16)
   ->setImageType(QrCode::IMAGE_TYPE_PNG);
header('Content-Type: '.$qrCode->getContentType());
$qrCode->render(); // 渲染图片 需要加上exit; 不然就是乱码
$qrCode->save('qrcode.png');  // 图片保存路径  需要保存就用这个 
exit;

生成海报二维码(组装图片)

$qrCode = new QrCode();
      $qrCode->setText('Life is too short to be generating QR codes') // 一般设置地址
         ->setSize(180)
         ->setPadding(10)
         ->setErrorCorrection('high')
         ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
         ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
//       ->setLabel('Scan the code') // 下面的字 好像不支持中文
         ->setLabelFontSize(16)
         ->setImageType(QrCode::IMAGE_TYPE_PNG);
      header('Content-Type: '.$qrCode->getContentType());
      $content=".\upload\shareqc\qrcodes.png";
      $qrCode->save($content);  // 图片保存路径
      // 组装合成图片 生成海报
      $path_1 = '.\upload\shareqc\1.jpeg';  // 背景图片
      $path_2 = $content; // 二维码图片
      $image_1 = imagecreatefromjpeg($path_1); // 注意图片后缀
      $image_2 = imagecreatefrompng($path_2); // 注意图片后缀
      $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);
      imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
      imagecopymerge($image_3, $image_2, 260, 410, 0, 0, imagesx($image_2), imagesy($image_2), 100);
      // 调整图片位置 左上方就是0.0 位置 自己根据大小设置 260.410
      $img_url = './upload/shareqc/' . time() . ".jpeg"; // 生成图片名字地址
      $res = imagejpeg($image_3, $img_url); // 存储图片是否成功  成功返回true  以上海报制作完成  可以添加文字 如下面所示

给海报添加文字

putenv('GDFONTPATH=' . realpath('.')); //设置字体路径
$qrCode = new QrCode();
      $qrCode->setText('Life is too short to be generating QR codes') // 一般设置地址
         ->setSize(180)
         ->setPadding(10)
         ->setErrorCorrection('high')
         ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
         ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
//       ->setLabel('Scan the code') // 下面的字 好像不支持中文
         ->setLabelFontSize(16)
         ->setImageType(QrCode::IMAGE_TYPE_PNG);
      header('Content-Type: '.$qrCode->getContentType());
      $content=".\upload\shareqc\qrcodes.png";
      $qrCode->save($content);  // 图片保存路径
      // 组装合成图片 生成海报
      $path_1 = '.\upload\shareqc\1.jpeg';  // 背景图片
      $path_2 = $content; // 二维码图片
      $image_1 = imagecreatefromjpeg($path_1); // 注意图片后缀
      $image_2 = imagecreatefrompng($path_2); // 注意图片后缀
      $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);
      imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));
      imagecopymerge($image_3, $image_2, 260, 410, 0, 0, imagesx($image_2), imagesy($image_2), 100);
      // 调整图片位置 左上方就是0.0 位置 自己根据大小设置 260.410
      $img_url = './upload/shareqc/' . time() . ".jpeg"; // 生成图片名字地址
      $res = imagejpeg($image_3, $img_url); // 存储图片是否成功  成功返回true  
     //  添加文字
      $dst = imagecreatefromstring(file_get_contents($img_url));
      /*操作图片*/
      //1.设置字体的路径
      $font = "msyh.ttf";  // 放在piblic 下面 最前面加上这句话设置路径的putenv('GDFONTPATH=' . realpath('.')); //设置字体路径
      //4.写入文字
      // 画布资源 字体大小 旋转角度 x轴 y轴 字体颜色 字体文件 需要渲染的字符串
      $black = imagecolorallocate($dst, 255, 255, 255); //字体颜色
      imagefttext($dst, 30, 0, 380, 1300, $black, $font, '扫一扫,关注我们');
      $img_add_url = './upload/shareqc/' . time() . ".jpeg";
      $res = imagejpeg($dst, $img_add_url);

大致效果  网上随便找的图 将就用一下 

1.jpegqrcodes.png

1568107010.jpeg


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学