<?php
require_once './phpqrcode/phpqrcode.php';
/*
* 地址:http://phpqrcode.sourceforge.net/ 下載qrcode類
* @param string $url 要生成的連接
* @param bool $local 是否生成本地文件
* @param string $logo 中間圖片地址
*/
echo qrcode('https://www.cnblogs.com/mengor/p/8192642.html',$local=true,$logo="./_img/20170713165304.png");
function qrcode($url,$local,$logo){
$value = $url; //二維碼內容 連接
$errorCorrectionLevel = 'M';//容錯級別
$matrixPointSize = 8;//生成圖片大小
if(!$local){
QRcode::png($value,false, $errorCorrectionLevel, $matrixPointSize, 2);
}else{
$abs_url = './_img/'; //保存的二維碼路徑
$qr_dir= $abs_url.microtime().'.png'; //保存的二維碼名字
//生成二維碼圖片
QRcode::png($value, $qr_dir, $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $qr_dir;//已經生成的原始二維碼圖
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;
//從新組合圖片並調整大小
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
//輸出圖片
$he_dir= $abs_url.microtime().'.png';//重組圖片路徑
imagepng($QR, $he_dir);
return '<img src="'.$he_dir.'" alt="">';
}else{
return '<img src="'.$qr_dir.'" alt="">';
}
}
}