本PHP5生成條形碼的簡(jiǎn)單實(shí)例中使用的是PHP5的版本。在使用前注意要將PHP的GD模塊開啟。在Windows中為php_gd2.dll,Linux中為gd.so。將壓縮文件解壓到Apache。
該軟件支持PHP4和PHP5兩個(gè)版本,本文中使用的是PHP5的版本。在使用前注意要將PHP的GD模塊開啟。在Windows中為php_gd2.dll,Linux中為gd.so。將壓縮文件解壓到Apache,執(zhí)行http://localhost/barcode/index.php。PHP5生成條形碼見下圖:
1. Type:選擇條形碼類型
2. Output:輸出的圖片格式
3. Thickness:條形碼高度
4. Resolution:條形碼大小
5. Font:條形碼下方的文字大小,也可不顯示文字
6. Text:條形碼打印的內(nèi)容

當(dāng)然,這個(gè)PHP5生成條形碼程序只是將文字生成為條形碼,但使用時(shí)不能靈活將其嵌入其他PHP程序,我將壓縮包里面的test.php做了一些調(diào)整,使其能靈活的用于其他程序。運(yùn)行時(shí)只需將條碼類型和文字傳給test.php即可,例如:
http://localhost/barcode/test.php?codebar=BCGcode39&text=20090729
或運(yùn)行 http://localhost/barcode/mytest.php
mytest.php代碼:
- <img src="test.php?codebar=BCGcode39&text=20090729">
PHP5生成條形碼效果圖:

PHP5生成條形碼test.php代碼:
- <?php
-
require('class/BCGFont.php'); require('class/BCGColor.php'); require('class/BCGDrawing.php'); $codebar = $_REQUEST['codebar']; include('class/'.$codebar.'.barcode.php'); $font = new BCGFont('./class/font/Arial.ttf', 10); $color_black = new BCGColor(0, 0, 0); $color_white = new BCGColor(255, 255, 255); $code = new $codebar(); $code->setScale(2); $code->setThickness(30); $code->setForegroundColor($color_black); $code->setBackgroundColor($color_white); $code->setFont($font); $text = $_REQUEST['text']; $code->parse($text); $drawing = new BCGDrawing('', $color_white); $drawing->setBarcode($code); $drawing->draw(); header('Content-Type: image/png'); $drawing->finish(BCGDrawing::IMG_FORMAT_PNG); ?> 在運(yùn)行過(guò)程中出現(xiàn)異常";
throw $e; //重?cái)S異常
}
}
?﹥
【編輯推薦】
- PHP 5中新增加日期(date)函數(shù)的常量簡(jiǎn)介
- 在PHP中如何判斷AJAX請(qǐng)求
- 如何書寫安全的PHP代碼
- PHP5中的異常處理詳解
- 實(shí)戰(zhàn)PHP5+Apache2.2+MySQL5.1配置經(jīng)歷