侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP实现的创建带logo图标二维码生成类详解

php  /  管理员 发布于 7年前   213

本文实例讲述了PHP实现的创建带logo图标二维码生成类。分享给大家供大家参考,具体如下:

这里介绍php实现创建二维码类,支持设置尺寸,加入LOGO,描边、圆角、透明度,等处理。提供完整代码,演示实例及详细参数说明,方便大家学习使用。

实现功能如下:

1.创建二维码
2.加入logo到二维码中
3.logo可描边
4.logo可圆角
5.logo可设透明度
6.logo图片及输出图片类型支持png,jpg,gif格式
7.可设置输出图片质量

设定参数说明:

ecc 二维码质量 L-smallest, M, Q, H-best
size 二维码尺寸 1-50
dest_file 生成的二维码图片路径
quality 生成的图片质量
logo logo路径,为空表示不加入logo
logo_size logo尺寸,null表示按二维码尺寸比例自动计算
logo_outline_size logo描边尺寸,null表示按logo尺寸按比例自动计算
logo_outline_color logo描边颜色
logo_opacity logo不透明度 0-100
logo_radius logo圆角角度 0-30

代码如下:

PHPQRCode.class.php

 'H',// 二维码质量 L-smallest, M, Q, H-best    'size' => 15,// 二维码尺寸 1-50    'dest_file' => 'qrcode.png',    // 创建的二维码路径    'quality' => 100,          // 图片质量    'logo' => '',// logo路径,为空表示没有logo    'logo_size' => null,        // logo尺寸,null表示按二维码尺寸比例自动计算    'logo_outline_size' => null,    // logo描边尺寸,null表示按logo尺寸按比例自动计算    'logo_outline_color' => '#FFFFFF', // logo描边颜色    'logo_opacity' => 100,       // logo不透明度 0-100    'logo_radius' => 0,         // logo圆角角度 0-30  );  /**   * 设定配置   * @param Array  $config 配置内容   */  public function set_config($config){    // 允许设定的配置    $config_keys = array_keys($this->_config);    // 获取传入的配置,写入设定    foreach($config_keys as $k=>$v){      if(isset($config[$v])){        $this->_config[$v] = $config[$v];      }    }  }  /**   * 创建二维码   * @param String $data 二维码内容   * @return String   */  public function generate($data){    // 创建临时二维码图片    $tmp_qrcode_file = $this->create_qrcode($data);    // 合拼临时二维码图片与logo图片    $this->add_logo($tmp_qrcode_file);    // 删除临时二维码图片    if($tmp_qrcode_file!='' && file_exists($tmp_qrcode_file)){      unlink($tmp_qrcode_file);    }    return file_exists($this->_config['dest_file'])? $this->_config['dest_file'] : '';  }  /**   * 创建临时二维码图片   * @param String $data 二维码内容   * @return String   */  private function create_qrcode($data){    // 临时二维码图片    $tmp_qrcode_file = dirname(__FILE__).'/tmp_qrcode_'.time().mt_rand(100,999).'.png';    // 创建临时二维码    QRcode::png($data, $tmp_qrcode_file, $this->_config['ecc'], $this->_config['size'], 2);    // 返回临时二维码路径    return file_exists($tmp_qrcode_file)? $tmp_qrcode_file : '';  }  /**   * 合拼临时二维码图片与logo图片   * @param String $tmp_qrcode_file 临时二维码图片   */  private function add_logo($tmp_qrcode_file){    // 创建目标文件夹    $this->create_dirs(dirname($this->_config['dest_file']));    // 获取目标图片的类型    $dest_ext = $this->get_file_ext($this->_config['dest_file']);    // 需要加入logo    if(file_exists($this->_config['logo'])){      // 创建临时二维码图片对象      $tmp_qrcode_img = imagecreatefrompng($tmp_qrcode_file);      // 获取临时二维码图片尺寸      list($qrcode_w, $qrcode_h, $qrcode_type) = getimagesize($tmp_qrcode_file);      // 获取logo图片尺寸及类型      list($logo_w, $logo_h, $logo_type) = getimagesize($this->_config['logo']);      // 创建logo图片对象      switch($logo_type){         case 1: $logo_img = imagecreatefromgif($this->_config['logo']); break;         case 2: $logo_img = imagecreatefromjpeg($this->_config['logo']); break;         case 3: $logo_img = imagecreatefrompng($this->_config['logo']); break;         default: return '';       }      // 设定logo图片合拼尺寸,没有设定则按比例自动计算      $new_logo_w = isset($this->_config['logo_size'])? $this->_config['logo_size'] : (int)($qrcode_w/5);      $new_logo_h = isset($this->_config['logo_size'])? $this->_config['logo_size'] : (int)($qrcode_h/5);      // 按设定尺寸调整logo图片      $new_logo_img = imagecreatetruecolor($new_logo_w, $new_logo_h);      imagecopyresampled($new_logo_img, $logo_img, 0, 0, 0, 0, $new_logo_w, $new_logo_h, $logo_w, $logo_h);      // 判断是否需要描边      if(!isset($this->_config['logo_outline_size']) || $this->_config['logo_outline_size']>0){        list($new_logo_img, $new_logo_w, $new_logo_h) = $this->image_outline($new_logo_img);      }      // 判断是否需要圆角处理      if($this->_config['logo_radius']>0){        $new_logo_img = $this->image_fillet($new_logo_img);      }      // 合拼logo与临时二维码      $pos_x = ($qrcode_w-$new_logo_w)/2;      $pos_y = ($qrcode_h-$new_logo_h)/2;      imagealphablending($tmp_qrcode_img, true);      // 合拼图片并保留各自透明度      $dest_img = $this->imagecopymerge_alpha($tmp_qrcode_img, $new_logo_img, $pos_x, $pos_y, 0, 0, $new_logo_w, $new_logo_h, $this->_config['logo_opacity']);      // 生成图片      switch($dest_ext){        case 1: imagegif($dest_img, $this->_config['dest_file'], $this->_config['quality']); break;        case 2: imagejpeg($dest_img, $this->_config['dest_file'], $this->_config['quality']); break;        case 3: imagepng($dest_img, $this->_config['dest_file'], (int)(($this->_config['quality']-1)/10)); break;      }     // 不需要加入logo    }else{      $dest_img = imagecreatefrompng($tmp_qrcode_file);      // 生成图片      switch($dest_ext){        case 1: imagegif($dest_img, $this->_config['dest_file'], $this->_config['quality']); break;        case 2: imagejpeg($dest_img, $this->_config['dest_file'], $this->_config['quality']); break;        case 3: imagepng($dest_img, $this->_config['dest_file'], (int)(($this->_config['quality']-1)/10)); break;      }    }  }  /**   * 对图片对象进行描边   * @param Obj  $img 图片对象   * @return Array   */  private function image_outline($img){    // 获取图片宽高    $img_w = imagesx($img);    $img_h = imagesy($img);    // 计算描边尺寸,没有设定则按比例自动计算    $bg_w = isset($this->_config['logo_outline_size'])? intval($img_w + $this->_config['logo_outline_size']) : $img_w + (int)($img_w/5);    $bg_h = isset($this->_config['logo_outline_size'])? intval($img_h + $this->_config['logo_outline_size']) : $img_h + (int)($img_h/5);    // 创建底图对象    $bg_img = imagecreatetruecolor($bg_w, $bg_h);    // 设置底图颜色    $rgb = $this->hex2rgb($this->_config['logo_outline_color']);    $bgcolor = imagecolorallocate($bg_img, $rgb['r'], $rgb['g'], $rgb['b']);    // 填充底图颜色    imagefill($bg_img, 0, 0, $bgcolor);    // 合拼图片与底图,实现描边效果    imagecopy($bg_img, $img, (int)(($bg_w-$img_w)/2), (int)(($bg_h-$img_h)/2), 0, 0, $img_w, $img_h);    $img = $bg_img;    return array($img, $bg_w, $bg_h);  }  /**   * 对图片对象进行圆角处理   * @param Obj $img 图片对象   * @return Obj   */  private function image_fillet($img){    // 获取图片宽高    $img_w = imagesx($img);    $img_h = imagesy($img);    // 创建圆角图片对象    $new_img = imagecreatetruecolor($img_w, $img_h);    // 保存透明通道    imagesavealpha($new_img, true);    // 填充圆角图片    $bg = imagecolorallocatealpha($new_img, 255, 255, 255, 127);    imagefill($new_img, 0, 0, $bg);    // 圆角半径    $r = $this->_config['logo_radius'];    // 执行圆角处理    for($x=0; $x<$img_w; $x++){      for($y=0; $y<$img_h; $y++){        $rgb = imagecolorat($img, $x, $y);        // 不在图片四角范围,直接画图        if(($x>=$r && $x<=($img_w-$r)) || ($y>=$r && $y<=($img_h-$r))){          imagesetpixel($new_img, $x, $y, $rgb);        // 在图片四角范围,选择画图        }else{          // 上左          $ox = $r; // 圆心x坐标          $oy = $r; // 圆心y坐标          if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){imagesetpixel($new_img, $x, $y, $rgb);          }          // 上右          $ox = $img_w-$r; // 圆心x坐标          $oy = $r;    // 圆心y坐标          if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){imagesetpixel($new_img, $x, $y, $rgb);          }          // 下左          $ox = $r;    // 圆心x坐标          $oy = $img_h-$r; // 圆心y坐标          if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){imagesetpixel($new_img, $x, $y, $rgb);          }          // 下右          $ox = $img_w-$r; // 圆心x坐标          $oy = $img_h-$r; // 圆心y坐标          if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) <= ($r*$r) ){imagesetpixel($new_img, $x, $y, $rgb);          }        }      }    }    return $new_img;  }  // 合拼图片并保留各自透明度  private function imagecopymerge_alpha($dest_img, $src_img, $pos_x, $pos_y, $src_x, $src_y, $src_w, $src_h, $opacity){    $w = imagesx($src_img);    $h = imagesy($src_img);    $tmp_img = imagecreatetruecolor($src_w, $src_h);    imagecopy($tmp_img, $dest_img, 0, 0, $pos_x, $pos_y, $src_w, $src_h);    imagecopy($tmp_img, $src_img, 0, 0, $src_x, $src_y, $src_w, $src_h);    imagecopymerge($dest_img, $tmp_img, $pos_x, $pos_y, $src_x, $src_y, $src_w, $src_h, $opacity);    return $dest_img;  }  /**   * 创建目录   * @param String $path   * @return Boolean   */  private function create_dirs($path){    if(!is_dir($path)){      return mkdir($path, 0777, true);    }    return true;  }  /** hex颜色转rgb颜色   * @param String $color hex颜色   * @return Array   */  private function hex2rgb($hexcolor){    $color = str_replace('#', '', $hexcolor);    if (strlen($color) > 3) {      $rgb = array(        'r' => hexdec(substr($color, 0, 2)),        'g' => hexdec(substr($color, 2, 2)),        'b' => hexdec(substr($color, 4, 2))      );    } else {      $r = substr($color, 0, 1) . substr($color, 0, 1);      $g = substr($color, 1, 1) . substr($color, 1, 1);      $b = substr($color, 2, 1) . substr($color, 2, 1);      $rgb = array(        'r' => hexdec($r),        'g' => hexdec($g),        'b' => hexdec($b)      );    }    return $rgb;  }  /** 获取图片类型    * @param String $file 图片路径    * @return int    */   private function get_file_ext($file){    $filename = basename($file);    list($name, $ext)= explode('.', $filename);    $ext_type = 0;    switch(strtolower($ext)){      case 'jpg':      case 'jpeg':        $ext_type = 2;        break;      case 'gif':        $ext_type = 1;        break;      case 'png':        $ext_type = 3;        break;    }    return $ext_type;  }} // class end?>

