<?php
/**
php使用二維碼
**/
class MyQrcode{
const SIZE = 150;
const LEVEL = "L";
const MARGIN = 4;
/**
logo 存放logo文件的地址
工廠方法建立二維碼
**/
public static function factoryCreateQrcode($type, $string, $filename = false, $logo = null,$level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN){
$type = strtolower($type);
if(!in_array($type, array("google", "php", "logo"))){
return false;
}
$method = "createQrcodeFrom".ucfirst($type);
return call_user_func_array(
'self::'.$method,
array($string, $filename, $logo, $level, $size, $margin, $logo)
);
}
/**
* google api 二維碼生成【QRcode能夠存儲最多4296個字母數字類型的任意文本,具體能夠查看二維碼數據格式】
* @param string $chl 二維碼包含的信息,能夠是數字、字符、二進制信息、漢字。不能混合數據類型,數據必須通過UTF-8 URL-encoded.若是須要傳遞的信息超過2K個字節,請使用POST方式
* @param int $widhtHeight 生成二維碼的尺寸設置
* @param string $EC_level 可選糾錯級別,QR碼支持四個等級糾錯,用來恢復丟失的、讀錯的、模糊的、數據。
* L-默認:能夠識別已損失的7%的數據
* M-能夠識別已損失15%的數據
* Q-能夠識別已損失25%的數據
* H-能夠識別已損失30%的數據
* @param int $margin 生成的二維碼離圖片邊框的距離
@return 圖像
*/
public static function createQrcodeFromGoogle($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN){
return 'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$level.'|'.$margin.'&chl='.urlencode($string);
}
/**
參數同上
**/
public static function createQrcodeFromPhp($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN) {
//加載你的php QR庫
$dir = dirname(__FILE__).DIRECTORY_SEPARATOR;
require_once $dir."phpqrcode".DIRECTORY_SEPARATOR."phpqrcode.php";
$filename = $dir.$filename;
QRcode::png($string, $filename, $level, $size, $margin);
return $filename;
}
public static function createQrcodeFromLogo($string, $filename = false, $logo = null, $level = self::LEVEL, $size = self::SIZE, $margin = self::MARGIN) {
$qrfile = self::createQrcodeFromPhp($string, $filename,$logo, $level, $size, $margin);
return self::creatQrcodeWithLogo($logo, $qrfile);
}
/**
@param $logo logo文件
@param $qrfile qrfile本地文件
@return 輸出到本地文件或者輸出到瀏覽器
**/
public static function creatQrcodeWithLogo($logo, $qrfile, $size=6)
{
if(!is_file($qrfile) || !is_file($logo)){
return false;
}
$QR = imagecreatefrompng($qrfile);//外面那QR圖
if ($logo !== FALSE) {
$logo = imagecreatefromstring(file_get_contents($logo));
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
$logo_qr_width = $QR_width/$size;
$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);
}
//二維碼文件存在輸出到文件不然輸出到瀏覽器
if(is_file($qrfile) && strpos($qrfile, "http") === false){
imagepng($QR, $qrfile);
} else{
header('Content-type: image/png');
imagepng($QR);
imagedestroy($QR);
}
}
}
/*
$type = "php";
$string = "beck.bi";
$filename = "test1.png";
MyQrcode::factoryCreateQrcode($type, $string, $filename );
*/
/*
$type = "google";
$string = "beck";
var_dump(MyQrcode::factoryCreateQrcode($type, $string));
*/
$type = "logo";
$string = "beck.bi";
$filename = "test2.png";
$logo = "1.jpg";
MyQrcode::factoryCreateQrcode($type, $string, $filename, $logo);
?>