PHP条形码生成
程序员文章站
2024-03-23 16:46:04
...
环境:
PHP版本: 7.3.4
PHP框架:tp5.1
第三方插件:barcodegen
一、下载插件,部署环境
安装方式有两种:
1、下载压缩安装
插件下载直通车:https://www.barcodebakery.com/en/download/php
下载5.2版本足够
下载完成后,部署到tp5.1框架中,extend文件夹下
2、使用composer安装,具体步骤可以查看barcode文档
二、使用
废话不多说,直接上demo
public function barcode_create(){
//内容
$content='6920973904560';
$file_dir = 'Uploads/barcode/'.date("Y-m-d",time()); //文件路径
if(!file_exists($file_dir)) { //判断文件是否存在
mkdir($file_dir,0777,true); //不存在则生成
}
$imgUrl = $file_dir . '/' . time() . '.png'; //图片路径
require_once(Env::get('extend_path') . 'barcode/class/BCGFont.php');
require_once(Env::get('extend_path') . 'barcode/class/BCGColor.php');
require_once(Env::get('extend_path') . 'barcode/class/BCGcode128.barcode.php');
require_once(Env::get('extend_path') . 'barcode/class/BCGDrawing.php');
require_once(Env::get('extend_path') . 'barcode/class/BCGFontFile.php');
// 加载字体大小
$font = new \BCGFontFile(Env::get('extend_path') . 'barcode/font/Arial.ttf', 18);
//颜色条形码
$color_black = new \BCGColor(0, 0, 0);
$color_white = new \BCGColor(255, 255, 255);
$drawException = null;
try{
$code = new \BCGcode128();
$code->setScale(2);
$code->setThickness(30); // 条形码的厚度
$code->setForegroundColor($color_black); // 条形码颜色
$code->setBackgroundColor($color_white); // 空白间隙颜色
$code->setFont($font); //
$code->parse($content); // 条形码需要的数据内容
}catch(\Exception $exception){
$drawException = $exception;
}
//根据以上条件绘制条形码
$drawing = new \BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
}else{
$drawing->setBarcode($code);
$drawing->setFilename($imgUrl); //存放路径
$drawing->draw();
}
// 生成PNG格式的图片
$drawing->finish($drawing::IMG_FORMAT_PNG);
$out_arr['code'] = '000000';
$out_arr['url'] = $imgUrl;
echo json_encode($out_arr,JSON_UNESCAPED_UNICODE);die;
}
遇到的问题:
1.引入类的时候不能使用loader自动加载,因为tp5.1已经废弃
2.mkdir使用时,递归创建文件夹时,需要加第三个参数true
以做笔记,少走弯路
推荐阅读
-
PHP条形码生成
-
js 生成随机数 博客分类: jshtml相关 JavaScript随机数random
-
辗转相除求最大公因数(greatest common factor)欧几里得 博客分类: php 辗转相最大公因数欧几里得算法
-
得到身份证信息 博客分类: php 身份证号信息
-
表达式求值 博客分类: php 数据结构
-
数组转换为树 博客分类: php php生成树
-
php通过thrift获取hadoop资源 博客分类: phpthrift hadoopthriftphpjavahdfs
-
【汇总】PHP扩展【xhprof,eaccelerator】 博客分类: php xhprofeaccelerator
-
Nginx环境下Php安装 nginxphp
-
【汇总】PHP常用小函数【概率,毫秒,IP】 博客分类: php php