Created by SnippLeaf.com
This file contains 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
<?php | |
class EAN13 { | |
private $code = null; | |
private $prefix = false; | |
public function __construct ($prefix = false) { | |
$this->prefix = $prefix === false ? false : (string) $prefix; | |
} | |
public static function create($prefix = false) { | |
$barcode = new static($prefix); | |
return $barcode->generate(); | |
} | |
public function generate() | |
{ | |
// Generate random | |
$this->code = (string) mt_rand(100000000000, 999999999999); // 12 chars long | |
if ($this->prefix) { | |
$this->code = $this->prefix . $this->code; | |
$this->code = substr($this->code, 0, -strlen($this->prefix)); | |
} | |
// Get latest digit | |
$this->code .= $this->getCheckDigit(); | |
return $this->code; | |
} | |
private function getCheckDigit() | |
{ | |
$codePartials = str_split($this->code); | |
$checkdigit = null; | |
$evenNumbers = 0; | |
$oddNumbers = 0; | |
foreach ($codePartials as $key => $value) { | |
if (($key + 1) % 2 == 0) { // Keys start from 0, We want the start to be 1 | |
$evenNumbers += $value; | |
} else { | |
$oddNumbers += $value; | |
} | |
} | |
$evenNumbers = $evenNumbers * 3; | |
$total = $evenNumbers + $oddNumbers; | |
if ($total % 10 == 0) { | |
$checkdigit = 0; | |
} else { | |
$next_multiple = $total + (10 - $total % 10); | |
$checkdigit = $next_multiple - $total; | |
} | |
return $checkdigit; | |
} | |
} | |
if ( !function_exists('create_mfg_code') ) { | |
function create_mfg_code($mfg_name) | |
{ | |
$rand = mt_rand(00000, 99999); | |
$code = initials( $mfg_name ); | |
$gen = $code . $rand; | |
return substr( $gen, 0, -strlen($code) ); | |
} | |
} | |
if ( !function_exists('initials') ) { | |
function initials($str) { | |
$ret = null; | |
foreach (explode(' ', $str) as $word) { | |
$num = to_num(strtolower($word[0])); | |
$ret .= $num; | |
} | |
return $ret; | |
} | |
} | |
if ( !function_exists('to_num') ) { | |
function to_num($data) { | |
$alphabet = array( 'a', 'b', 'c', 'd', 'e', | |
'f', 'g', 'h', 'i', 'j', | |
'k', 'l', 'm', 'n', 'o', | |
'p', 'q', 'r', 's', 't', | |
'u', 'v', 'w', 'x', 'y', | |
'z' | |
); | |
$alpha_flip = array_flip($alphabet); | |
$return_value = -1; | |
$length = strlen($data); | |
for ($i = 0; $i < $length; $i++) { | |
$return_value += | |
($alpha_flip[$data[$i]] + 1) * pow(26, ($length - $i - 1)); | |
} | |
return $return_value; | |
} | |
} | |
// Create Number System | |
$number_system = 17; | |
// Craete Manufactur Code | |
$mfg_code = create_mfg_code( 'Gudang Timur' ); | |
// Craete Product Code | |
$product_code = substr(round(microtime(true)),0,5); | |
// Combine | |
$item_code = $number_system . $mfg_code . $product_code; | |
echo "-------------" . PHP_EOL; | |
echo "Number System: " . $number_system . ' ('.strlen($number_system).')' . PHP_EOL; | |
echo "Manufactur Code: " . $mfg_code . ' ('.strlen($mfg_code).')' . PHP_EOL; | |
echo "Product Code: " . $product_code . ' ('.strlen($product_code).')' . PHP_EOL; | |
echo "Total Length: " . strlen($item_code) . PHP_EOL; | |
echo "-------------" . PHP_EOL; | |
$barcode = EAN13::create($item_code); | |
echo "Barcode: ". $barcode . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment