1. 程式人生 > 其它 >app直播系統原始碼,點選生成條形碼

app直播系統原始碼,點選生成條形碼

app直播系統原始碼,點選生成條形碼實現的相關程式碼

<?php
include 'vendor/autoload.php';
class barcodeService
{
private $filePath, $foregroundColor,$backgroundColor;
private $text = '', $labelName = '', $scale = 2, $tn = 25, $fontSize = 10;
public function options($data)
{
foreach ($data as $key => $val) {
$this->{$key} = $val;
}
return $this;
}
public function generate()
{
try {
$barcode = new CodeItNow\BarcodeBundle\Utils\BarcodeGenerator();
$barcode->setText($this->text);
$barcode->setType(CodeItNow\BarcodeBundle\Utils\BarcodeGenerator::Code128);
//條形碼下面字型
$barcode->setLabel($this->labelName);
//比例
$barcode->setScale($this->scale);
//高度
$barcode->setThickness($this->tn);
$barcode->setFontSize($this->fontSize);
!empty($this->filePath) && $this->checkDir(dirname($this->filePath)) && $barcode->setFilename($this->filePath);
//條形碼顏色
!empty($this->foregroundColor) && $barcode->setForegroundColor($this->foregroundColor);
//背景色
!empty($this->backgroundColor) && $barcode->setBackgroundColor($this->backgroundColor);
$barcode->generate();
} catch (\Exception $e) {
//處理失敗情況
var_dump($e);
die;
}
}
private function checkDir($dir, $dirmode = 0755)
{
$path = explode('/', str_replace('\\', '/', $dir));
$depth = count($path);
for ($i = $depth; $i > 0; $i--) {
if (file_exists(implode('/', array_slice($path, 0, $i)))) {
break;
}
}
for ($i; $i < $depth; $i++) {
if ($d = implode('/', array_slice($path, 0, $i + 1))) {
if (!is_dir($d)) {
mkdir($d, $dirmode);
}
}
}
return is_dir($dir);
}
}
$func = function ($salt = '') {
return date('YmdHis') . mt_rand(10, 99) . substr(mt_rand(), -1);
};
$text = $func();
$path = './img/' . $text . '.png';
//儲存圖片
(new barcodeService())->options([
'text' => $text,
'labelName' => $text,
'filePath' => $path,
'foregroundColor' => '#696969',
// 'backgroundColor' =>'#000000'
])->generate();
var_dump($text);
echo '<img src="'.$path.'" alt="">';

以上就是 app直播系統原始碼,點選生成條形碼實現的相關程式碼,更多內容歡迎關注之後的文章