主頁 > 知識庫 > PHP實(shí)現(xiàn)的創(chuàng)建帶logo圖標(biāo)二維碼生成類詳解

PHP實(shí)現(xiàn)的創(chuàng)建帶logo圖標(biāo)二維碼生成類詳解

熱門標(biāo)簽:Linux服務(wù)器 電子圍欄 科大訊飛語音識別系統(tǒng) 服務(wù)器配置 銀行業(yè)務(wù) 阿里云 Mysql連接數(shù)設(shè)置 團(tuán)購網(wǎng)站

本文實(shí)例講述了PHP實(shí)現(xiàn)的創(chuàng)建帶logo圖標(biāo)二維碼生成類。分享給大家供大家參考,具體如下:

這里介紹php實(shí)現(xiàn)創(chuàng)建二維碼類,支持設(shè)置尺寸,加入LOGO,描邊、圓角、透明度,等處理。提供完整代碼,演示實(shí)例及詳細(xì)參數(shù)說明,方便大家學(xué)習(xí)使用。

實(shí)現(xiàn)功能如下:

1.創(chuàng)建二維碼
2.加入logo到二維碼中
3.logo可描邊
4.logo可圓角
5.logo可設(shè)透明度
6.logo圖片及輸出圖片類型支持png,jpg,gif格式
7.可設(shè)置輸出圖片質(zhì)量

設(shè)定參數(shù)說明:

ecc 二維碼質(zhì)量 L-smallest, M, Q, H-best
size 二維碼尺寸 1-50
dest_file 生成的二維碼圖片路徑
quality 生成的圖片質(zhì)量
logo logo路徑,為空表示不加入logo
logo_size logo尺寸,null表示按二維碼尺寸比例自動(dòng)計(jì)算
logo_outline_size logo描邊尺寸,null表示按logo尺寸按比例自動(dòng)計(jì)算
logo_outline_color logo描邊顏色
logo_opacity logo不透明度 0-100
logo_radius logo圓角角度 0-30

代碼如下:

PHPQRCode.class.php

?php
require_once dirname(__FILE__)."/qrcode/qrlib.php";
/**
 * PHP創(chuàng)建二維碼類
 * Date:  2018-03-18
 * Author: fdipzone
 * Version: 1.0
 *
 * Description:
 * PHP實(shí)現(xiàn)創(chuàng)建二維碼類,支持設(shè)置尺寸,加入LOGO,圓角,透明度,等處理。
 *
 * Func:
 * public set_config      設(shè)定配置
 * public generate       創(chuàng)建二維碼
 * private create_qrcode    創(chuàng)建純二維碼圖片
 * private add_logo       合拼純二維碼圖片與logo圖片
 * private image_outline    圖片對象進(jìn)行描邊
 * private image_fillet     圖片對象進(jìn)行圓角處理
 * private imagecopymerge_alpha 合拼圖片并保留各自透明度
 * private create_dirs     創(chuàng)建目錄
 * private hex2rgb       hex顏色轉(zhuǎn)rgb顏色
 * private get_file_ext     獲取圖片類型
 */
