1.下载地址:https://sourceforge.net/projects/phpqrcode/
2.在vendor文件夹放入phpqrcode文件(解压后有两层phpqrcode文件夹,直接把里面那层phpqrcode文件夹剪切到vendor文件夹)
3.在public/static/images文件夹中上传logo.jpg文件,上传是为了为最后的结果做准备
在public/文件夹下创建个qrcode文件夹,用来放 生成的二维码
4.在公共方法common.php文件里加入
/**
* @param string $content
* @param string $filename
* 生成带logo二维码封装函数
*/
function create_qrcode($phone)
{
header('Content-Type: image/png');
vendor("phpqrcode.phpqrcode");
$data ='http://www.aaa.com/so/info/infodisplay/name/'.$phone;//需要生成二维码的内容
$outfile=ROOT_PATH."public/qrcode/".$phone.'.jpg';//生成二维码的路径
$level = 'L';//容错级别
$size =6;//图片大小慢慢自己调整,只要是int就行
$QRcode = new \QRcode();
ob_start();
$QRcode->png($data,$outfile,$level,$size,2);
$logo = ROOT_PATH.'/public/static/images/logo.jpg';//准备好的logo图片
$QR = ROOT_PATH."public/qrcode/".$phone.'.jpg';//已经生成的原始二维码图
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);
}
//输出图片 带logo图片
imagepng($QR, ROOT_PATH."public/qrcode/".$phone.'.jpg');
return 'ok';
}
/*
文件下载封装函数:
参数说明----$file_name:文件名(文件名为非中文)
*/
function file_dowm($file_name){
header("Content-type:text/html;charset=utf-8");
//文件转码
$file_name=iconv('UTF-8', 'GB2312', $file_name);
//使用绝对路径
$file_path=ROOT_PATH.'public\qrcode\\'.$file_name.'.jpg';
echo $file_path;
//打开文件---先判断再操作
if(!file_exists($file_path)){
echo "文件不存在";
return ; //直接退出
}
//存在--打开文件
$fp=fopen($file_path,"r");
//获取文件大小
$file_size=filesize($file_path);
/*
//这里可以设置超过多大不能下载
if($file_size>50){
echo "文件太大不能下载";
return ;
}*/
//http 下载需要的响应头
header("Content-type: application/octet-stream"); //返回的文件
header("Accept-Ranges: bytes"); //按照字节大小返回
header("Accept-Length: $file_size"); //返回文件大小
header("Content-Disposition: attachment; filename=".$file_name.'.jpg');//这里客户端的弹出对话框,对应的文件名
//ob_clean(),和flush() 是解决文件过大或损坏或乱码的
ob_clean();
flush();
//向客户端返回数据
//设置大小输出
$buffer=1024;
//为了下载安全,我们最好做一个文件字节读取计数器
$file_count=0;
//判断文件指针是否到了文件结束的位置(读取文件是否结束)
while(!feof($fp) && ($file_size-$file_count)>0){
$file_data=fread($fp,$buffer);
//统计读取多少个字节数
$file_count+=$buffer;
//把部分数据返回给浏览器
echo $file_data;
}
//关闭文件
fclose($fp);
}
如果生成不用带logo的二维码,create_qrcode()方法换成下面的方法
/**
* @param string $content
* @param string $filename
* 生成二维码
*/
function create_qrcode($phone)
{
header('Content-Type: image/png');
vendor("phpqrcode.phpqrcode");
$data ='http://www.aaa.com/so/info/infodisplay/name/'.$phone;
$outfile=ROOT_PATH."public/qrcode/".$phone.'.jpg';
$level = 'L';
$size =4;
$QRcode = new \QRcode();
ob_start();
$QRcode->png($data,$outfile,$level,$size,2);
ob_end_clean();
return 'ok';
}
5.控制器里的方法直接传参调用就行
例子
public function qrcode(Request $request)
{
$phone = $request->param('phone');
if(create_qrcode($phone)=='ok')
{
file_dowm($phone);
exit();
}
else
{
echo '失败';
exit();
}
}
6.视图里放个a连接
<a href="{:url('tour/qrcode',['phone'=>$v.phone])}">生成二维码</a>