《PHP實(shí)戰(zhàn):Thinkphp3.2.3整合phpqrcode生成帶logo的二維碼》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):Thinkphp3.2.3整合phpqrcode生成帶logo的二維碼,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:thinkphp教程
PHP實(shí)例Thinkphp中沒有二維碼相關(guān)的庫(kù),因此我們可以通過整合phpqrcode來完成生成二維碼的功能.
PHP實(shí)例下載phpqrcode
PHP實(shí)例下載地址:http://phpqrcode.sourceforge.net/
PHP實(shí)例整合到Thinkphp框架
PHP實(shí)例在“ThinkPHP\Library\Vendor\”下新建目錄phpqrcode,將壓縮包內(nèi)容解壓到該文件夾下.
PHP實(shí)例調(diào)用phpqrcode生成二維碼
PHP實(shí)例在IndexController控制器下添加如下方法:
PHP實(shí)例
public function qrcode($url="www.baidu.com",$level=3,$size=4)
{
Vendor('phpqrcode.phpqrcode');
$errorCorrectionLevel =intval($level) ;//容錯(cuò)級(jí)別
$matrixPointSize = intval($size);//生成圖片大小
//生成二維碼圖片
$object = new \QRcode();
$object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
}
PHP實(shí)例訪問:http://127.0.0.1/Index/qrcode即可看到生成的二維碼.
PHP實(shí)例生成帶logo的二維碼
PHP實(shí)例先調(diào)用phpqrcode生成一張二維碼,再使用php的image相關(guān)函數(shù)將logo圖片添加到生成的二維碼圖片上.
PHP實(shí)例
include 'phpqrcode.php';
$value = 'http://www.cnblogs.com/txw1958/'; //二維碼內(nèi)容
$errorCorrectionLevel = 'L';//容錯(cuò)級(jí)別
$matrixPointSize = 6;//生成圖片大小
//生成二維碼圖片
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'logo.png';//準(zhǔn)備好的logo圖片
$QR = 'qrcode.png';//已經(jīng)生成的原始二維碼圖
if ($logo !== FALSE) {
$QR = imagecreatefromstring(file_get_contents($QR));
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);//二維碼圖片寬度
$QR_height = imagesy($QR);//二維碼圖片高度
$logo_width = imagesx($logo);//logo圖片寬度
$logo_height = imagesy($logo);//logo圖片高度
$logo_qr_width = $QR_width / 5;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//重新組合圖片并調(diào)整大小
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
//輸出圖片
imagepng($QR, 'helloweixin.png');
echo '<img src="helloweixin.png">';
PHP實(shí)例以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/5099.html