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直播系统源码,点击生成条形码实现的相关代码,更多内容欢迎关注之后的文章