YII2.0框架生成二维码

2021-12-24 18:47:52 浏览数 (2)

  1. 安装扩展

由于YII框架有composer,应付个小小的二维码,不成问题。(如果还不知道composer,请自行查阅点击打开链接),我们只要运行命令行

composer require 2amigos/yii2-qrcode-helper

或者在 composer.json 中添加 2amigos/yii2-qrcode-helper

按步骤安装即可。安装完成后进行下一步。 2.代码参考

代码语言:javascript复制
<?php
namespace commonlib;

use DaQrCodeContractsErrorCorrectionLevelInterface;
use DaQrCodeExceptionInvalidPathException;
use DaQrCodeQrCode;
use Yii;

class QrCodeLib
{
    /**
     * 获取二维码
     * @param string $content
     * @param int $size
     * @return void
     */
    public static function getQrCode($content = '', $size = 300)
    {

        header('Content-Type: image/png');//解决输出乱码
        $qrCode = new QrCode($content);
        $qrCode->setSize($size);
        $code = $qrCode->writestring();
        exit($code);
    }

    /**
     * 获取带logo的二维码
     * @param string $content
     * @param string $logoAddress 入口web下的文件
     * @param int $size
     * @param int $margin
     * @param int $logoWidth
     * @return void
     */
    public static function getQrCodeWithLogo($content = '', $logoAddress = '', $size = 300, $margin = 5, $logoWidth = 65)
    {
        header('Content-Type: image/png');//防止乱码
        try {
            $code = (new QrCode($content, ErrorCorrectionLevelInterface::HIGH))
                ->useLogo($logoAddress)
                ->useEncoding('UTF-8')
                ->setSize($size)
                ->setMargin($margin)
                ->setLogoWidth($logoWidth)
                ->writestring();
            exit($code);
        } catch (InvalidPathException $e) {
            print_r('生成二维码失败' . $e->getMessage());
        }
    }

    /**
     * 获取二维码base64
     * @param string $content
     * @param int $size
     * @return void
     */
    public static function getQrCodeBase64($content = '', $size = 300) {
        $qrCode = new QrCode($content);
        $qrCode->setSize($size);
        $code = $qrCode->writestring();
        //使用base64_encode变成编码字符串
        $imageString = base64_encode($code);
        return json_encode(['png'=>'data:image/png;base64,' . $imageString]);
        //如果直接输出,用exit
        //        exit();
    }

    /**
     * 上传到oss
     * @param string $content
     * @param int $size
     * @param string $path
     * @param string $type
     * @return bool|string
     */
    public static function getQrCodeUploadOss($content = '', $size = 300, $path = './', $type = 'png') {
        $qrCode = new QrCode($content);
        $qrCode->setSize($size);
        $code = $qrCode->writestring();
        //使用base64_encode变成编码字符串
        $imageString = base64_encode($code);
        if (!is_dir($path)) {
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($path, 0777,true);
        }
        $keyName = date('Ymd'). md5(uniqid(microtime(true),true)) . "_".rand(100,9999) . ".{$type}";
        $ossUrl = Yii::$app->params['environment'] . '/****/' . date('Ym') . '/' . date('md') . '/' . $keyName;
        $newFile = $path . $keyName;

        if (file_put_contents($newFile, base64_decode($imageString))){
            $objOss= new Aliyunoss();
            if($objOss::upFile($keyName, $ossUrl, $objOss::HASH_KEY_PIC )){
                unlink($newFile);
                return $objOss::readFile(Aliyunoss::HASH_KEY_PIC, $ossUrl, 36000);
            } else {
                unlink($newFile);
                return false;
            }
        }else{

        }
    }

}

0 人点赞