引入use Endroid\QrCode\QrCode;
/*
*生成二维码
*/
public function build()
{
$text = $this->request->get('url', 'https://www.baidu.com/');
$size = $this->request->get('size', 250);
$padding = $this->request->get('padding', 15);
$errorcorrection = $this->request->get('errorcorrection', 'medium');
$foreground = $this->request->get('foreground', "#ffffff");
$background = $this->request->get('background', "#000000");
$logo = $this->request->get('logo');
$logosize = $this->request->get('logosize');
$label = $this->request->get('label');
$labelfontsize = $this->request->get('labelfontsize');
$labelhalign = $this->request->get('labelhalign');
$labelvalign = $this->request->get('labelvalign');
// 前景色
list($r, $g, $b) = sscanf($foreground, "#%02x%02x%02x");
$foregroundcolor = ['r' => $r, 'g' => $g, 'b' => $b];
// 背景色
list($r, $g, $b) = sscanf($background, "#%02x%02x%02x");
$backgroundcolor = ['r' => $r, 'g' => $g, 'b' => $b];
$qrCode = new QrCode();
$qrCode
->setText($text)
->setSize($size)
->setPadding($padding)
->setErrorCorrection($errorcorrection)
->setForegroundColor($foregroundcolor)
->setBackgroundColor($backgroundcolor)
->setLogoSize($logosize)
->setLabel($label)
->setLabelFontSize($labelfontsize)
->setLabelHalign($labelhalign)
->setLabelValign($labelvalign)
->setImageType(QrCode::IMAGE_TYPE_PNG);
$fontPath = ROOT_PATH . 'public/assets/fonts/SourceHanSansK-Regular.ttf';
if (file_exists($fontPath)) {
$qrCode->setLabelFontPath($fontPath);
}
if ($logo) {
$qrCode->setLogo(ROOT_PATH . 'public/assets/img/qrcode.png');
}
$filename = '/uploads/erweicode/' . time().mt_rand(1000,9999).'.png';
// $url = $qrCode->save($filename);
$qrCode->save(ROOT_PATH . 'public' . $filename);
return $filename;
//dump($url);die;
//也可以直接使用render方法输出结果
//$qrCode->render();
//return new Response($qrCode->get(), 200, ['Content-Type' => $qrCode->getContentType()]);
}
把qrcode放到addons下