温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么创建带logo图标的二维码

发布时间:2021-05-27 16:32:28 阅读:151 作者:Leah 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章将为大家详细讲解有关使用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生成的图片质量
logologo路径,为空表示不加入logo
logo_sizelogo尺寸,null表示按二维码尺寸比例自动计算
logo_outline_sizelogo描边尺寸,null表示按logo尺寸按比例自动计算
logo_outline_colorlogo描边颜色
logo_opacitylogo不透明度 0-100
logo_radiuslogo圆角角度 0-30

代码如下:

PHPQRCode.class.php

<?php
require_once dirname(__FILE__)."/qrcode/qrlib.php";
/**
 * PHP创建二维码类
 * Date:  2018-03-18
 * Author: fdipzone
 * Version: 1.0
 *
 * Description:
 * PHP实现创建二维码类,支持设置尺寸,加入LOGO,圆角,透明度,等处理。
 *
 * Func:
 * public set_config      设定配置
 * public generate       创建二维码
 * private create_qrcode    创建纯二维码图片
 * private add_logo       合拼纯二维码图片与logo图片
 * private image_outline    图片对象进行描边
 * private image_fillet     图片对象进行圆角处理
 * private imagecopymerge_alpha 合拼图片并保留各自透明度
 * private create_dirs     创建目录
 * private hex2rgb       hex颜色转rgb颜色
 * private get_file_ext     获取图片类型
 */
class PHPQRCode// class start
  /** 默认设定 */
  private $_config array(
    'ecc' => '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; 
        defaultreturn ''; 
      }
      // 设定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_img0000$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_imgtrue);
      // 合拼图片并保留各自透明度
      $dest_img $this->imagecopymerge_alpha($tmp_qrcode_img$new_logo_img$pos_x$pos_y00$new_logo_w$new_logo_h$this->_config['logo_opacity']);
      // 生成图片
      switch($dest_ext){
        case 1imagegif($dest_img$this->_config['dest_file'], $this->_config['quality']); break;
        case 2imagejpeg($dest_img$this->_config['dest_file'], $this->_config['quality']); break;
        case 3imagepng($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 1imagegif($dest_img$this->_config['dest_file'], $this->_config['quality']); break;
        case 2imagejpeg($dest_img$this->_config['dest_file'], $this->_config['quality']); break;
        case 3imagepng($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_img00$bgcolor);
    // 合拼图片与底图,实现描边效果
    imagecopy($bg_img$img, (int)(($bg_w-$img_w)/2), (int)(($bg_h-$img_h)/2), 00$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_imgtrue);
    // 填充圆角图片
    $bg imagecolorallocatealpha($new_img255255255127);
    imagefill($new_img00$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_img00$pos_x$pos_y$src_w$src_h);
    imagecopy($tmp_img$src_img00$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($path0777true);
    }
    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($color02)),
        'g' => hexdec(substr($color22)),
        'b' => hexdec(substr($color42))
      );
    } else {
      $r substr($color01) . substr($color01);
      $g substr($color11) . substr($color11);
      $b substr($color21) . substr($color21);
      $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

<?php
require 'PHPQRCode.class.php';
$config array(
    'ecc' => '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://www.jb51.net/';
// 创建二维码类
$oPHPQRCode new PHPQRCode();
// 设定配置
$oPHPQRCode->set_config($config);
// 创建二维码
$qrcode $oPHPQRCode->generate($data);
// 显示二维码
echo '<img src="'.$qrcode.'?t='.time().'">';
?>

生成的二维码图片:

关于使用PHP怎么创建带logo图标的二维码就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI

开发者交流群×