TP6 生成Qrcode二维码和Barcode条形码的方法

ThinkPHP 244 0 2022-11-09

TP6 生成Qrcode二维码和Barcode条形码的方法

运行命令下载:composer require codeitnowin/barcode

注:好像已经不可用,如不可以下载那就从以下网址下载吧

下载地址一:https://github.com/codeitnowin/barcode-generator

下载地址二:https://pan.baidu.com/s/1-hFWYfed4y3YkWKLz2o75g

手动下载完后解压缩,将codeitnowin文件整个拷贝到项目的扩展文件夹extend中,如下图:

二维码和条形码生成代码如下:


<?php
namespace app\index\controller;
use app\BaseController;

use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
use CodeItNow\BarcodeBundle\Utils\QrCode;

class Barcode extends BaseController
{
	public function index(){
		
		echo '<p>Example - QrCode</p>';
		$qrCode = new QrCode();
		$qrCode
		    ->setText('http://www.hilo8.com/index/share')
		    ->setSize(200)
		    ->setPadding(10)
		    ->setErrorCorrection('high')
		    ->setForegroundColor($this->hex2rgba('#F00'))
		    ->setBackgroundColor($this->hex2rgba('#FFF'))
		    ->setLabel('http://www.hilo8.com/')
		    ->setLabelFontSize(14)
		    ->setImageType(QrCode::IMAGE_TYPE_PNG);
		echo '<img src="data:' . $qrCode->getContentType() . ';base64,' . $qrCode->generate() . '" />';
				
		echo '<hr>';
		echo '<p>Example - Code128</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("0123456789");
		$barcode->setType(BarcodeGenerator::Code128);
		$barcode->setScale(1);
		$barcode->setThickness(25);
		$barcode->setFontSize(10);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Code11</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("0123456789");
		$barcode->setType(BarcodeGenerator::Code11);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Code39</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("0123456789");
		$barcode->setType(BarcodeGenerator::Code39);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
		echo '<hr>';
				
		echo '<p>Example - Code39Extended</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("0123456789");
		$barcode->setType(BarcodeGenerator::Code39Extended);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Ean128</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("00123456789012345675");
		$barcode->setType(BarcodeGenerator::Ean128);
		$barcode->setScale(2);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
		
		echo '<hr>';
		echo '<p>Example - Ean13</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("6974798950011");
		$barcode->setType(BarcodeGenerator::Ean13);
		$barcode->setScale(2);
		$barcode->setFontSize(14);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Gs1128</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("00123456789012345675");
		$barcode->setType(BarcodeGenerator::Gs1128);
		$barcode->setScale(2);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Gs1128</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("4157707266014651802001012603068039000000006377069620171215");
		$barcode->setType(BarcodeGenerator::Gs1128);
		$barcode->setScale(2);
		$barcode->setFontSize(14);
		$barcode->setNoLengthLimit(true);
		$barcode->setAllowsUnknownIdentifier(true);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - I25</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("00123456789012345675");
		$barcode->setType(BarcodeGenerator::I25);
		$barcode->setFontSize(12);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Isbn</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("0012345678901");
		$barcode->setType(BarcodeGenerator::Isbn);
		$barcode->setScale(2);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Msi</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("0012345678901");
		$barcode->setType(BarcodeGenerator::Msi);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Postnet</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("01234567890");
		$barcode->setType(BarcodeGenerator::Postnet);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - S25</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("012345678901");
		$barcode->setType(BarcodeGenerator::S25);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
				
		echo '<hr>';
		echo '<p>Example - Upca</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("012345678901");
		$barcode->setType(BarcodeGenerator::Upca);
		$barcode->setScale(2);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';
		
		echo '<hr>';
		echo '<p>Example - Upce</p>';
		$barcode = new BarcodeGenerator();
		$barcode->setText("012345");
		$barcode->setType(BarcodeGenerator::Upce);
		$barcode->setScale(2);
		$code = $barcode->generate();
		echo '<img src="data:image/png;base64,' . $code . '" />';

	}
	
	private function hex2rgba($hexColor='#000',$a=0)
	{
	    $color = str_replace('#','',strtoupper(trim($hexColor)));
	    if(strlen($color)>3){
	        $rgba=array(
	            'r'=>hexdec(substr($color,0,2)),
	            'g'=>hexdec(substr($color,2,2)),
	            'b'=>hexdec(substr($color,4,2)),
				'a'=>$a
	        );
	    }else{
	        $rgba=array(
	            'r'=>hexdec(substr($color,0,1). substr($color,0,1)),
	            'g'=>hexdec(substr($color,1,1). substr($color,1,1)),
	            'b'=>hexdec(substr($color,2,1). substr($color,2,1)),
				'a'=>$a
	        );
	    }
	    return $rgba;
	}
	
}


效果如下:

上一篇:TP6常用模板变量和语句输出

下一篇:没有了

讨论数量:0

请先登录再发表讨论。 2023-02-03

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链