1、进入composer官方网站,搜索phpqrcode安装包
composer命令安装
composer require aferrandini/phpqrcode
生成二维码图片的公共方法:
// 公用二维码生成 static function setQrcode($url){ //二维码图片保存路径 $path = 'advert/qrcode/'.date("Ymd"); $pathname = storage_path('app/public/').$path; if(!is_dir($pathname)) { //若目录不存在则创建之 mkdir($pathname,0777,true); } $qrcode = new QRcode(); //二维码图片保存路径(若不生成文件则设置为false) $suijishu = date("YmdHis").rand(100,999); $filename = $pathname . "/qrcode_" . $suijishu . ".png";//生成路径 $destName = 'storage/'.$path."/qrcode_" . $suijishu . ".png";//返回路径 //二维码容错率,默认L $level = "L"; //二维码图片每个黑点的像素,默认4 $size = '10'; //二维码边框的间距,默认2 $padding = 2; //保存二维码图片并显示出来,$filename必须传递文件路径 $saveandprint = true; //生成二维码图片 $qrcode->png($url,$filename,$level,$size,$padding,$saveandprint); //二维logo //$logo = "./public/wxpay/images/logo.png"; //$QR = imagecreatefromstring(file_get_contents($filename)); //$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 / 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); //imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); //imagepng($QR,$filename); return $destName; }
控制器调用:
use PHPQRCode\QRcode; $result['code_url'] = $this->setQrcode($result['code_url']);//生成二维码