Dysan Posted December 13, 2007 Share Posted December 13, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 how does this have anything to do with PHP??? your problem is getting it into the format of your reader which we are clueelss on Quote Link to comment Share on other sites More sharing options...
thefollower Posted December 13, 2007 Share Posted December 13, 2007 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 Quote Link to comment Share on other sites More sharing options...
zq29 Posted December 13, 2007 Share Posted December 13, 2007 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... Quote Link to comment Share on other sites More sharing options...
zq29 Posted December 13, 2007 Share Posted December 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
Dysan Posted December 13, 2007 Author Share Posted December 13, 2007 How do I make the generated barcode small in width and height, as it's to big for the barcode scanner? Quote Link to comment Share on other sites More sharing options...
zq29 Posted December 13, 2007 Share Posted December 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
Dysan Posted December 13, 2007 Author Share Posted December 13, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
Dysan Posted December 13, 2007 Author Share Posted December 13, 2007 Is it possible you can explain whats going on in your code, plus use proper variable names, instead of just $c etc? Quote Link to comment Share on other sites More sharing options...
zq29 Posted December 13, 2007 Share Posted December 13, 2007 Is it possible you can explain whats going on in your code, plus use proper variable names, instead of just $c etc? I have now commented my previous post. Hope that explains it better! Quote Link to comment Share on other sites More sharing options...
Dysan Posted December 13, 2007 Author Share Posted December 13, 2007 Is it possible to get the script to include the *'s at the beginning and end of the string, without physically typing them in? Quote Link to comment Share on other sites More sharing options...
zq29 Posted December 14, 2007 Share Posted December 14, 2007 Assuming you have an input form of some kind, just concatenate the strings... <?php $str = "*".$_POST['input']."*"; ?> Quote Link to comment Share on other sites More sharing options...
Dysan Posted December 14, 2007 Author Share Posted December 14, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
Dysan Posted December 16, 2007 Author Share Posted December 16, 2007 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!!!! Quote Link to comment Share on other sites More sharing options...
AndyB Posted December 16, 2007 Share Posted December 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.