Method Example of Generating Bar Code Based on thinkPHP Framework
- 2021-10-15 10:02:39
- OfStack
In this paper, an example is given to describe the method of generating barcode by thinkPHP framework. Share it for your reference, as follows:
Before we do it, we download barcode class first. If you want to download this class, you can click here to download it.
We write a method code in the background as follows:
// Generate barcode
public function barcode(){
import('@.ORG.Util.barcode.BCGFontFile');// Font class
import('@.ORG.Util.barcode.BCGColor');// Font color class
import('@.ORG.Util.barcode.BCGDrawing');
import('@.ORG.Util.barcode.BCGcode39');
$text = $_GET['text'];
$texts = isset($text)?$text:'00000000000';
$color_black = new \BCGColor(0,0,0);
$color_white = new \BCGColor(255,255,255);
$drawException = null;
try {
$code = new \BCGcode39();
$code->setScale(2);
$code->setThickness(30);
$code->setForegroundColor($color_black);
$code->setBackgroundColor($color_white);
$code->parse($texts);
} catch(Exception $exception) {
$drawException = $exception;
}
$drawing = new \BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}
header('Content-Type: image/png');
header('Content-Disposition: inline; filename="barcode.png"');
$drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
}
Call directly from the foreground:
<img src="{:U('ContractCommonApply/barcode')}/text/{$res[0]['ContractCode']}" alt="">
The code called with js is as follows:
<script type="text/javascript" language="JavaScript">
document.writeln("<img src=/ Directory /test_1D.php?text= Content />");
</script>
PS: Here we recommend a similar barcode generation tool for your reference:
Online barcode (1-dimensional code) generation/real-time preview tool:
http://tools.ofstack.com/aideddesign/tiaoxingma
For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.