1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace app\common;
- use Endroid\QrCode\Color\Color;
- use Endroid\QrCode\Encoding\Encoding;
- use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
- use Endroid\QrCode\Label\Label;
- use Endroid\QrCode\Logo\Logo;
- use Endroid\QrCode\QrCode as QrCodeQrCode;
- use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
- use Endroid\QrCode\Writer\PngWriter;
- class Qrcode{
- private $logo = null;
- private $label = null;
- private $size = 400;
-
- public function __construct()
- {
- //
- }
-
- /**
- * 设置二维码LOGO
- */
- public function setLogo($logo_path)
- {
- $this->logo = Logo::create($logo_path) //logo的照片路径
- ->setResizeToWidth(20); //logo的大小
- }
-
- /**
- * 设置二维码下方文字
- */
- public function setLabel($text)
- {
- $this->label = Label::create($text) //二维码下面的文字
- ->setTextColor(new Color(0, 0, 0)); //文字的颜色
- }
-
- /**
- * 生成二维码
- * @param string $url [链接]
- * @return string [type] [返回图片path]
- */
- public function create(string $url,string $fileName)
- {
- $writer = new PngWriter();
-
- $qrCode = QrCodeQrCode::create($url)//跳转的url地址
- ->setEncoding(new Encoding('UTF-8')) //设置编码格式
- ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow()) //设置纠错级别为低
- ->setSize($this->size) //大小
- ->setMargin(30) //边距
- ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin()) //设置圆轮大小边距
- ->setForegroundColor(new Color(0, 0, 0)) //前景色
- ->setBackgroundColor(new Color(255, 255, 255)); //背景色
-
- $result = $writer->write($qrCode, $this->logo, $this->label);
-
- $result->getString();
-
- $file_path = public_path() . "/storage/qrcode/";
-
- if (!is_dir($file_path)) {
- mkdir($file_path, 0755, true);
- }
-
- if (!empty($fileName)){
- $qrcode = $fileName.'.png';
- }else{
- $qrcode = time() . mt_rand(0, 9999). '.png';
- }
-
- $result->saveToFile($file_path . $qrcode);
-
- return $file_path . $qrcode;
- }
- }
|