Source for file Code39.php
Documentation is available at Code39.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Ryan Briones <ryanbriones@webxdesign.org> |
// +----------------------------------------------------------------------+
// Last Modified: 2003/12/09 - Ryan Briones
* Image_Barcode_Code39 creates Code 3 of 9 ( Code39 ) barcode images. It's
* implementation borrows heavily for the perl module GD::Barcode::Code39
* @author Ryan Briones <ryanbriones@webxdesign.org>
* @todo add a function to print images directly to browser not using the draw() function
require_once "Image/Barcode.php";
* the function str_split() has been including, not part of the class for
* it's ability to emulate Perl's split( //, $text ). This function was
* stolen from the PHP function documentation comments on PHP's str_split()
* which is to be included in PHP5.
* @param int number of characters you wish to split on
* @return array|false Returns an array or false when $num is less than 1
if($num < 1 ) return FALSE;
for ($j = 0; $j < strlen($str); $j= $j+ $num) {
$arr[] = substr($str,$j,$num);
class Image_Barcode_Code39 extends Image_Barcode {
var $_barcodeheight = 50;
var $_coding_map = array (
* @param int $wThin Width of the thin lines on the barcode
* @param int $wThick Width of the thick lines on the barcode
function Image_Barcode_Code39 ( $text = '', $wThin = 0 , $wThick = 0 ) {
// Check $text for invalid characters
if ( $this->checkInvalid ( $text ) ) {
if ( $wThin > 0 ) $this->_barthinwidth = $wThin;
if ( $wThick > 0 ) $this->_barthickwidth = $wThick;
* make an image resource using the GD image library
* @param bool $noText Set to true if you'd like your barcode to be sans text
* @param int $bHeight height of the barcode image including text
* @return resource The Barcode Image (TM)
function plot ( $noText = false , $bHeight = 0 ) {
// add start and stop * characters
$final_text = "*" . $this->text . "*";
$this->_barcodeheight = $bHeight;
foreach ( str_split( $final_text ) as $character ) {
$barcode .= $this->_dumpCode ( $this->_coding_map[$character] . '0' );
$barcode_len = strlen( $barcode );
// Create GD image object
$img = imagecreate( $barcode_len, $this->_barcodeheight );
// Allocate black and white colors to the image
// fill background with white color
// draw barcode bars to image
foreach ( str_split( $barcode ) as $character_code ) {
if ( $character_code == 0 ) {
imageline( $img, $xpos, 0 , $xpos, $this->_barcodeheight, $white );
imageline( $img, $xpos, 0 , $xpos, $this->_barcodeheight, $black );
foreach ( str_split( $barcode ) as $character_code ) {
if ( $character_code == 0 ) {
imageline( $img, $xpos, 0 , $xpos, $this->_barcodeheight - $font_height - 1 , $white );
imageline( $img, $xpos, 0 , $xpos, $this->_barcodeheight - $font_height - 1 , $black );
// draw text under barcode
( $barcode_len - $font_width * strlen( $this->text ) )/2 ,
$this->_barcodeheight - $font_height,
* send image to the browser; for Image_Barcode compaitbility
* @param string $imgtype Image type; accepts jpg, png, and gif, but gif only works if you've payed for licensing
* @param bool $noText Set to true if you'd like your barcode to be sans text
* @param int $bHeight height of the barcode image including text
* @return bool true or die
function draw ( $text, $imgtype = 'png', $noText = false , $bHeight = 0 ) {
// Check $text for invalid characters
if ( $this->checkInvalid ( $text ) ) {
$img = $this->plot ( $noText, $bHeight );
header("Content-type: image/gif");
header("Content-type: image/jpg");
header("Content-type: image/png");
* _dumpCode is a PHP implementation of dumpCode from the Perl module
* GD::Barcode::Code39. I royally screwed up when trying to do the thing
* my own way the first time. This way works.
* @param string $code Code39 barcode code
* @return string $result barcode line code
function _dumpCode ( $code ) {
$color = 1; // 1: Black, 0: White
// if $bit is 1, line is wide; if $bit is 0 line is thin
$result .= ( ( $bit == 1 ) ? str_repeat( " $color" , $this->_barthickwidth ) : str_repeat( " $color" , $this->_barthinwidth ) );
$color = ( ( $color == 0 ) ? 1 : 0 );
* check for invalid characters
* @return bool returns true when invalid characters have been found
function checkInvalid ( $text ) {
return preg_match( "/[^0-9A-Z\-*+\$%\/. ]/", $text );
Documentation generated on Mon, 11 Mar 2019 14:21:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|