PHP二维码

  1. 修改composer.json

         "bacon/bacon-qr-code": "^1.0.3",
    
  2. 安装

     composer install
    
  3. 或者-直接安装扩展类库

     composer require bacon/bacon-qr-code
    
  4. 源码使用

     use BaconQrCode\Writer;
     use BaconQrCode\Renderer\Image\Png;
     //生成二维码buffer使用↓
     public function buildQRCodeBuffer($str, $size)
     {
         $str = !empty($str) ? $str : ' ';
         $size = (is_numeric($size) && $size > 0) ? $size : 256;
         $png = new Png();
         $png->setWidth($size);
         $png->setHeight($size);
         unset($size);
         $write = new Writer($png);
         unset($png);
         $qr_code_buffer = $write->writeString($str, 'UTF-8');
         unset($write, $str);
         return $qr_code_buffer;
     }
    
     //生成二维码文件到指定路径使用↓
     public function buildQRCodeBuffer($str, $size, $path)
     {
         $str = !empty($str) ? $str : ' ';
         $size = (is_numeric($size) && $size > 0) ? $size : 256;
         //假设__DIR__ . '/lost_qr_code'存在,且有被写入的权限
         $path = !empty($path) ? $path : __DIR__ . '/lost_qr_code';
         $png = new Png();
         $png->setWidth($size);
         $png->setHeight($size);
         unset($size);
         $write = new Writer($png);
         unset($png);
         $write->writeFile($str, $path, 'UTF-8');
         unset($write, $str);
     }
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-08-10 15:11:40

results matching ""

    No results matching ""