Jump to content

Code 39 Barcodes


Dysan

Recommended Posts

The following code is my attempt at creating a code 39 barcode, based on what characters a string contains.

 

For some reason, when the generated barcode is printered and scanner using a barcode scanner, the barcode doesn't scan - Why is this?

 

<?php
$rectangle_width = 2;
$rectangle_height = 20;

$image = imagecreatetruecolor(500, 182);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);

$find = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*');
$replace = array('00110000', '00110001', '00110010', '00110011', '00110100', '00110101', '00110110', '00110111', '00111000', '00111001', '00101010');

$string = '*1101*';
$textstring = str_replace($find, $replace, $string);

$j = 0;
$sarray = str_split($textstring, 1);
for($i = 0; $i < count($sarray); $i++)
{
    if($sarray[$i] ==  '0')
    {
        imagefilledrectangle($image, $rectangle_width * $j, 0, ($rectangle_width * $j) + $rectangle_width, $rectangle_height, $white);
        $j++;
        
    }
    elseif($sarray[$i] ==  '1')
    {
        imagefilledrectangle($image, $rectangle_width * $j, 0, ($rectangle_width * $j) + $rectangle_width, $rectangle_height, $black);
        $j++;
    }
} 

header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>

Link to comment
Share on other sites

I think this is to do with either the bar code coming out totally wrong or the scanner don't work.

 

What does the bar code look like when it prints

I'm quite sure your barcode is not being generated properly - I'm messing around with some code at the moment, I'll come back in a bit with a possible solution...

Link to comment
Share on other sites

Ok, try this out...

<?php
$thin   = 4; //Width of thin bars in pixels
$thick  = 12; //Width of thick bars in pixels
$space  = 2; //Width of space between bars

$image = imagecreatetruecolor(500,182); //Create the canvas
$black = imagecolorallocate($image,0,0,0); //Set the black colour
$white = imagecolorallocate($image,255,255,255); //Set the white colour
imagefill($image,0,0,$white); //Fill the canvas with white

/*Build an array of characters and their corresponding black (BAR) and white (SPC) bar, 0: Thin, 1: Thick
E.g. 6 equates to thin black, thin white, thick black, thick white, thick black, thin white, thin black, thin white, thin black.*/
$char = array("1" => array("BAR" => "10001", "SPC" => "0100"),
              "2" => array("BAR" => "01001", "SPC" => "0100"),
              "3" => array("BAR" => "11000", "SPC" => "0100"),
              "4" => array("BAR" => "00101", "SPC" => "0100"),
              "5" => array("BAR" => "10100", "SPC" => "0100"),
              "6" => array("BAR" => "01100", "SPC" => "0100"),
              "7" => array("BAR" => "00011", "SPC" => "0100"),
              "8" => array("BAR" => "10010", "SPC" => "0100"),
              "9" => array("BAR" => "01010", "SPC" => "0100"),
              "0" => array("BAR" => "00110", "SPC" => "0100"),
              "*" => array("BAR" => "00110", "SPC" => "1000")
             );

$str = '*1234*'; //The string to convert
$c   = str_split($str); //The string converted to an array, each index holds a single character
$x   = 0; //Set the x-offset at zero for positioning the first bar, used later for tracking

foreach($c as $d) { //Loop through each of the single characters of the input string
    //Build each black/white bar combo, black[0]/white[0], black[1]/white[1], etc.
    for($i=0; $i<5; $i++) { //Loop 5 times, as there are 5 black bars in each character
        $w = ($char[$d]['BAR']{$i} == "0") ? $thin : $thick; //Check if the current black bar is thick(1) or thin(0) and store width 
        imagefilledrectangle($image,$x,0,$x+$w,182,$black); //Draw the bar with the correct offset and width
        $x += $w+$space; //Add the width and a space to the offset tracker
        //Repeat for white bar, there are only 4 white bars per character
        if($i < 4) {
            $w = ($char[$d]['SPC']{$i} == "0") ? $thin : $thick;
            imagefilledrectangle($image,$x,0,$x+$w,182,$white);
            $x += $w+$space;
        }
    }
}
//Output image to browser
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>

 

There are some obvious improvements that can be made to this, for example, working out the width of the barcode in pixels before creation so that the image created at the correct size, that and the inclusion of the non numeric characters available in the Code 39 specification.

Link to comment
Share on other sites

How do I make the generated barcode small in width and height, as it's to big for the barcode scanner?

$thin is the width of the thin bars, $thick is the width of the thick bars, $space is the width between bars. Change every instance of 182 to change the height. $space should be less than $thin and $thick should be three times the width of $thin.

Link to comment
Share on other sites

This code works, the barcodes printed scan successfully: http://www.sid6581.net/cs/php-scripts/barcode/barcode.phps

 

What do I need to change in my code, in order to get my method to work?

 

$rectangle_width = 2;
$rectangle_height = 20;

$image = imagecreatetruecolor(500, 182);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);

$find = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*');
$replace = array('00110000', '00110001', '00110010', '00110011', '00110100', '00110101', '00110110', '00110111', '00111000', '00111001', '00101010');

$string = '*1101*';
$textstring = str_replace($find, $replace, $string);

$j = 0;
$sarray = str_split($textstring, 1);
for($i = 0; $i < count($sarray); $i++)
{
    if($sarray[$i] ==  '0')
    {
        imagefilledrectangle($image, $rectangle_width * $j, 0, ($rectangle_width * $j) + $rectangle_width, $rectangle_height, $white);
        $j++;
        
    }
    elseif($sarray[$i] ==  '1')
    {
        imagefilledrectangle($image, $rectangle_width * $j, 0, ($rectangle_width * $j) + $rectangle_width, $rectangle_height, $black);
        $j++;
    }
} 

header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>

Link to comment
Share on other sites

The following is code I have developed based on your code.

 

Why is the barcode for value/string "000000" bigger than the barcodes for value/string "111111" to "999999" in width?

How do I display each barcode at the same width? - Your code does exactly this, why doesn't mine?

 

<?php
$rectangle_width = 2;
$rectangle_height = 20;

$image = imagecreatetruecolor(500, 182);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);

$find = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*');
$replace = array('1010001110111010', '1110100010101110', '1011100010101110', '1110111000101010', '1010001110101110', '1110100011101010', '1011100011101010', '1010001011101110', '1110100010111010', '1011100010111010', '1000101110111010');

$string = '*284861*';
$textstring = str_replace($find, $replace, $string);

$j = 0;
$sarray = str_split($textstring, 1);
for($i = 0; $i < count($sarray); $i++)
{
    if($sarray[$i] ==  '0')
    {
        imagefilledrectangle($image, $rectangle_width * $j, 0, ($rectangle_width * $j) + $rectangle_width, $rectangle_height, $white);
        $j++;
        
    }
    elseif($sarray[$i] ==  '1')
    {
        imagefilledrectangle($image, $rectangle_width * $j, 0, ($rectangle_width * $j) + $rectangle_width, $rectangle_height, $black);
        $j++;
    }
} 

header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>

Link to comment
Share on other sites

Is it possible to display all barcodes generated at the same width, as if 000000 is entered as the string, the barcodes generated is larger in width than the barcode generated for the string 111111?

 

Any help would be much appreciated!!!!

 

How do I display each barcode at the same width? - Your code does exactly this, why doesn't mine?

 

I would have thought THAT was all the help you needed. His works, yours doesn't. Study his code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.