symfony2中一维码和二维码使用

底层service代码

namespace AppBundle\Service;
use AppBundle\Exception\BaseException;
use BG\BarcodeBundle\Util\Base1DBarcode as barCode;
use BG\BarcodeBundle\Util\Base2DBarcode as matrixCode;
use Endroid\QrCode\QrCode;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class QrcodeService
{

    private $accessKey;
    private $secretKey;
    private $bucket;
    private $barcodeFolder;

    function __construct($accessKey, $secretKey, $bucket, $barcodeFolder)
    {
        $this->accessKey = $accessKey;
        $this->secretKey = $secretKey;
        $this->bucket = $bucket;
        $this->barcodeFolder = $barcodeFolder;
    }

    /**  * 生成二维码  * @param string $text  * @return QrCode  * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException  * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException  */  public function qrCode($text = 'caishen', $size = '200')
    {
        $qrCode = new QrCode();
        return  $qrCode->setText($text)
                       ->setSize(200)
                       ->setPadding(10)
                       ->setErrorCorrection('high')
                       ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
                       ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
                       ->setLabel($text)
                       ->setLabelFontSize(20)
                       ->render();
    }

    /**  * 一维码  * 物料  */  public function barcode($text, $sn, $category = 'material')
    {
        if (empty($sn)) {
            throw new \Exception('物料编号不能为空', BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
        }

        $myBarcode = new barCode();
        $savePath = $myBarcode->savePath = $this->barcodeFolder;

        $bcPathAbs = $myBarcode->getBarcodePNGPath($sn, 'C39', 1, 200);

        $keys = 'uploads/barcodes/' . $category . '/' . $text . '.png';

        $value = $this->uploadPhoto($this->accessKey, $this->secretKey, $this->bucket, $bcPathAbs, $keys);

        if (!isset($value[0])) {
            $uri = 'uploads/barcodes/material/'.$text.'.png';
        } else {
            $uri = $value[0]['key'];
        }

        unlink($bcPathAbs);

        return $uri;
    }

    /**  * 二维码  * 页面url  */  public function matrixCode($text, $type)
    {
        $myBarcode = new matrixCode();
        $savePath = $myBarcode->savePath = 'D:/img/';

        $keys = 'uploads/barcodes/material/'.$text.'png';


        $bcPathAbs = $myBarcode->getBarcodePNGPath($text, 'QRCODE', $w = 10, $h = 10, $color=array(0,0,0),$filename=null);
        $value = $this->uploadPhoto($this->accessKey, $this->secretKey, $this->bucket, $bcPathAbs, $keys);
        if (empty($value)) {
            throw new \Exception('生成条形码失败或条形码已经存在', BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
        }
        $url = $value[0]['key'];
        $this->delDir($savePath);

        return 'http://7xpzg5.com1.z0.glb.clouddn.com'.$url;
    }

    /*  * 上传图片  */  public function uploadPhoto($accessKey, $secretKey, $bucket, $filePath, $keys)
    {
        $auth = new Auth($accessKey, $secretKey);
        $upToken = $auth->uploadToken($bucket);
        $uploadManager =  new UploadManager();

        return $uploadManager->putFile($upToken, $keys, $filePath);

    }
}
控制器
/**  * 制作一维码  * @param Request $request  * @return JsonResponse  */ public function genBarCodeAction(Request $request)
{
    $qrCodeService = $this->get('app.qrcode_service');
    $url = $qrCodeService ->barcode($materialId, $material->getSn());

    return new JsonResponse($ret);
}


你可能感兴趣的:(symfony2中一维码和二维码使用)