条形码 (barcode) 是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符。
这里采用 php 实现,支持大多数的码制。
安装所需的代码库
composer require codeitnowin/barcode
实例:
$nowDay = date('Y-m-d');
$ymd = date('Ymd');
if(!is_dir($_SERVER['DOCUMENT_ROOT'].'/uploads/qrcode/'.$nowDay)){
mkdir($_SERVER['DOCUMENT_ROOT'].'/uploads/qrcode/'.$nowDay,0777,true);
}
$file_path = './uploads/qrcode/'.$nowDay.'/';
$file_id = uniqid();
$filename = $file_path.$file_id.'.jpg';
$barcode = new BarcodeGenerator();
$no = ProofingNotice::setProofingCode($ymd);
$barcode->setText($no);
$barcode->setType(BarcodeGenerator::Code128);
$barcode->setScale(2);
$barcode->setThickness(25);
$barcode->setFontSize(10);
$barcode->setFilename($filename);
$code = $barcode->generate();
$uploads = new UploadsController();
$file = '/uploads/qrcode/'.$nowDay.'/'.$file_id.'.png';
$uploads->uploadsiImgMobile($file);
DB::table('proofing_notice')->where('id',$notice_id)->update(['barcode'=>$new_path]);
相关代码(具体展示在后面)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
use CodeItNow\BarcodeBundle\Utils\QrCode;
class BarCodeController extends Controller
{
public function index()
{
echo '<p>Example - QrCode</p>';
$qrCode = new QrCode();
$qrCode
->setText('https://www.php.net/manual/zh/')
->setSize(300)
->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('https://www.php.net/')
->setLabelFontSize(16)
->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(2);
$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);
$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);
$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->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);
$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);
$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);
$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);
$code = $barcode->generate();
echo '<img src="data:image/png;base64,' . $code . '" />';
}
}
|