PHP實(shí)現(xiàn)的創(chuàng)建帶logo圖標(biāo)二維碼生成類詳解
本文實(shí)例講述了PHP實(shí)現(xiàn)的創(chuàng)建帶logo圖標(biāo)二維碼生成類。分享給大家供大家參考,具體如下:
這里介紹php實(shí)現(xiàn)創(chuàng)建二維碼類,支持設(shè)置尺寸,加入LOGO,描邊、圓角、透明度,等處理。提供完整代碼,演示實(shí)例及詳細(xì)參數(shù)說(shuō)明,方便大家學(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ù)說(shuō)明:
| 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 圖片對(duì)象進(jìn)行描邊
* private image_fillet 圖片對(duì)象進(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路徑,為空表示沒(méi)有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);
// 獲取傳入的配置,寫(xiě)入設(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í)二維碼圖片對(duì)象
$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圖片對(duì)象
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圖片合拼尺寸,沒(méi)有設(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;
}
}
}
/**
* 對(duì)圖片對(duì)象進(jìn)行描邊
* @param Obj $img 圖片對(duì)象
* @return Array
*/
private function image_outline($img){
// 獲取圖片寬高
$img_w = imagesx($img);
$img_h = imagesy($img);
// 計(jì)算描邊尺寸,沒(méi)有設(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)建底圖對(duì)象
$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);
}
/**
* 對(duì)圖片對(duì)象進(jìn)行圓角處理
* @param Obj $img 圖片對(duì)象
* @return Obj
*/
private function image_fillet($img){
// 獲取圖片寬高
$img_w = imagesx($img);
$img_h = imagesy($img);
// 創(chuàng)建圓角圖片對(duì)象
$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);
// 不在圖片四角范圍,直接畫(huà)圖
if(($x>=$r && $x<=($img_w-$r)) || ($y>=$r && $y<=($img_h-$r))){
imagesetpixel($new_img, $x, $y, $rgb);
// 在圖片四角范圍,選擇畫(huà)圖
}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 = 'http://www.dbjr.com.cn/';
// 創(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
在線二維碼解碼識(shí)別工具
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常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
通過(guò)PHP實(shí)現(xiàn)獲取訪問(wèn)用戶IP
這篇文章主要介紹了通過(guò)PHP實(shí)現(xiàn)獲取訪問(wèn)用戶IP,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
php安全配置記錄和常見(jiàn)錯(cuò)誤梳理(總結(jié))
下面小編就為大家?guī)?lái)一篇php安全配置記錄和常見(jiàn)錯(cuò)誤梳理(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
完美解決phpexcel導(dǎo)出到xls文件出現(xiàn)亂碼的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決phpexcel導(dǎo)出到xls文件出現(xiàn)亂碼的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
PHP5.5基于mysqli連接MySQL數(shù)據(jù)庫(kù)和讀取數(shù)據(jù)操作實(shí)例詳解
這篇文章主要介紹了PHP5.5基于mysqli連接MySQL數(shù)據(jù)庫(kù)和讀取數(shù)據(jù)操作,結(jié)合實(shí)例形式詳細(xì)分析了php5.5使用mysqli連接、讀取mysql數(shù)據(jù)庫(kù),以及PDO預(yù)處理相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
PHP的數(shù)組中提高元素查找與元素去重的效率的技巧解析
這篇文章主要介紹了PHP的數(shù)組中提高元素查找與元素去重的效率的技巧解析,文中對(duì)比了相關(guān)方法的執(zhí)行速度來(lái)總結(jié)數(shù)組中使元素查找和去重更加高效的辦法,需要的朋友可以參考下2016-03-03

