天天看點

php生成條形碼和二維碼(php)

導言

現在條形碼和二維碼已經在我們的生活中随處可見了,是以今天就來分享一下怎麼去生成符合我們需求的條形碼二維碼。

條形碼的生成

說到php生成的條形碼,就不得不提到一個很好用的類庫,barcode。

首先,我們可以到官網去下載下傳最新的代碼類庫。

選擇我們需要的版本和類型,我這邊用的是php的7.3.4,是以我下載下傳的6.0.0版本。

檔案下載下傳好了,他裡面有兩個目錄,我們直接進入到example目錄内,打開code檔案,找到我們需要的條形碼類型,這裡我需要的是CODE128,是以 很簡單,隻需要打開test_code128.php,将其中内容複制出來,粘貼到我們需要使用的地方。

barcode官網

php生成條形碼和二維碼(php)
<?php

header("Access-Control-Allow-Origin: *");
require __DIR__ . '/tiaoma/example/vendor/autoload.php';

use BarcodeBakery\Common\BCGColor;
use BarcodeBakery\Common\BCGDrawing;
use BarcodeBakery\Common\BCGFontFile;
use BarcodeBakery\Barcode\BCGcode128;

// Loading Font
$font = new BCGFontFile(__DIR__ . '/tiaoma/example/font/Arial.ttf', 18);

// Don't forget to sanitize user inputs
$name = $_POST['name'];
$text = empty($name) ? (empty($_GET['name']) ? 'a123' : $_GET['name']) : $name;
//$text = isset($_GET['text']) ? $_GET['text'] : 'a123';

// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);

$drawException = null;
try {
    $code = new BCGcode128();
    $code->setScale(2); // Resolution
    $code->setThickness(30); // Thickness
    $code->setForegroundColor($color_black); // Color of bars
    $code->setBackgroundColor($color_white); // Color of spaces
    $code->setFont($font); // Font (or 0)
    $code->setStart(null);
    $code->setTilde(true);
    $code->parse($text); // Text
} catch (Exception $exception) {
    $drawException = $exception;
}

/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new BCGDrawing('', $color_white);
if ($drawException) {
    $drawing->drawException($drawException);
} else {
    $drawing->setBarcode($code);
    $drawing->draw();
}

// Header that says it is an image (remove it if you save the barcode to a file)
//header('Content-Type: image/png');
//header('Content-Disposition: inline; filename="barcode.png"');

// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
?>
           

這裡有個小坑,我這樣使用之後,在我的network裡面是能夠看到生成之後的條形碼,但是在控制台列印的時候出現的是一串亂碼。

仔細排查之後,發現這樣之後我得到的是資料流而不是前端需要的圖檔位址,于是一陣百度之後,找到了一個合适的方法,修改了一下代碼

ob_start();
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
$img = ob_get_contents();
ob_end_clean();
$img = 'data:png;base64,' . base64_encode($img); //對生成條形碼的資料進行64位編碼
ob_flush();
$data = array(
    'msg'  => 'Success',
    'name' => $img,
);
echo json_encode($data);
           

這樣處理之後,前端就能得到需要的圖檔位址,條形碼也能在前端頁面上正常顯示了。

二維碼的生成

在php中,生成二維碼最常用的就是qrcode

使用的方法也和barcode類似;

public function drawcode($name='')
    {
        require_once ROOT . '/core/plugins/qrcode.class.php';


        $value = empty($name)? 'no input':$name; //二維碼内容
        $errorCorrectionLevel = 'L';  //容錯級别
        $matrixPointSize = 8;      //生成圖檔大小
        $qrcode = new QRcode();
        ob_start();
        QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize, 2);
        $img = ob_get_contents();
        ob_end_clean();
        $img = 'data:png;base64,' . base64_encode($img); //對生成二維碼的資料進行64位編碼
        ob_flush();
        $data = array(
		    'msg'  => 'Success',
		    'name' => $img,
			);
		echo json_encode($data);
	}