Forked from BHSPitMonkey/generate_code39_barcode.php
Created
September 13, 2011 15:35
-
-
Save ofus/1214127 to your computer and use it in GitHub Desktop.
A PHP function that generates a Code 39 type barcode in pure HTML/CSS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prints out a Code 39 barcode in HTML/CSS | |
// $string should be a Code 39-compliant string to encode. | |
// Any characters not in the array below will be discarded. | |
function print_code39_barcode($string) { | |
$code39 = array( | |
'0'=>'NnNwWnWnN', '1'=>'WnNwNnNnW', | |
'2'=>'NnWwNnNnW', '3'=>'WnWwNnNnN', | |
'4'=>'NnNwWnNnW', '5'=>'WnNwWnNnN', | |
'6'=>'NnWwWnNnN', '7'=>'NnNwNnWnW', | |
'8'=>'WnNwNnWnN', '9'=>'NnWwNnWnN', | |
'A'=>'WnNnNwNnW', 'B'=>'NnWnNwNnW', | |
'C'=>'WnWnNwNnN', 'D'=>'NnNnWwNnW', | |
'E'=>'WnNnWwNnN', 'F'=>'NnWnWwNnN', | |
'G'=>'NnNnNwWnW', 'H'=>'WnNnNwWnN', | |
'I'=>'NnWnNwWnN', 'J'=>'NnNnWwWnN', | |
'K'=>'WnNnNnNwW', 'L'=>'NnWnNnNwW', | |
'M'=>'WnWnNnNwN', 'N'=>'NnNnWnNwW', | |
'O'=>'WnNnWnNwN', 'P'=>'NnWnWnNwN', | |
'Q'=>'NnNnNnWwW', 'R'=>'WnNnNnWwN', | |
'S'=>'NnWnNnWwN', 'T'=>'NnNnWnWwN', | |
'U'=>'WwNnNnNnW', 'V'=>'NwWnNnNnW', | |
'W'=>'WwWnNnNnN', 'X'=>'NwNnWnNnW', | |
'Y'=>'WwNnWnNnN', 'Z'=>'NwWnWnNnN', | |
'-'=>'NwNnNnWnW', '.'=>'WwNnNnWnN', | |
' '=>'NwWnNnWnN', '$'=>'NwNwNwNnN', | |
'/'=>'NwNwNnNwN', '+'=>'NwNnNwNwN', | |
'%'=>'NnNwNwNwN', '*'=>'NwNnWnWnN'); | |
echo "<style type='text/css'>div.barcode{height:40px;margin:0 auto;padding:0;}div.barcode .black{background-color:black;}div.barcode .white{background-color:white;}div.barcode .narrow{width:1px;}div.barcode .wide{width:3px;}div.barcode div{height:100%;float:left;margin:0;padding:0;}</style>\n"; | |
echo "<div class='barcode'>"; | |
// Split the string up into its separate characters and iterate over them | |
if (substr($string,0,1)!='*') $string = "*$string"; | |
if (substr($string,-1,1)!='*') $string = "$string*"; | |
$string = strtoupper($string); | |
$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); | |
foreach ($chars as $char) { | |
// Split this character's encoding string up into its separate characters and iterate | |
$pattern = preg_split('//', $code39[$char], -1, PREG_SPLIT_NO_EMPTY); | |
foreach ($pattern as $bar) { | |
// Determine bar's class | |
switch ($bar) { | |
case 'W': $classes = "wide black"; break; | |
case 'N': $classes = "narrow black"; break; | |
case 'w': $classes = "wide white"; break; | |
case 'n': $classes = "narrow white"; break; | |
} | |
// Print bar | |
echo "<div class='$classes'></div>"; | |
} | |
// Separator between characters | |
echo "<div class='narrow white'></div>"; | |
} | |
echo "</div>\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment