下载php&qrcode集成包(文章已于资源绑定,自行下载)
1.解压后只用到两个文件
2.fonts粘贴到public目录下
3.qrcode更改名称QRcode并放到extend的qrcode目录下,并在QRcode文件中加入命名空间:namespace qrcode;
4.在自己需要使用的控制类里引入:use qrcode\QRcode;
/**
* 生成二维码
* @param $content 二维码内容
* @param $param
* @return string
*/
public function createCode($content, $param)
{
try{
// 删除更新前的二维码
if ($param["table_code_path"]) {
unlink($_SERVER['DOCUMENT_ROOT']."/".$param["table_code_path"]);
}
$url = “http://www.baidu.com”; // $content 二维码内容 例如扫码跳转百度
$errorCorrectionLevel = "L"; // 容错级别
$matrixPointSize = 15; // 图片大小
//生成二维码图片
// 判断是否有这个文件夹 没有的话就创建一个
if (!is_dir("qrcode/table_qrcode")) {
// 创建文件夹
mkdir("qrcode/table_qrcode",0777,true);
}
//设置二维码文件名
$filename = 'qrcode/table_qrcode/' . date('Y-m-d') . rand(10000, 99999) . '.png';
// 生成二维码
QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
// 二维码绝对路径
$table_path_code = $_SERVER['DOCUMENT_ROOT']."/".$filename;
$image = imagecreatefrompng($table_path_code);
$font = $_SERVER['DOCUMENT_ROOT'].'/fonts/simsun.ttc'; // 字体文件 要上传到/public/fonts目录下
$color = imagecolorallocate($image,0,0,0); // 文字颜色
imagettftext($image, 16, 0, 150, 425, $color, $font, '二维码码:'.$param["table_code"]);
imagepng($image, $table_path_code);
// 删除原来的图片
imagedestroy($image);
return $filename; // 二维码地址
}catch (\Exception $e){
return $this->jsonError(config("responseText.text.error"));
}
}
二维码只能打码,不然审核不过
5.前端通过图片地址下载和打印
①下载:
<div><a style="color: #0D9AFF;text-decoration: underline;" download='文件名称' href='图片地址'>点击下载</a></div>
②打印:
var v = document.createElement("div");
$(v).append(table_code_img_path + "</div>"); // table_code_img_path 图片地址
var h = window.open("Print_window", "_blank");
h.document.write($(v).prop("outerHTML"));
h.document.close();
h.print();
h.close();