PHP實現(xiàn)的創(chuàng)建帶logo圖標二維碼生成類詳解
本文實例講述了PHP實現(xiàn)的創(chuàng)建帶logo圖標二維碼生成類。分享給大家供大家參考,具體如下:
這里介紹php實現(xiàn)創(chuàng)建二維碼類,支持設置尺寸,加入LOGO,描邊、圓角、透明度,等處理。提供完整代碼,演示實例及詳細參數(shù)說明,方便大家學習使用。
實現(xiàn)功能如下:
1.創(chuàng)建二維碼
2.加入logo到二維碼中
3.logo可描邊
4.logo可圓角
5.logo可設透明度
6.logo圖片及輸出圖片類型支持png,jpg,gif格式
7.可設置輸出圖片質量
設定參數(shù)說明:
| 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
<?php
require_once dirname(__FILE__)."/qrcode/qrlib.php";
/**
* PHP創(chuàng)建二維碼類
* Date: 2018-03-18
* Author: fdipzone
* Version: 1.0
*
* Description:
* PHP實現(xiàn)創(chuàng)建二維碼類,支持設置尺寸,加入LOGO,圓角,透明度,等處理。
*
* Func:
* public set_config 設定配置
* public generate 創(chuàng)建二維碼
* private create_qrcode 創(chuàng)建純二維碼圖片
* private add_logo 合拼純二維碼圖片與logo圖片
* private image_outline 圖片對象進行描邊
* private image_fillet 圖片對象進行圓角處理
* private imagecopymerge_alpha 合拼圖片并保留各自透明度
* private create_dirs 創(chuàng)建目錄
* 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', // 創(chuàng)建的二維碼路徑
'quality' => 100, // 圖片質量
'logo' => '', // logo路徑,為空表示沒有l(wèi)ogo
'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];
}
}
}
/**
* 創(chuàng)建二維碼
* @param String $data 二維碼內容
* @return String
*/
public function generate($data){
// 創(chuàng)建臨時二維碼圖片
$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'] : '';
}
/**
* 創(chuàng)建臨時二維碼圖片
* @param String $data 二維碼內容
* @return String
*/
private function create_qrcode($data){
// 臨時二維碼圖片
$tmp_qrcode_file = dirname(__FILE__).'/tmp_qrcode_'.time().mt_rand(100,999).'.png';
// 創(chuàng)建臨時二維碼
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){
// 創(chuàng)建目標文件夾
$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'])){
// 創(chuàng)建臨時二維碼圖片對象
$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']);
// 創(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 '';
}
// 設定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);
// 創(chuàng)建底圖對象
$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);
// 合拼圖片與底圖,實現(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);
}
/**
* 對圖片對象進行圓角處理
* @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坐標
$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;
}
/**
* 創(chuàng)建目錄
* @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
<?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 = 'http://chabaoo.cn/';
// 創(chuàng)建二維碼類
$oPHPQRCode = new PHPQRCode();
// 設定配置
$oPHPQRCode->set_config($config);
// 創(chuàng)建二維碼
$qrcode = $oPHPQRCode->generate($data);
// 顯示二維碼
echo '<img src="'.$qrcode.'?t='.time().'">';
?>
生成的二維碼圖片:

源碼下載地址:點擊此處本站下載。
PS:這里再為大家推薦兩款二維碼相關在線工具供大家參考使用:
在線生成二維碼工具(加強版)
http://tools.jb51.net/transcoding/jb51qrcode
在線二維碼解碼識別工具
http://tools.jb51.net/transcoding/trans_qrcode
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據結構與算法教程》、《php程序設計算法總結》、《PHP數(shù)學運算技巧總結》、《php字符串(string)用法總結》及《php常見數(shù)據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
完美解決phpexcel導出到xls文件出現(xiàn)亂碼的問題
下面小編就為大家?guī)硪黄昝澜鉀Qphpexcel導出到xls文件出現(xiàn)亂碼的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
PHP5.5基于mysqli連接MySQL數(shù)據庫和讀取數(shù)據操作實例詳解
這篇文章主要介紹了PHP5.5基于mysqli連接MySQL數(shù)據庫和讀取數(shù)據操作,結合實例形式詳細分析了php5.5使用mysqli連接、讀取mysql數(shù)據庫,以及PDO預處理相關操作技巧,需要的朋友可以參考下2019-02-02
PHP的數(shù)組中提高元素查找與元素去重的效率的技巧解析
這篇文章主要介紹了PHP的數(shù)組中提高元素查找與元素去重的效率的技巧解析,文中對比了相關方法的執(zhí)行速度來總結數(shù)組中使元素查找和去重更加高效的辦法,需要的朋友可以參考下2016-03-03

