在barcode官网下载barcodegen.1d-php5.v5.0.1.zip版本,查看官方文档。
解压文件存放到本地测试服务器中,访问/index.php,跳转到/html/BCGcode39.php,页面效果如下:
生成条形码后,可以到此官网进行校验。
实例:
提交数据 /html/image.php?filetype=PNG&dpi=72&scale=2&rotation=0&font_family=Arial.ttf&font_size=10&text=961001272500&thickness=30&checksum=&code=BCGcode39
image.php 程序源码
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
<?php define('IN_CB', true); include_once('include/function.php'); function showError() { header('Content-Type: image/png'); readfile('error.png'); exit; } $requiredKeys = array('code', 'filetype', 'dpi', 'scale', 'rotation', 'font_family', 'font_size', 'text'); // Check if everything is present in the request foreach ($requiredKeys as $key) { if (!isset($_GET[$key])) { showError(); } } if (!preg_match('/^[A-Za-z0-9]+$/', $_GET['code'])) { showError(); } $code = $_GET['code']; // Check if the code is valid if (!file_exists('config' . DIRECTORY_SEPARATOR . $code . '.php')) { showError(); } include_once('config' . DIRECTORY_SEPARATOR . $code . '.php'); $class_dir = '..' . DIRECTORY_SEPARATOR . 'class'; require_once($class_dir . DIRECTORY_SEPARATOR . 'BCGColor.php'); require_once($class_dir . DIRECTORY_SEPARATOR . 'BCGBarcode.php'); require_once($class_dir . DIRECTORY_SEPARATOR . 'BCGDrawing.php'); require_once($class_dir . DIRECTORY_SEPARATOR . 'BCGFontFile.php'); if (!include_once($class_dir . DIRECTORY_SEPARATOR . $classFile)) { showError(); } include_once('config' . DIRECTORY_SEPARATOR . $baseClassFile); $filetypes = array('PNG' => BCGDrawing::IMG_FORMAT_PNG, 'JPEG' => BCGDrawing::IMG_FORMAT_JPEG, 'GIF' => BCGDrawing::IMG_FORMAT_GIF); $drawException = null; try { $color_black = new BCGColor(0, 0, 0); $color_white = new BCGColor(255, 255, 255); $code_generated = new $className(); if (function_exists('baseCustomSetup')) { baseCustomSetup($code_generated, $_GET); } if (function_exists('customSetup')) { customSetup($code_generated, $_GET); } $code_generated->setScale(max(1, min(4, $_GET['scale']))); $code_generated->setBackgroundColor($color_white); $code_generated->setForegroundColor($color_black); if ($_GET['text'] !== '') { $text = convertText($_GET['text']); $code_generated->parse($text); } } catch(Exception $exception) { $drawException = $exception; } $drawing = new BCGDrawing('', $color_white); if($drawException) { $drawing->drawException($drawException); } else { $drawing->setBarcode($code_generated); $drawing->setRotationAngle($_GET['rotation']); $drawing->setDPI($_GET['dpi'] === 'NULL' ? null : max(72, min(300, intval($_GET['dpi'])))); $drawing->draw(); } switch ($_GET['filetype']) { case 'PNG': header('Content-Type: image/png'); break; case 'JPEG': header('Content-Type: image/jpeg'); break; case 'GIF': header('Content-Type: image/gif'); break; } $drawing->finish($filetypes[$_GET['filetype']]); ?> |