The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. July 29, 2010, 08:01:37 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Barcode generator using PHP and GD  (Read 946 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 8649



View Profile
« on: January 11, 2010, 07:05:17 PM »

Here is a little class I put together to generate bar codes on the fly. It is reasonably well tested, but not thoroughly. It does a bit of work to try to protect you from doing something stupid (like output with an unsupported format) but again, not a lot. These barcodes should be readable by any standard barcode scanner.

It is quite simple. Create an instance of the class (either supplying the initial geometry of the barcode or letting the class set defaults) then build a code, passing the text, optionally showing the text value at the base of the code and optionally to a file or streamed directly out.

Options
  • mode: Output file type. GIF, PNG, JPEG, WBMP. Default is GIF.
  • bcThinWidth: the pixel width of the thin line in the code. Default is 2px.
  • bcThickWidth: multiple of thin line for thick lines. Default is 3.
  • bcHeight: overall height of the output image. Default is 50px
  • bcFontSize: stock GD font size. Default is 2 (about a 12px)
  • showText: Display the input text in a small white box, centered at the bottom of the image. Default is false
  • fileName: If this parameter is passed, then the output of the function will be into a file. Otherwise (default) it will be streamed directly back out to the browser.

Here are a couple examples with the Show Text option set on:






Enjoy!

<?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'=>'000110100',
	
'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',
	
'-'=>'010000101',
	
'.'=>'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);
	
	
}

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

	
	
imagedestroy($im); 
	
	

	
}
}

?>
« Last Edit: January 11, 2010, 08:07:08 PM by perkiset » Logged

It's the things you learn after you know it all that really count.
-John Wooden
nutballs
Administrator
Lifer
*****
Online Online

Posts: 5296


DAMN YOU!!!!!!!


View Profile
« Reply #1 on: January 11, 2010, 07:30:56 PM »

Any particular code type?
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 8649



View Profile
« Reply #2 on: January 11, 2010, 07:33:26 PM »

Um, yeah. Lines.  ROFLMAO I dunno which encoding this is ... I scoffed the bitmaps from another site. I'll look it up.
Logged

It's the things you learn after you know it all that really count.
-John Wooden
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 8649



View Profile
« Reply #3 on: January 11, 2010, 07:45:59 PM »

It's Code39. You may check my work here:

h++p : / / everything2 dot com/title/three+of+nine
Logged

It's the things you learn after you know it all that really count.
-John Wooden
isthisthingon
Global Moderator
Lifer
*****
Online Online

Posts: 2368


Cooperation > Competition


View Profile
« Reply #4 on: January 11, 2010, 09:04:40 PM »

ZXing ("Zebra Crossing")



Open source barcode image processing library for Java and Android.  Handy since the built-in camera that acts as a barcode scanner is typically built into the device Smiley  If nothing else perks, perhaps their code would be helpful to enhance your PHP 

http://code.google.com/p/zxing/
Logged

~ The best things in life are free ~
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 8649



View Profile
« Reply #5 on: January 11, 2010, 09:58:37 PM »

Thanks ITTO Smiley

Loves me some simple GD and Code3of9 though. No Java no more for me Wink
Logged

It's the things you learn after you know it all that really count.
-John Wooden
isthisthingon
Global Moderator
Lifer
*****
Online Online

Posts: 2368


Cooperation > Competition


View Profile
« Reply #6 on: January 11, 2010, 11:28:57 PM »

>No Java no more for me

You think Java's more challenging than people or multi-corp tax friendly business strategies?  IMO, it gives those who parade around as "brilliant" an alibi for failing at social life Smiley  Java only recognizes someone as an "expert" if they surrender enough of themselves to earn the title 
Logged

~ The best things in life are free ~
kurdt
Lifer
*****
Offline Offline

Posts: 999


it's all part of the plan


View Profile
« Reply #7 on: January 12, 2010, 12:14:28 AM »

Great piece of code Smiley

Now go and make me PHP code that does these little things: http://techtv.mit.edu/collections/newsoffice/videos/3489-bokode-imperceptible-visual-tags-for-camera-based-interaction-from-a-distance

Then I'll be all Praise Praise
Logged

Coming up with new features is easy - inventing features that replace old features is hard.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 8649



View Profile
« Reply #8 on: January 12, 2010, 09:06:19 AM »

You think Java's more challenging than people or multi-corp tax friendly business strategies?  IMO, it gives those who parade around as "brilliant" an alibi for failing at social life Smiley  Java only recognizes someone as an "expert" if they surrender enough of themselves to earn the title 
Too true. I recall a certain egghead in our past (20 years ago now. Hows THAT to f*&k up your morning coffee?) that hangs there to this day. Good for him and they that choose it, but I really don't like it one bit at all.

I enjoy this "life" thing. Smiley
Logged

It's the things you learn after you know it all that really count.
-John Wooden
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 8649



View Profile
« Reply #9 on: January 12, 2010, 09:08:40 AM »

Been there, done that. Pain in the ass and I couldn't get my warehouse workers to be able to identify it without a whole lot of training on how to squint their eyes correctly, so I gave up. Simple binary for me  ROFLMAO
Logged

It's the things you learn after you know it all that really count.
-John Wooden
krustee
Rookie
**
Offline Offline

Posts: 31


View Profile
« Reply #10 on: January 14, 2010, 04:56:32 AM »

Thanks for the code perks, I am actually putting it to good use as we speak.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 8649



View Profile
« Reply #11 on: January 14, 2010, 11:42:25 AM »

 Smiley glad to hear it K.
Logged

It's the things you learn after you know it all that really count.
-John Wooden
Pages: [1]
  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!