class PHPQRCode{ // class start
  /** 默認(rèn)設(shè)定 */
  private $_config = array(
    'ecc' => 'H',            // 二維碼質(zhì)量 L-smallest, M, Q, H-best
    'size' => 15,            // 二維碼尺寸 1-50
    'dest_file' => 'qrcode.png',    // 創(chuàng)建的二維碼路徑
    'quality' => 100,          // 圖片質(zhì)量
    'logo' => '',            // logo路徑,為空表示沒有l(wèi)ogo
    'logo_size' => null,        // logo尺寸,null表示按二維碼尺寸比例自動(dòng)計(jì)算
    'logo_outline_size' => null,    // logo描邊尺寸,null表示按logo尺寸按比例自動(dòng)計(jì)算
    'logo_outline_color' => '#FFFFFF', // logo描邊顏色
    'logo_opacity' => 100,       // logo不透明度 0-100
    'logo_radius' => 0,         // logo圓角角度 0-30
  );
  /**
   * 設(shè)定配置
   * @param Array  $config 配置內(nèi)容
   */
  public function set_config($config){
    // 允許設(shè)定的配置
    $config_keys = array_keys($this->_config);
    // 獲取傳入的配置,寫入設(shè)定
    foreach($config_keys as $k=>$v){
      if(isset($config[$v])){
        $this->_config[$v] = $config[$v];
      }
    }
  }
  /**
   * 創(chuàng)建二維碼
   * @param String $data 二維碼內(nèi)容
   * @return String
   */
  public function generate($data){
    // 創(chuàng)建臨時(shí)二維碼圖片
    $tmp_qrcode_file = $this->create_qrcode($data);
    // 合拼臨時(shí)二維碼圖片與logo圖片
    $this->add_logo($tmp_qrcode_file);
    // 刪除臨時(shí)二維碼圖片
    if($tmp_qrcode_file!=''  file_exists($tmp_qrcode_file)){
      unlink($tmp_qrcode_file);
    }
    return file_exists($this->_config['dest_file'])? $this->_config['dest_file'] : '';
  }
  /**
   * 創(chuàng)建臨時(shí)二維碼圖片
   * @param String $data 二維碼內(nèi)容
   * @return String
   */
  private function create_qrcode($data){
    // 臨時(shí)二維碼圖片
    $tmp_qrcode_file = dirname(__FILE__).'/tmp_qrcode_'.time().mt_rand(100,999).'.png';
    // 創(chuàng)建臨時(shí)二維碼
    QRcode::png($data, $tmp_qrcode_file, $this->_config['ecc'], $this->_config['size'], 2);
    // 返回臨時(shí)二維碼路徑
    return file_exists($tmp_qrcode_file)? $tmp_qrcode_file : '';
  }
  /**
   * 合拼臨時(shí)二維碼圖片與logo圖片
   * @param String $tmp_qrcode_file 臨時(shí)二維碼圖片
   */
  private function add_logo($tmp_qrcode_file){
    // 創(chuàng)建目標(biāo)文件夾
    $this->create_dirs(dirname($this->_config['dest_file']));
    // 獲取目標(biāo)圖片的類型
    $dest_ext = $this->get_file_ext($this->_config['dest_file']);
    // 需要加入logo
    if(file_exists($this->_config['logo'])){
      // 創(chuàng)建臨時(shí)二維碼圖片對象
      $tmp_qrcode_img = imagecreatefrompng($tmp_qrcode_file);
      // 獲取臨時(shí)二維碼圖片尺寸
      list($qrcode_w, $qrcode_h, $qrcode_type) = getimagesize($tmp_qrcode_file);
      // 獲取logo圖片尺寸及類型
      list($logo_w, $logo_h, $logo_type) = getimagesize($this->_config['logo']);
      // 創(chuàng)建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 ''; 
      }
      // 設(shè)定logo圖片合拼尺寸,沒有設(shè)定則按比例自動(dòng)計(jì)算
      $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);
      // 按設(shè)定尺寸調(diào)整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與臨時(shí)二維碼
      $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;
      }
    }
  }
  /**
   * 對圖片對象進(jìn)行描邊
   * @param Obj  $img 圖片對象
   * @return Array
   */
  private function image_outline($img){
    // 獲取圖片寬高
    $img_w = imagesx($img);
    $img_h = imagesy($img);
    // 計(jì)算描邊尺寸,沒有設(shè)定則按比例自動(dòng)計(jì)算
    $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);
    // 創(chuàng)建底圖對象
    $bg_img = imagecreatetruecolor($bg_w, $bg_h);
    // 設(shè)置底圖顏色
    $rgb = $this->hex2rgb($this->_config['logo_outline_color']);
    $bgcolor = imagecolorallocate($bg_img, $rgb['r'], $rgb['g'], $rgb['b']);
    // 填充底圖顏色
    imagefill($bg_img, 0, 0, $bgcolor);
    // 合拼圖片與底圖,實(shí)現(xiàn)描邊效果
    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);
  }
  /**
   * 對圖片對象進(jìn)行圓角處理
   * @param Obj $img 圖片對象
   * @return Obj
   */
  private function image_fillet($img){
    // 獲取圖片寬高
    $img_w = imagesx($img);
    $img_h = imagesy($img);
    // 創(chuàng)建圓角圖片對象
    $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'];
    // 執(zhí)行圓角處理
    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坐標(biāo)
          $oy = $r; // 圓心y坐標(biāo)
          if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) = ($r*$r) ){
            imagesetpixel($new_img, $x, $y, $rgb);
          }
          // 上右
          $ox = $img_w-$r; // 圓心x坐標(biāo)
          $oy = $r;    // 圓心y坐標(biāo)
          if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) = ($r*$r) ){
            imagesetpixel($new_img, $x, $y, $rgb);
          }
          // 下左
          $ox = $r;    // 圓心x坐標(biāo)
          $oy = $img_h-$r; // 圓心y坐標(biāo)
          if( ( ($x-$ox)*($x-$ox) + ($y-$oy)*($y-$oy) ) = ($r*$r) ){
            imagesetpixel($new_img, $x, $y, $rgb);
          }
          // 下右
          $ox = $img_w-$r; // 圓心x坐標(biāo)
          $oy = $img_h-$r; // 圓心y坐標(biāo)
          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;
  }
  /**
   * 創(chuàng)建目錄
   * @param String $path
   * @return Boolean
   */
  private function create_dirs($path){
    if(!is_dir($path)){
      return mkdir($path, 0777, true);
    }
    return true;
  }
  /** hex顏色轉(zhuǎn)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

?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,
);
// 二維碼內(nèi)容
$data = 'https://www.jb51.net/';
// 創(chuàng)建二維碼類
$oPHPQRCode = new PHPQRCode();
// 設(shè)定配置
$oPHPQRCode->set_config($config);
// 創(chuàng)建二維碼
$qrcode = $oPHPQRCode->generate($data);
// 顯示二維碼
echo 'img src="'.$qrcode.'?t='.time().'">';
?>

生成的二維碼圖片:

源碼下載地址:點(diǎn)擊此處本站下載

PS:這里再為大家推薦兩款二維碼相關(guān)在線工具供大家參考使用:

在線生成二維碼工具(加強(qiáng)版)
http://tools.jb51.net/transcoding/jb51qrcode

在線二維碼解碼識別工具
http://tools.jb51.net/transcoding/trans_qrcode

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • PHP生成二維碼與識別二維碼的方法詳解【附源碼下載】
  • PHP生成(支持多模板)二維碼海報(bào)代碼
  • PHP基于phpqrcode類生成二維碼的方法詳解
  • php實(shí)現(xiàn)生成帶二維碼圖片并強(qiáng)制下載功能
  • 微信小程序 PHP生成帶參數(shù)二維碼
  • php生成二維碼圖片方法匯總
  • php微信高級接口調(diào)用方法(自定義菜單接口、客服接口、二維碼)
  • php+laravel 掃碼二維碼簽到功能

標(biāo)簽:江蘇 衡水 棗莊 萍鄉(xiāng) 大理 蚌埠 衢州 廣元

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)現(xiàn)的創(chuàng)建帶logo圖標(biāo)二維碼生成類詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266