The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 19, 2013, 10:29:00 AM

Login with username, password and session length


Pages: 1 [2] 3
  Print  
Author Topic: Barcode generator using PHP and GD  (Read 25946 times)
danross
n00b
*
Offline Offline

Posts: 3


View Profile
« Reply #15 on: August 18, 2011, 03:04:30 PM »

Hello, I had the same issue with the zero code when scanned shows a dash.  I swapped the array keys for the 0 and the - and this fixed the problem.  Seems the code for the zero is actually the dash.  Just wanted to pass that along.  Thanks for the code, it works great!!

Also, I had to add some additional code to the bottom of the function to get it to work correctly.  The switch block at the bottom of the code needs break statements after each case block.

    switch($this->mode) {
      case 'gif': imagegif($im, $fileName);
      break;
      case 'png': imagepng($im, $fileName);
      break;
      case 'jpeg': imagejpeg($im, $fileName);
      break;
      case 'wbmp': imagewbmp($im, $fileName);
      break;
    }
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #16 on: August 18, 2011, 07:29:18 PM »

Thanks much Dan, very nice.

So you're saying that dash and zero are the same? So simply outputting a zero instead of a dash is accurate?

LOL @ breaks - really? I missed that? What an idiot  ROFLMAO Thanks, good catch. I think I grabbed the code from my editor then prettied it up for display here and um, the forum code dropped the breaks. YEAH! That's it! It's the forum code!  ROFLMAO ROFLMAO
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
danross
n00b
*
Offline Offline

Posts: 3


View Profile
« Reply #17 on: August 19, 2011, 10:54:04 AM »

Hi, Thanks for the Thanks, glad to help.  What I did to fix the dash vs. zero issue was just swap the array keys for the barcode string.  I just did this on a hunch and it worked.

So the code was:
 '0'=>'000110100'
 '-'=>'010000101'

Now is:
'-'=>'000110100'
'0'=>'010000101'

Now the barcode scans correctly.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #18 on: August 19, 2011, 02:21:54 PM »

That's excellent. I copied those bits from a 39s website and confirmed on another ... I wonder if that's a common error.

Below then is the updated code, including your 0->dash mod and breaks in the switch D'oh!, as well as a little default catcher in that same switch

<?php

$bc 
= new barCode();
$bc->build('hello world');

