国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP實現的創建帶logo圖標二維碼生成類詳解

2024-05-05 00:04:29
字體:
來源:轉載
供稿:網友

本文實例講述了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

<?phprequire_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;         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

<?phprequire '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 = 'http://m.survivalescaperooms.com/';// 創建二維碼類$oPHPQRCode = new PHPQRCode();// 設定配置$oPHPQRCode->set_config($config);// 創建二維碼$qrcode = $oPHPQRCode->generate($data);// 顯示二維碼echo '<img src="'.$qrcode.'?t='.time().'">';?>

生成的二維碼圖片

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 重庆市| 松滋市| 安国市| 桃园县| 赤城县| 文水县| 通渭县| 太谷县| 格尔木市| 凤台县| 隆昌县| 龙胜| 霸州市| 高阳县| 洛宁县| 固阳县| 汉寿县| 任丘市| 闽侯县| 万盛区| 肃宁县| 余姚市| 乌拉特中旗| 广安市| 农安县| 宽甸| 巴彦淖尔市| 杭州市| 兴化市| 临武县| 红安县| 东兴市| 博罗县| 肥城市| 麻栗坡县| 清远市| 保山市| 靖江市| 孝昌县| 抚顺市| 轮台县|