📖 File Reader
<?php
namespace app\common;
use app\BaseController;
class Common_qrcode extends BaseController
{
/**
* 二维码生成
* @param $value 二维码内容
* @param $logo_url logo地址:/www/wwwroot/baichengdc.gkktc.cn/public/uploads/logo/20240727/62fcbd9fd5d58a3f798c0eb59dfb6ca8.jpg(全路径)
* @param $background_url 背景地址:/www/wwwroot/baichengdc.gkktc.cn/public/uploads/logo/20240727/62fcbd9fd5d58a3f798c0eb59dfb6ca8.png(全路径)
* @return void
*/
public function qrcode($value='',$logo_url='',$background_url='')
{
require_once 'extend/phpqrcode/phpqrcode.php';
$path = $_SERVER['DOCUMENT_ROOT'].'/public/uploads/qrcode/'.date("Ymd",time());//文件地址
if(!is_readable($path)) {
//is_file($path) or mkdir($path,0777);
mkdir($path,0777,true);
}
$filename = md5(time()).'.png';
$filelink = $path.'/'.$filename;
$QRcode = new \QRcode(); //实例类
$errorCorrectionLevel = 'H'; //容错级别
$matrixPointSize = 6; //生成图片大小
$QRcode->png($value,$filelink , $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $filelink; //已经生成的原始二维码图
/*添加logo*/
if (file_exists($logo_url)) {
$QR = imagecreatefromstring(file_get_contents($QR)); //目标图象连接资源。
$logo = imagecreatefromstring(file_get_contents($logo_url)); //源图象连接资源。
$QR_width = imagesx($QR); //二维码图片宽度
$QR_height = imagesy($QR); //二维码图片高度
$logo_width = imagesx($logo); //logo图片宽度
$logo_height = imagesy($logo); //logo图片高度
$logo_qr_width = $QR_width / 4; //组合之后logo的宽度(占二维码的1/5)
$scale = $logo_width/$logo_qr_width; //logo的宽度缩放比(本身宽度/组合后的宽度)
$logo_qr_height = $logo_height/$scale; //组合之后logo的高度
$from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点
//重新组合图片并调整大小(将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中)
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
//输出图片
imagepng($QR, $filelink);
//销毁图像
imagedestroy($QR);
imagedestroy($logo);
}
/*添加背景图*/
if (file_exists($background_url)){
$backgroupImg = imagecreatefromstring(file_get_contents($background_url));
$newQR = imagecreatefromstring(file_get_contents($filelink));
//获取新的尺寸
list($width, $height) = getimagesize($filelink);
$new_width = 260;
$new_height = 260;
//重新组合图片并调整大小
imagecopyresampled($backgroupImg,$newQR,245, 700, 0, 0,$new_width, $new_height, $width, $height);//输出图片
imagepng($backgroupImg, $filelink);
//销毁图像
imagedestroy($backgroupImg);
imagedestroy($newQR);
}
$data = [
'filelink'=>$filelink,
'erweima_url'=>'/public/uploads/qrcode/'.date("Ymd",time()).'/'.$filename,
];
return $data;
}
/**
* bese64格式图片存储本地
* @param $base64_string 图片字符串
* @param $user_id 会员id
* @return void
*/
static function bese64_transition($base64_string='',$user_id='')
{
$base64_arr = explode(',',$base64_string);
if($base64_arr[0] != 'data:image/png;base64' && $base64_arr[0] != 'data:image/jpeg;base64'){
$data = ['code'=>400,'msg'=>'格式错误,请传入base64格式'];
return $data;
}
// 去掉Base64字符串开头的数据头部
$base64_string = explode(',', $base64_string)[1];
// 解码Base64字符串
$decoded_data = base64_decode($base64_string);
///生成文件并写入
$file_name = md5($user_id).'.png';//文件名
$file_link = '/public/uploads/qrcode_WeChat/';
$path = $_SERVER['DOCUMENT_ROOT'].$file_link;//文件地址
if(!is_readable($path)) {is_file($path) or mkdir($path,0777);}
if (file_put_contents($path.$file_name, $decoded_data) !== false) { // 将解码后的数据写入到文件
$data = ['code'=>200,'msg'=>'转换成功','file_link'=>$file_link.$file_name];
return $data;
} else {
$data = ['code'=>400,'msg'=>'转换失败'];
return $data;
}
}
/**
* 二维码生成(根据已知二维码合成背景图)
* @param $url 二维码地址:/www/wwwroot/baichengdc.gkktc.cn/public/uploads/logo/20240727/62fcbd9fd5d58a3f798c0eb59dfb6ca8.png(全路径)
* @param $background_url 背景地址:/www/wwwroot/baichengdc.gkktc.cn/public/uploads/logo/20240727/62fcbd9fd5d58a3f798c0eb59dfb6ca8.png(全路径)
* @return void
*/
static function qrcode_wechat($url='',$background_url)
{
require_once 'extend/phpqrcode/phpqrcode.php';
$filelink = $url;//已有二维码地址
/*添加背景图*/
if (file_exists($background_url)){
$backgroupImg = imagecreatefromstring(file_get_contents($background_url));
$newQR = imagecreatefromstring(file_get_contents($filelink));
//获取新的尺寸
list($width, $height) = getimagesize($filelink);
$new_width = 260;
$new_height = 260;
//重新组合图片并调整大小
imagecopyresampled($backgroupImg,$newQR,245, 700, 0, 0,$new_width, $new_height, $width, $height);//输出图片
imagepng($backgroupImg, $filelink);
//销毁图像
imagedestroy($backgroupImg);
imagedestroy($newQR);
}
$data = [
'filelink'=>$filelink,
];
return $data;
}
}