thinkphp控制器里怎么使用require
程序员文章站
2022-05-29 23:22:51
...
public function search_edit(){
// Including all required classes
require_once('D:\wamp64\www\aier\Public\Admin\barcodegen\class\BCGFontFile.php');
require_once('D:\wamp64\www\aier\Public\Admin\barcodegen\class\BCGColor.php');
require_once('D:\wamp64\www\aier\Public\Admin\barcodegen\class\BCGDrawing.php');
$codebar = BCGcode39; //条形码将要数据的内容
// Including the barcode technology
require_once('D:\wamp64\www\aier\Public\Admin\barcodegen\class\barcode.php');
// Loading Font
$font = new BCGFontFile('./font/Arial.ttf', 12);
// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);
$drawException = null;
try {
$code = new $codebar();//实例化对应的编码格式
$code->setScale(2); // Resolution
$code->setThickness(23); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$text = 'dfdf'; //条形码将要数据的内容
$code->parse($text);
} catch(Exception $exception) {
$drawException = $exception;
}
/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
}
报错:
Class 'Admin\Controller\BCGFontFile' not found
require是成功了,但require里的变量无法找到,用不了。
果然有用,谢谢!
// Including all required classes
require_once('D:\wamp64\www\aier\Public\Admin\barcodegen\class\BCGFontFile.php');
require_once('D:\wamp64\www\aier\Public\Admin\barcodegen\class\BCGColor.php');
require_once('D:\wamp64\www\aier\Public\Admin\barcodegen\class\BCGDrawing.php');
$codebar = BCGcode39; //条形码将要数据的内容
// Including the barcode technology
require_once('D:\wamp64\www\aier\Public\Admin\barcodegen\class\barcode.php');
// Loading Font
$font = new BCGFontFile('./font/Arial.ttf', 12);
// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);
$drawException = null;
try {
$code = new $codebar();//实例化对应的编码格式
$code->setScale(2); // Resolution
$code->setThickness(23); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$text = 'dfdf'; //条形码将要数据的内容
$code->parse($text);
} catch(Exception $exception) {
$drawException = $exception;
}
/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
}
报错:
Class 'Admin\Controller\BCGFontFile' not found
require是成功了,但require里的变量无法找到,用不了。
回复讨论(解决方案)
试试
$font = new \BCGFontFile('./font/Arial.ttf', 12);
下面所有的你引入的文件类,都加一个 \
试试
$font = new \BCGFontFile('./font/Arial.ttf', 12);
下面所有的你引入的文件类,都加一个 \
果然有用,谢谢!
下一篇: php创建多层(多级)目录的函数