Clase para generar códigos de barras
| From: | Fernando Herrero Peletero | Date: | Thu, 10 Jun 2004 22:34:00 +0000 |
| Subject: | Clase para generar códigos de barras | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-49321@lists.php.net to get a copy of this message | ||
Source Code of index.php:
<?
switch($_SERVER['REQUEST_METHOD']) {
case 'GET':
if (array_key_exists('source', $_GET)) {
echo 'Source Code of <a
href=index.php>index.php</a>:<hr>';
highlight_file('index.php');
echo '<hr>Source Code of form.html:<hr>';
highlight_file('form.html');
echo '<hr>Source Code of ean13.inc.php:<hr>';
highlight_file('ean13.inc.php');
echo '<hr>Source Code of ps_ean13.txt:<hr>';
highlight_file('ps_ean13.txt');
echo '<hr>Source Code of ps_supp.txt:<hr>';
highlight_file('ps_supp.txt');
} else {
include('form.html');
}
break;
case 'POST':
require_once('ean13.inc.php');
$barcode = new EAN13($_POST['number_system'],
$_POST['manufacturer_code'], $_POST['product_code'],
$_POST['supplemental']);
$resolution = ((array_key_exists('preview', $_POST)) ?
($_POST['resolution']) : (0));
$barcode->output_barcode('', 'D', $resolution);
break;
default:
}
?>
Source Code of form.html:
<html>
<head>
<script language="javascript">
function set_preview() {
document.ean13.resolution.disabled =
!document.ean13.preview.checked;
}
function get_barcode() {
document.ean13.method = 'post';
document.ean13.action = 'index.php';
document.ean13.submit();
}
</script>
</head>
<body>
<form name="ean13">
<input type="text" name="number_system" value=""
size="3"
maxlength="2">
<input type="text" name="manufacturer_code" value=""
size="6"
maxlength="5">
<input type="text" name="product_code" value=""
size="6"
maxlength="5">
<input type="text" name="supplemental" value=""
size="6"
maxlength="5"><br>
Preview: <input type="checkbox" name="preview"
onChange="set_preview()">
<select name="resolution" disabled>
<option value="72">72</option>
<option value="150">150</option>
<option value="300">300</option>
</select>
<input type="button" value="Get BarCode"
onClick="get_barcode()">
</form>
<br>
<a href=index.php?source>View Source Code</a>
</body>
</html>
Source Code of ean13.inc.php:
<?
// Functions from PHP5
if (substr(PHP_VERSION, 0, 1) < 5) {
function str_split($str, $len = 1) {
return explode('¬', wordwrap($str, $len, '¬', true));
}
}
// Class for creating EAN13 barcodes
class EAN13 {
var $message;
var $number_system;
var $manufacturer;
var $product;
var $checksum;
var $lh_coded;
var $rh_coded;
var $supplemental;
var $supp_checksum;
var $supp_coded;
// EAN Parity Encodig Table => EAN_pet
var $EAN_pet = array(
0 => array('O', 'O', 'O', 'O', 'O',
'O'),
1 => array('O', 'O', 'E', 'O', 'E',
'E'),
2 => array('O', 'O', 'E', 'E', 'O',
'E'),
3 => array('O', 'O', 'E', 'E', 'E',
'O'),
4 => array('O', 'E', 'O', 'O', 'E',
'E'),
5 => array('O', 'E', 'E', 'O', 'O',
'E'),
6 => array('O', 'E', 'E', 'E', 'O',
'O'),
7 => array('O', 'E', 'E', 'E', 'O',
'E'),
8 => array('O', 'E', 'O', 'E', 'E',
'O'),
9 => array('O', 'E', 'E', 'O', 'E',
'O')
);
// EAN Character Set Encoding Table => EAN_cet
var $EAN_cet = array(
0 => array('O' => '0001101', 'E' =>
'0100111', 'R' =>
'1110010'),
1 => array('O' => '0011001', 'E' =>
'0110011', 'R' =>
'1100110'),
2 => array('O' => '0010011', 'E' =>
'0011011', 'R' =>
'1101100'),
3 => array('O' => '0111101', 'E' =>
'0100001', 'R' =>
'1000010'),
4 => array('O' => '0100011', 'E' =>
'0011101', 'R' =>
'1011100'),
5 => array('O' => '0110001', 'E' =>
'0111001', 'R' =>
'1001110'),
6 => array('O' => '0101111', 'E' =>
'0000101', 'R' =>
'1010000'),
7 => array('O' => '0111011', 'E' =>
'0010001', 'R' =>
'1000100'),
8 => array('O' => '0110111', 'E' =>
'0001001', 'R' =>
'1001000'),
9 => array('O' => '0001011', 'E' =>
'0010111', 'R' => '1110100')
);
// UPC 2-Digit Parity Pattern => UPC_2dpp
var $UPC_5dpp = array(
0 => array('O', 'O'),
1 => array('O', 'E'),
2 => array('E', 'O'),
3 => array('E', 'E')
);
// UPC 5-Digit Parity Pattern => UPC_5dpp
var $UPC_5dpp = array(
0 => array('E', 'E', 'O', 'O',
'O'),
1 => array('E', 'O', 'E', 'O',
'O'),
2 => array('E', 'O', 'O', 'E',
'O'),
3 => array('E', 'O', 'O', 'O',
'E'),
4 => array('O', 'E', 'E', 'O',
'O'),
5 => array('O', 'O', 'E', 'E',
'O'),
6 => array('O', 'O', 'O', 'E',
'E'),
7 => array('O', 'E', 'O', 'E',
'O'),
8 => array('O', 'E', 'O', 'O',
'E'),
9 => array('O', 'O', 'E', 'O', 'E')
);
// $number_system => unsigned int numerical 2 chars string
// $manufacturer, $product => unsigned int numerical 5 chars string
// $supplemental => optional unsigned int numerical 2 or 5 chars
string
function EAN13 ($number_system, $manufacturer, $product,
$supplemental = null) {
$this->message = $number_system . $manufacturer . $product;
$this->number_system = $number_system;
$this->manufacturer = $manufacturer;
$this->product = $product;
$this->supplemental = $supplemental;
$this->ean13_lh_encoding();
$this->ean13_rh_encoding();
if (strlen($this->supplemental) == 2 or
strlen($this->supplemental) == 5) {
$this->upc_encoding();
}
}
function get_barcode () {
$str0 = ((strlen($this->supplemental) == 2) ? (96) : (75));
$str0 = ((strlen($this->supplemental) == 5) ? (115) : ($str0));
$str1 = $this->lh_coded;
$str2 = $this->rh_coded;
$str3 = $this->number_system[0];
$str4 = wordwrap($this->number_system[1] . $this->manufacturer,
1, ') (', true);
$str5 = wordwrap($this->product . $this->checksum, 1, ') (',
true);
$barcode = sprintf(implode('', file('ps_ean13.txt')), $str0,
$str1, $str2, $str3, $str4, $str5);
if (strlen($this->supplemental) == 2 or
strlen($this->supplemental) == 5) {
$str0 = wordwrap($this->supplemental, 1, ') (', true);
$barcode .= sprintf(implode('', file('ps_supp.txt')),
$this->supp_coded, $str0);
}
$barcode .= "\nshowpage\n";
return $barcode;
}
function get_full_name () {
if (strlen($this->supplemental) == 2 or
strlen($this->supplemental) == 5) {
return $this->number_system . '-' . $this->manufacturer .
'-' . $this->product . '-' . $this->checksum . '-' .
$this->supplemental;
} else {
return $this->number_system . '-' . $this->manufacturer .
'-' . $this->product . '-' . $this->checksum;
}
}
function write_barcode ($file_name, $dpi = false) {
if (!@$handle = fopen($file_name, "wb")) {
$this->error('Unable to create output file: ' . $file_name);
}
fwrite($handle, $this->get_barcode());
fclose($handle);
if ($dpi) {
exec('epstool -t6p --doseps-reverse --dpi ' . $dpi . ' ' .
$file_name . ' ' . $file_name);
}
}
function output_barcode ($name = '', $dest = '', $dpi = false) {
global $HTTP_SERVER_VARS;
if (is_bool($dest)) {
$dest = (($dest) ? ('D') : ('F'));
}
$dest = strtoupper($dest);
if ($dest == '') {
if ($name == '') {
$dest = 'I';
} else {
$dest = 'F';
}
}
if ($name == '') {
$name = $this->get_full_name() . '.eps';
}
if (is_bool($dpi)) {
$dpi = (($dpi) ? (72) : (false));
}
switch ($dest) {
case 'I':
$tmp_file = tempnam('.', $name);
$this->write_barcode($tmp_file, $dpi);
if (isset($HTTP_SERVER_VARS['SERVER_NAME'])) {
header('Content-Type: application/eps');
if (headers_sent()) {
unlink($tmp_file);
$this->error('Some data has already been output
to browser, can\'t send PDF file');
}
header('Content-Length: ' . filesize($tmp_file));
header('Content-disposition: inline; filename=' .
$name);
}
readfile($tmp_file);
unlink($tmp_file);
return true;
break;
case 'D':
$tmp_file = tempnam('.', $name);
$this->write_barcode($tmp_file, $dpi);
if(isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']) and
strpos($HTTP_SERVER_VARS['HTTP_USER_AGENT'],'MSIE'))
header('Content-Type: application/force-download');
else
header('Content-Type: application/octet-stream');
if (headers_sent()) {
unlink($tmp_file);
$this->error('Some data has already been output to
browser, can\'t send PDF file');
}
header('Content-Length: ' . filesize($tmp_file));
header('Content-disposition: attachment; filename=' .
$name);
readfile($tmp_file);
unlink($tmp_file);
return true;
break;
case 'F':
$this->write_barcode($name, $dpi);
return true;
break;
case 'S':
return $this->buffer;
default:
$this->Error('Incorrect output destination: '.$dest);
return false;
}
}
function ean13_checksum () {
$this->checksum = 0;
foreach (str_split(strrev($this->message)) as $pos => $val) {
$this->checksum += $val * (3 - 2 * ($pos % 2));
}
$this->checksum = 10 - ($this->checksum % 10);
}
function ean13_lh_encoding () {
$this->lh_coded = '';
foreach (str_split($this->number_system[1] .
$this->manufacturer) as $pos => $val) {
$this->lh_coded .=
$this->EAN_cet[$val][$this->EAN_pet[$this->number_system[0]][$pos]];
}
$this->lh_coded = wordwrap($this->lh_coded, 1, ' ', true);
}
function ean13_rh_encoding () {
$this->ean13_checksum();
$this->rh_coded = '';
foreach (str_split($this->product . $this->checksum) as $pos =>
$val) {
$this->rh_coded .= $this->EAN_cet[$val]['R'];
}
$this->rh_coded = wordwrap($this->rh_coded, 1, ' ', true);
}
function upc_checksum () {
if (strlen($this->supplemental) == 2) {
$this->supp_checksum = $this->supplemental % 8;
} elseif (strlen($this->supplemental) == 5) {
$this->supp_checksum = 0;
foreach (str_split(strrev($this->supplemental)) as $pos =>
$val ) {
$this->supp_checksum += $val * (3 + 6 * ($pos % 2));
}
$this->supp_checksum = $this->supp_checksum % 10;
}
}
function upc_encoding () {
$this->upc_checksum();
$this->supp_coded = '1011';
foreach(str_split($this->supplemental) as $pos => $val) {
$this->supp_coded .=
$this->EAN_cet[$val][$this->UPC_5dpp[$this->supp_checksum][$pos]] . '01';
}
$this->supp_coded = wordwrap(substr($this->supp_coded, 0 , -2),
1, ' ', true);
}
function error ($msg) {
die('<B>FPDF error: </B>' . $msg);
}
}
?>
Source Code of ps_ean13.txt:
%%!PS-Adobe-3.0 EPSF-3.0
%%%%BoundingBox: 0 0 %s 36
/inch {72 mul} def
/size { 1 } def
/barwidth { .7 } def
/barheith { 26.7 } def
/senheith { 30.3 } def
/ypos { 8 } def
/xpos { 10 barwidth mul } def
/sentinel {
barwidth setlinewidth 0 setgray
count -1 roll dup count 3 sub add 1 exch {
count -1 roll 1 eq {
barwidth mul barwidth 2 div add xpos add ypos senheith sub
barheith add moveto
0 senheith rlineto stroke
} {
pop
} ifelse
} for
} def
/bars_ean13 {
barwidth setlinewidth 0 setgray
count -1 roll dup count 3 sub add 1 exch {
count -1 roll 1 eq {
barwidth mul barwidth 2 div add xpos add ypos moveto
0 barheith rlineto stroke
} {
pop
} ifelse
} for
} def
/text_ean13 {
count -1 roll dup count 3 sub 7 mul add 7 exch {
barwidth mul barwidth 2 div add xpos add ypos 7 sub moveto
count -1 roll show
} for
} def
size dup scale
0 1 0 1 sentinel
45 0 1 0 1 0 sentinel
92 1 0 1 sentinel
3 %s bars_ean13
50 %s bars_ean13
/Helvetica findfont 8 scalefont 0 setgray setfont
-9 (%s) text_ean13
3 (%s) text_ean13
50 (%s) text_ean13
Source Code of ps_supp.txt:
/bars_supp {
barwidth setlinewidth 0 setgray
count -1 roll dup count 3 sub add 1 exch {
count -1 roll 1 eq {
barwidth mul barwidth 2 div add xpos add ypos senheith sub
barheith add moveto
0 senheith 7 sub rlineto stroke
} {
pop
} ifelse
} for
} def
/text_supp {
count -1 roll dup count 3 sub 9 mul add 9 exch {
barwidth mul barwidth 2 div add xpos add ypos barheith add 6 sub
moveto
count -1 roll show
} for
} def
105 %s bars_supp
107 (%s) text_supp