demo.php

 'H',  // L-smallest, M, Q, H-best    'size' => 12,  // 1-50    'dest_file' => 'qrcode.png',    'quality' => 90,    'logo' => 'logo.jpg',    'logo_size' => 100,    'logo_outline_size' => 20,    'logo_outline_color' => '#FFFF00',    'logo_radius' => 15,    'logo_opacity' => 100,);// 二维码内容$data = 'https:';// 创建二维码类$oPHPQRCode = new PHPQRCode();// 设定配置$oPHPQRCode->set_config($config);// 创建二维码$qrcode = $oPHPQRCode->generate($data);// 显示二维码echo '';?>

生成的二维码图片:

源码下载地址:点击此处本站下载。

PS:这里再为大家推荐两款二维码相关在线工具供大家参考使用:

在线生成二维码工具(加强版)
http://tools..net.cn/transcoding/jb51qrcode

在线二维码解码识别工具
http://tools..net.cn/transcoding/trans_qrcode

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • 使用PHP生成二维码的两种方法(带logo图像)
  • PHP下通过QRCode类库创建中间带网站LOGO的二维码
  • Thinkphp3.2.3整合phpqrcode生成带logo的二维码
  • PHP基于phpqrcode生成带LOGO图像的二维码实例
  • Laravel使用PHPQRCODE实现生成带有LOGO的二维码图片功能示例
  • php生成带logo二维码方法小结
  • PHP基于phpqrcode类生成二维码的方法详解
  • PHP微信开发之二维码生成类
  • php实现扫描二维码根据浏览器类型访问不同下载地址


  • 上一条:
    PHP使用debug_backtrace方法跟踪调试代码调用详解
    下一条:
    PHP实现的生成唯一RequestID类完整示例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客