Skip to content

Instantly share code, notes, and snippets.

@carlok
Last active December 17, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlok/5660903 to your computer and use it in GitHub Desktop.
Save carlok/5660903 to your computer and use it in GitHub Desktop.
PHP5 class written by Carlo Perassi based on the EAN13 functions of SVG barcode for PHP Application written by Trần Ngọc Quân
<?php
/*
PHP5 class written by Carlo Perassi [0]
based on the EAN13 functions of SVG barcode for PHP Application [1]
written by Trần Ngọc Quân [2].
Same license.
[0] http://perassi.org/2013/05/28/php5-svg-ean13-barcode-class/
[1] http://sourceforge.net/projects/phpsvgbarcode/
[2] http://vnwildman.users.sourceforge.net/
*/
class Barcode {
private $_prity;
private $_bartable;
private $_guard;
private $_center;
private $_unit;
private $_bw;
private $_width;
private $_height;
private $_fs;
private $_dx;
private $_x;
private $_yt;
private $_sb;
private $_lb;
public function __construct() {
$this->_prity = array(
array(1, 1, 1, 1, 1, 1),
array(1, 1, 0, 1, 0, 0),
array(1, 1, 0, 0, 1, 0),
array(1, 1, 0, 0, 0, 1),
array(1, 0, 1, 1, 0, 0),
array(1, 0, 0, 1, 1, 0),
array(1, 0, 0, 0, 1, 1),
array(1, 0, 1, 0, 1, 0),
array(1, 0, 1, 0, 0, 1),
array(1, 0, 0, 1, 0, 1)
);
// left has white bar at 1st.
// right has black bar at 1st (event only).
$this->_bartable = array(
array('3211', '1123'),
array('2221', '1222'),
array('2122', '2212'),
array('1411', '1141'),
array('1132', '2311'),
array('1231', '1321'),
array('1114', '4111'),
array('1312', '2131'),
array('1213', '3121'),
array('3112', '2113')
);
$this->_guard = '101';
$this->_center = '01010';
//configure
$this->_unit = 'px';
$this->_bw = 3; // bar width
$this->_width = $this->_bw * 106;
$this->_height = $this->_bw * 50;
$this->_fs = 8 * $this->_bw; // font size
$this->_yta = 45 * $this->_bw;
$this->_ytb = 2.5 * $this->_bw;
$this->_dx = 2 * $this->_bw; // lenght between bar and text
$this->_x = 7 * $this->_bw;
$this->_sb = 35 * $this->_bw;
$this->_lb = 45 * $this->_bw;
}
public function check($str) {
$code = str_split($str);
return 10 - ((3 * ($code[1] + $code[3] + $code[5] + $code[7] + $code[9] + $code[11]) + ($code[0] + $code[2] + $code[4] + $code[6] + $code[8] + $code[10])) % 10);
}
public function draw($num) {
$num = preg_replace('/\D/', '', $num);
$char = $num . $this->check($num);
$first = (int) substr($num, 0, 1);
$oe = $this->_prity[$first]; // old event array for first number
$char = str_split($char);
$img = '';
$img.= '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
$img .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
$img.= "<svg width='" . $this->_width . $this->_unit . "' height='" . $this->_height . $this->_unit . "' version='1.1' xmlns='http://www.w3.org/2000/svg'>" . "\n";
$this->_xt = $this->_x + $this->_dx - 8 * $this->_bw; // start point of text drawing
$img.= "<text x='" . $this->_xt . $this->_unit . "' y='" . $this->_yta . $this->_unit . "' font-family='Arial' font-size='" . $this->_fs . "'>" . $char[0] . '</text>' . "\n";
$img.= "<desc>First guard</desc>\n";
$val = str_split($this->_guard);
foreach ($val as $bar) {
if ((int) $bar === 1) {
$img.= "<rect x='" . $this->_x . $this->_unit . "' y='" . $this->_ytb . $this->_unit . "' width='" . $this->_bw . $this->_unit . "' height='" . $this->_lb . $this->_unit . "' fill='black' stroke-width='0' />" . "\n";
}
$this->_x = $this->_x + $this->_bw;
}
// draw Left Bar.
for ($i = 1; $i < 7; $i++) {
$id = $i - 1; // id for Old-event array
$oev = !$oe[$id]; // old-event value
$val = $this->_bartable[$char[$i]][$oev];
$img.= '<desc>' . htmlspecialchars($char[$i]) . "</desc>\n";
$this->_xt = $this->_x + $this->_dx;
$img.= "<text x='" . $this->_xt . $this->_unit . "' y='" . $this->_yta . $this->_unit . "' font-family='Arial' font-size='" . $this->_fs . "'>" . $char[$i] . "</text>\n";
$val = str_split($val);
for ($j = 0; $j < 4; $j++) {
$num = (int) $val[$j];
$w = $this->_bw * $num;
if ($j % 2) {
$img.= "<rect x='" . $this->_x . $this->_unit . "' y='" . $this->_ytb . $this->_unit . "' width='" . $w . $this->_unit . "' height='" . $this->_sb . $this->_unit . "' fill='black' stroke-width='0' />" . "\n";
}
$this->_x = $this->_x + $w;
}
}
// draw Center Bar.
$val = $this->_center;
$img .= "<desc>Center</desc>\n";
$val = str_split($val);
foreach ($val as $bar) {
if ((int) $bar === 1) {
$img .= "<rect x='" . $this->_x . $this->_unit . "' y='" . $this->_ytb . $this->_unit . "' width='" . $this->_bw . $this->_unit . "' height='" . $this->_lb . $this->_unit . "' fill='black' stroke-width='0' />" . "\n";
}
$this->_x = $this->_x + $this->_bw;
}
// draw Right Bar always in first column.
for ($i = 7; $i < 13; $i++) {
$val = $this->_bartable[$char[$i]][0];
$img .= '<desc>' . htmlspecialchars($char[$i]) . "</desc>\n";
$this->_xt = $this->_x + $this->_dx;
$img .= "<text x='" . $this->_xt . $this->_unit . "' y='" . $this->_yta . $this->_unit . "' font-family='Arial' font-size='" . $this->_fs . "'>" . $char[$i] . "</text>\n";
$val = str_split($val);
for ($j = 0; $j < 4; $j++) {
$num = (int) $val[$j];
$w = $this->_bw * $num;
if (!($j % 2)) {
$img.= "<rect x='" . $this->_x . $this->_unit . "' y='" . $this->_ytb . $this->_unit . "' width='" . $w . $this->_unit . "' height='" . $this->_sb . $this->_unit . "' fill='black' stroke-width='0' />" . "\n";
}
$this->_x = $this->_x + $w;
}
}
// draw End guard Bar.
$val = $this->_guard;
$img .= "<desc>End guard</desc>\n";
$val = str_split($val);
foreach ($val as $bar) {
if ((int) $bar === 1) {
$img.= "<rect x='" . $this->_x . $this->_unit . "' y='" . $this->_ytb . $this->_unit . "' width='" . $this->_bw . $this->_unit . "' height='" . $this->_lb . $this->_unit . "' fill='black' stroke-width='0' />" . "\n";
}
$this->_x = $this->_x + $this->_bw;
}
$img.= '</svg>';
return $img;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment