phpqrcode不生成真实图片

  phpqrcode是一个PHP生成二维码的类库,使用它可以轻松生成二维码,官网地址:http://phpqrcode.sourceforge.net/
  当然Google也提供了较为完善的二维码生成接口,调用也简单方便,但这里就不做详细阐述了,有兴趣的请移步: https://developers.google.com/chart/infographics/docs/qr_codes
  phpqrcode生成二维码仅使用QRcode::png方法即可,QRcode::png有六个参数,后面我们详细讲解,现先看一个小例子:

    require_once './phpqrcode/phpqrcode.php';
    $url = 'http://baidu.com/?dddddddddd=888888';
    QRcode::png($url,false);//生成二维码并将其直接输出到浏览器中```

![上面生成的二维码](http://upload-images.jianshu.io/upload_images/5983766-4a3d4517504f8c11.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这样很容易就生成了一个简单的二维码,但在实际应用中,我们还需要使用其他参数,下面我们来逐个讲解:

public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false){
$enc = QRencode::factory($level, $size, $margin);
return $enc->encodePNG($text, $outfile, $saveandprint=false);
}

  1. 第一个参数$text,就是上面代码里的URL网址参数
  2. 第二个参数$outfile默认为否,不生成文件,只将二维码图片返回,否则需要给出存放生成二维码图片的路径
  3. 第三个参数$level默认为L,这个参数可传递的值分别是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)。这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比。
    利用二维维码的容错率,我们可以将头像放置在生成的二维码图片任何区域。
  4. 第四个参数$size,控制生成图片的大小,默认为4
  5. 第五个参数$margin,控制生成二维码的空白区域大小
  6. 第六个参数$saveandprint,保存二维码图片并显示出来,$outfile必须传递图片路径。
  现在我们来说说如何让phpqrcode不生成真实图片,当第二个参数为``false``的时候,png 方法会将png图像输出到浏览器,这里我们重点看``ImagePng``方法,
```The path or an open stream resource (which is automatically being closed after this function returns) to save the file to. If not set or NULL, the raw image stream will be outputted directly.```这是PHP官网对它的介绍,意思是如果第二个参数未设置或设置``NULL``的话,该方法将生成的原始图像流放到缓冲区,在页面打开时直接输出。这就意味着我们不需要使用``echo``将内容输出,也不能直接使用变量接收它。这个时候``ob系``函数变得就尤为重要了。
  下面我们直接来看代码:

function __getQrcode(){
require_once './phpqrcode/phpqrcode.php';
$url = "http://www.baidu.com?xxxxxxxxxxxx=dd";
ob_start();
QRcode::png($url, false , '1', 3 , $margin = 2, $saveandprint=true);
$data =ob_get_contents();
ob_end_clean();
return "data:image/jpeg;base64,".base64_encode($data);
}

?>

111111111111111111111111111111111




![上面生成的二维码](http://upload-images.jianshu.io/upload_images/5983766-e4ff6438a0e0ccdd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
**值得注意的是,我们需要修改``QRimage``类,注释大概第``914``行的代码,如下图:**
![QRimage](http://upload-images.jianshu.io/upload_images/5983766-5a95ac928ef165d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  至此我们就可以使用phpqrcode不生成真实图片了。至于``ob系``函数就请移步PHP官网: `` http://php.net/manual/en/function.ob-start.php ``
  这里楼主再给大家送点小福利,既然说了二维码,就顺便说说条形码,代码如下:

// 引用class文件对应的类
require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');
// 条形码的编码格式
require_once('class/BCGcode39.barcode.php');
define('BARCODE_FONT',12); // 字体大小

/**

  • PHP生成条形码
    */
    class Barcode
    {

    function creat_barcode($content = null)
    {
    $content = $content ? $content : 'HELLO';
    $color_black = new BCGColor(0, 0, 0);
    $color_white = new BCGColor(255, 255, 255);

     $drawException = null;
     try {
         $code = new BCGcode39();
         $code->setScale(1);   //0.8-2   一般选 1 
         $code->setThickness(30); // 条形码的厚度
         $code->setForegroundColor($color_black); // 条形码颜色
         $code->setBackgroundColor($color_white); // 空白间隙颜色
         $code->setFont(BARCODE_FONT);
         $code->parse($content); // 条形码需要的数据内容
     } catch (Exception $exception) {
         $drawException = $exception;
     }
    
     $drawing = new BCGDrawing('', $color_white);
     if ($drawException) {
         $drawing->drawException($drawException);
     } else {
         $drawing->setBarcode($code);
         $drawing->draw();
     }
    
     ob_start();
     header('Content-Type: image/png');
     $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
     $barcode_info = ob_get_contents();
     ob_end_clean();
    
     return $barcode_info;
    

    }
    }

    /**

    • 获取条码src
      */
      function getBarCode($bn)
      {
      $barcodeObj = new Barcode();
      $barcode_info = $barcodeObj->creat_barcode($bn);
      echo $barcode_info;
      }




你可能感兴趣的:(phpqrcode不生成真实图片)