<?php
namespace imcat;
(!defined('RUN_INIT')) && die('No Init');
include_once(DIR_STATIC.'/ximp/class/QRcodeBase.cls_php');
/*
* PHP QR Code encoder, Version: 1.1.4, Build: 2010100721
* http://phpqrcode.sourceforge.net/
* https://sourceforge.net/projects/phpqrcode/
public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
public static function text($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
public static function raw($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
* 二维码最大容量是1850个大写字母,2710个数字,1108个字节,500多个汉字
*/
class extQRcode{
static function show($text, $size=3, $level=2, $margin=4, $type='png', $outfile=false){
\QRcode::$type($text, $outfile, $level, $size, $margin);
}
# 卡片二维码背景:frame+qr+logo
# cfg: ['x'=>250,'y'=>450,'len'=>1000]
static function card($framefp, $qrurl, $cfg=[], $logofp=''){
$frame = $framefp ? imagecreatefromstring(self::getRfile($framefp)) : null;
#imagecolortransparent($frame, imagecolorallocate($frame,255,255,255)); // 防止png透明背景变黑
$qrcode = imagecreatefromstring(self::getRfile($qrurl));
$logo = $logofp ? imagecreatefromstring(self::getRfile($logofp)) : null;
// frame+qr
$len = $cfg['len']; $dx = $cfg['x']; $dy = $cfg['y'];
$qr_width = imagesx($qrcode); $qr_height = imagesy($qrcode);
imagecopyresampled($frame, $qrcode, $dx, $dy, 0, 0, $len, $len, $qr_width, $qr_height);
// frame+logo
$dpos = 0.375 * $len; $len = $len/4; // (logo固定1/4大小(0.25), 0.375=(1-0.25)/2
if($logo){
$logow = imagesx($logo); $logoh = imagesy($logo);
$rlogo = self::radiusImg($logo, $logow, $logoh, intval(0.15*$logow));
imagecopyresampled($frame, $rlogo, $dx+$dpos, $dy+$dpos, 0, 0, $len, $len, $logow, $logoh);
}
return $frame;
}
static function logo($qrfp, $logo='', $newfp='') {
//@ini_set("max_execution_time", "10"); // 设置该次请求超时时长,10s
//@ini_set("request_terminate_timeout", "10"); // 兼容php-fpm设置超时
if(empty($logo)) { // 使用系统的默认logo
$logo = DIR_VIEWS . '/base/assets/logo/imcat-40x.png';
}elseif(empty(strpos($logo,'/'))){
$logo = DIR_VIEWS . '/base/assets/logo/'.$logo;
}
$newfp || $newfp = $qrfp;
$qrfp = imagecreatefromstring(file_get_contents($qrfp));
$logo = imagecreatefromstring(file_get_contents($logo));
if ($qrfp && $logo) {
$QR_width = imagesx($qrfp);
$QR_height = imagesy($qrfp);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
$dst_w = $dst_h = $QR_width / 4; // 中间logo是方的
$from_width = ($QR_width - $dst_w) / 2;
imagecopyresampled($qrfp, $logo, $from_width, $from_width, 0, 0, $dst_w, $dst_h, $logo_width, $logo_height);
}
return imagepng($qrfp, $newfp);
}
// 带缓存有logo的二维码
static function cqr($text, $logo='', $dir='') {
$dir || $dir = date('Ym');
$fp = "/qrcode/$dir/".extCache::fName($text).'.png';
if(!file_exists(DIR_URES.$fp)){
if(!is_dir(DIR_URES."/qrcode/")){ mkdir(DIR_URES."/qrcode/"); }
if(!is_dir(DIR_URES."/qrcode/$dir/")){ mkdir(DIR_URES."/qrcode/$dir/"); }
$re1 = self::show($text, 3, 1, 4, 'png', DIR_URES.$fp); dump(DIR_URES.$fp);
self::logo(DIR_URES.$fp, $logo, '');
}
return PATH_URES.$fp;
}
static function radiusImg($src_img, $width,$height, $radius = 15) {
$w = &$width;
$h = &$height;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $radius; //圆 角半径
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
//不在四角的范围内,直接画
imagesetpixel($img, $x, $y, $rgbColor);
} else {
//在四角的范围内选择画
//上左
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//上右
$y_x = $w - $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//下左
$y_x = $r; //圆心X坐标
$y_y = $h - $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
//下右
$y_x = $w - $r; //圆心X坐标
$y_y = $h - $r; //圆心Y坐标
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
}
return $img;
}
static function getRfile($fp) {
if(!$fp){ return null; }
$opts = [
"ssl" => [
"verify_peer"=>false,
"verify_peer_name"=>false,
],
];
if(strpos($fp,'ttps://')){
$res = file_get_contents($fp, false, stream_context_create($opts));
}else{
$res = file_get_contents($fp);
}
return $res;
}
}
/*
$logo_width = imagesx($logo); $logo_height = imagesy($logo);
$rlogo = self::radiusImg($logo, $logo_width, $logo_height, intval(0.15*$logo_width));
*/