class 
barCode
{
	
public 
$bcHeight$bcThinWidth$bcThickWidth$bcFontSize$mode;
	

	
function 
__construct($mode='gif'$height=50$thin=2$thick=3$fSize=2)
	
{
	
	
$this->bcHeight $height;
	
	
$this->bcThinWidth $thin;
	
	
$this->bcThickWidth $this->bcThinWidth $thick;
	
	
$this->fontSize $fSize;
	
	
$this->mode $mode;
	
	
$this->outMode = array('gif'=>'gif''png'=>'png''jpeg'=>'jpeg''wbmp'=>'vnd.wap.wbmp');
	
	
$this->codeMap = array(
	
	
	
'0'=>'010000101',
	
'1'=>'100100001',
	
'2'=>'001100001',
	
'3'=>'101100000',
	
	
	
'4'=>'000110001',
	
'5'=>'100110000',
	
'6'=>'001110000',
	
'7'=>'000100101',
	

	
	
	
'8'=>'100100100',
	
'9'=>'001100100',
	
'A'=>'100001001',
	
'B'=>'001001001',
	
	
	
'C'=>'101001000',
	
'D'=>'000011001',
	
'E'=>'100011000',
	
'F'=>'001011000',
	
	
	
'G'=>'000001101',
	
'H'=>'100001100',
	
'I'=>'001001100',
	
'J'=>'000011100',
	
	
	
'K'=>'100000011',
	
'L'=>'001000011',
	
'M'=>'101000010',
	
'N'=>'000010011',
	
	
	
'O'=>'100010010',
	
'P'=>'001010010',
	
'Q'=>'000000111',
	
'R'=>'100000110',
	
	
	
'S'=>'001000110',
	
'T'=>'000010110',
	
'U'=>'110000001',
	
'V'=>'011000001',
	
	
	
'W'=>'111000000',
	
'X'=>'010010001',
	
'Y'=>'110010000',
	
'Z'=>'011010000',
	
	
	
' '=>'011000100',
	
'$'=>'010101000',
	
'%'=>'000101010',
	
'*'=>'010010100',
	
	
	
'+'=>'010001010',
	
'-'=>'000110100',
	
'.'=>'110000100',
	
'/'=>'010100010'
	
	
	
);        
	
}
	

	
public function 
build($text=''$showText=false$fileName=null)
	
{
	
	
if (
trim($text) <= ' '
	
	
	
throw new 
exception('barCode::build - must be passed text to operate');
	
	
if (!
$fileType $this->outMode[$this->mode]) 
	
	
	
throw new 
exception("barCode::build - unrecognized output format ({$this->mode})");
	
	
if (!
function_exists("image{$this->mode}")) 
	
	
	
throw new 
exception("barCode::build - unsupported output format ({$this->mode} - check phpinfo)");
	
	

	
	
$text  =  strtoupper($text);
	
	
$dispText "* $text *";
	
	
$text "*$text*"// adds start and stop chars
	
	
$textLen  =  strlen($text); 
	
	
$barcodeWidth  =  $textLen * ($this->bcThinWidth $this->bcThickWidth) - $this->bcThinWidth
	
	
$im imagecreate($barcodeWidth$this->bcHeight); 
	
	
$black imagecolorallocate($im000); 
	
	
$white imagecolorallocate($im255255255); 
	
	
imagefill($im00$white); 
	
	

	
	
$xpos 0;
	
	
for (
$idx=0$idx<$textLen$idx++)
	
	

	
	
	
if (!
$char $text[$idx]) $char '-';
	
	
	
for (
$ptr=0$ptr<=8$ptr++)
	
	
	

	
	
	
	
$elementWidth = ($this->codeMap[$char][$ptr]) ? $this->bcThickWidth $this->bcThinWidth
	
	
	
	
if ((
$ptr 1) % 2
	
	
	
	
	
imagefilledrectangle($im$xpos0$xpos $elementWidth-1$this->bcHeight$black); 
	
	
	
	
$xpos += $elementWidth
	
	
	
}
	
	
	
$xpos += $this->bcThinWidth
	
	
}
	
	

	
	
if (
$showText)
	
	
{
	
	
	
$pxWid imagefontwidth($this->fontSize) * strlen($dispText) + 10;
	
	
	
$pxHt imagefontheight($this->fontSize) + 2;
	
	
	
$bigCenter $barcodeWidth 2;
	
	
	
$textCenter $pxWid 2;
	
	
	
imagefilledrectangle($im$bigCenter $textCenter$this->bcHeight $pxHt$bigCenter $textCenter$this->bcHeight$white);
	
	
	
imagestring($im$this->fontSize, ($bigCenter $textCenter) + 5, ($this->bcHeight $pxHt) + 1$dispText$black);
	
	
}

	
	
$badMode false;
	
	
if (!
$fileNameheader("Content-type:  image/{$fileType}");
	
	
switch(
$this->mode)
	
	
{
	
	
	
case 
'gif'
	
	
	
	
imagegif($im$fileName);
	
	
	
	
break;
	
	
	
case 
'png'
	
	
	
	
imagepng($im$fileName);
	
	
	
	
break;
	
	
	
case 
'jpeg'
	
	
	
	
imagejpeg($im$fileName);
	
	
	
	
break;
	
	
	
case 
'wbmp'
	
	
	
	
imagewbmp($im$fileName);
	
	
	
	
break;
	
	
	
default:
	
	
	
	
$badMode true;
	
	
}

	
	
imagedestroy($im); 
	
	

	
	
if (
$badMode
	
	
	
throw new 
Exception("barCode: Unknown Graphics Type '{$this->mode}'");
	
}
}

?>
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
pj1985
n00b
*
Offline Offline

Posts: 1


View Profile
« Reply #19 on: September 24, 2011, 04:26:25 PM »

Not sure if this is a stupid question but I want to display some text below the barcode other than the input text. Perhaps like an item name. It seems that every time I even try to echo some text I get an error. Any help would be great.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #20 on: September 25, 2011, 12:34:42 PM »

The way this code is set up, its designed to create a graphic that is pulled from a server like any other graphic for a web page. So to to what you're asking needs a bit more code because any text echo'd out during the example above would upset PHP as well as the browser.

So consider this sequence:

The browser asks for the url, "/productDetail.php?itemid=1234"

The code at the server might look something like this:

/productDetail.php
<?php

$itemID 
$_GET['itemid'];

// Now I'm going to simulate going to a database to get the product description
$db = new dbConnection('127.0.0.1''username''password''thedatabase');
$descrip $db->singleAnswer("select description from inventory_table where itemid=$itemID");

// Send out the HTML ...
echo <<<HTML
<html>
<body>
<img src="/graphics/createBarcode.php?itemid=1234"><br>
$descrip
</body>
</html>
HTML;

?>

As you can see I've put the description of the item into the HTML under a request for the barcode image.

Now we need to implement the barcode code into our project with a script to answer for the graphic.

/graphics/createBarcode.php
<?php

// NOTE: This assumes that you've created a file called class.barcode.php, and you don't have the
// $bc = bit at the top, it is JUST a class file
include "classes/class.barcode.php";
$bc = new barCode();
$bc->build($_GET['itemid']);

?>

Hope this clears it up a bit. It's a little complicated, but once your mind is around it it's not bad.
« Last Edit: September 25, 2011, 12:37:09 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
purefovmew
n00b
*
Offline Offline

Posts: 1


View Profile
« Reply #21 on: January 04, 2012, 01:04:05 AM »

I tried putting it in a html tag but it does not work. Any idea why?

Also, i tried to save the barcode as a picture but i couldn't. The default format is php. Can someone help me?
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #22 on: January 04, 2012, 10:07:24 AM »

Can you post here how you placed the HTML tag?

If you couldn't save it as a picture either then perhaps the problem is not having GD installed correctly on your server.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Phaėton
Lifer
*****
Offline Offline

Posts: 544


⎝⏠⏝⏠⎠


View Profile
« Reply #23 on: January 05, 2012, 12:51:07 AM »

nice!

Logged

When I was your age we used to walk to the TV to change the channel....  _̴ı̴̴̡̡̡ ̡͌l̡̡̡ ̡͌l̡*̡̡ ̴̡ı̴̴̡ ̡̡͡|̲̲̲͡͡͡ ̲▫̲͡ ̲̲̲͡͡π̲̲͡͡ ̲̲͡▫̲̲͡͡ ̲|̡̡̡ ̡ ̴̡ı̴̡̡
jocsism
n00b
*
Offline Offline

Posts: 1


View Profile
« Reply #24 on: January 10, 2012, 07:12:45 AM »



sir perkiset i just saw this php barcode and spike test it and it all went well..but i guess my test werent enough so if you found more behavior of this code plss let us know. but all in all this php code is easy to use and integrate to another pages. thanks a lot sir it really helped. im going to use this for my asset management system..if it happens for you to have further suggestions regarding the use of barcodes im looking forward for updates. hehe

thanks and more knowledge sir!
-jocsism
Logged
Mikru
n00b
*
Offline Offline

Posts: 1


View Profile
« Reply #25 on: June 06, 2012, 04:02:30 PM »

The problem of replacing 0 with - is because of line
Code:
if (!$char = $text[$idx]) $char = '-';

in fact the 0 is same as false value in php so to solve this problem you should simply remove this "if" or replace it with
Code:
$char = $text[$idx];
if (!$char && $char != 0) $char = '-';

And the problem of not displaying the barcode - make sure that in createBarcode.php file there is no any echo or even whitespace after php closing tag or before opening one. the ?> can be ommited if the file contains only php code
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #26 on: June 06, 2012, 06:13:46 PM »

Nice.

Welcome to The Cache, Mikru.

Interesting that you've posted here, I've been working for the last few days on attaching a bar code scanner to an iPad and have referenced this thread. In fact, I'm going to rebuild this class in JS with the HTML5 Canvas for a little project I'm working on. I'll look forward to your thoughts.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nass_a_b
n00b
*
Offline Offline

Posts: 2


View Profile
« Reply #27 on: October 31, 2012, 05:01:58 AM »

Hello,
I am a newbie, so please bare with my question.

Can you tell me in steps what I need to do to make the code work.

I tried putting the code in a .php file and recall it, but I keep getting all kinds of errors (Parse errors and others).
And how can I make the code generate passed text to it.

Thank you.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #28 on: November 01, 2012, 12:55:06 PM »

First, if you copied it directly from this site and pasted into a file, it may contain text gremlins. These are either control characters or CRLFs that do not work with your system. For example, if you are viewing this on a Windows box, you'll probably get a complete CHR(10) + CHR(13) line break. If you then posted that into a Linux box and try to run the code, it will fail because the CHR(13) is forbidden.

Second is how you are calling it. This routine is used as a class to be called from other routines to generate the barcode. It also requires that you have GD installed. If you do not, you'll get all kinds of parse and missing function errors.

If you could paste the errors your are having then perhaps we can further assist.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nass_a_b
n00b
*
Offline Offline

Posts: 2


View Profile
« Reply #29 on: November 02, 2012, 09:44:32 PM »

Thank you perkiset for the reply,

I copied the code from this site to windows based text editor, and when I tried to run the code, I got the following error:

Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in c:\program files\easyphp1-7\www\testbarcodeclass.php on line 8

The Code until line 8 on my editor is:



<?php

$bc = new barCode();
$bc->build('hello world');

class barCode
{
   public $bcHeight, $bcThinWidth, $bcThickWidth, $bcFontSize, $mode;



Thank you for your kind help.
Logged
Pages: 1 [2] 3
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!