=0; $i-=2) { $ch = substr($s, $i, 1); // s.charAt(i); // if (validChars.indexOf(ch) < 0) { // return "invalid characters in identifier"; // } $digit = ord(substr($s, $i, 1)) - 48; // s.charCodeAt(i)-48; $sum += (2 * $digit) - floor($digit / 5) * 9; if ($i>0) { $digit = ord(substr($s, $i-1, 1))-48; $sum += $digit; // s.charCodeAt(i-1)-48; sum += digit; } } $sum = abs($sum); $checkdigit = (10 - ($sum % 10)) % 10; return(sprintf("%d", $checkdigit)); } // Uncomment for example //$input=array('600168', '234109', '740323'); //foreach($input as $x) { // printf("%s:%s\n", $x, luhn($x)); //} // Pattern of black pixels(1) and spaces(0) for each digit in MSI Plessey // See https://en.wikipedia.org/wiki/MSI_Barcode // and https://en.wikipedia.org/wiki/Plessey_Code $codes=array( "s" => "110", /* Start Code */ "e" => "1001", /* End Code */ "0" => "100100100100", "1" => "100100100110", "2" => "100100110100", "3" => "100100110110", "4" => "100110100100", "5" => "100110100110", "6" => "100110110100", "7" => "100110110110", "8" => "110100100100", "9" => "110100100110", ); // Generates a Plessey Code barcode from a string of digits 0-9. // Also accepted are the special values 's' for start sequence and // 'e' for end sequence. // // Start and end codes are affixed automatically, as well as a // base-10 check digit. Edit the "$c = ..." line if you require // different behavior. function plessey($c) { global $codes; $c = 's' . $c . luhn($c) . 'e'; $bar = 2; // Width of narrow bars in pixels $h = 40; // Height of image $y = 10; // Top/bottom margins $w = 0; // Width, to be calculated $x = 0; // X drawing position for($m=0; $m < strlen($c); $m++) $w += $bar*(strlen($codes[$c[$m]])); // Side margins $w += 40; $x += 40 / 2; $im=@imagecreate($w, $h) or die("Cannot Initialize new GD image stream"); $white = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, $w, 50, $white); for($m=0; $m < strlen($c); $m++) for($n=0; $n < strlen($codes[$c[$m]]); $n++, $x+=2) if($codes[$c[$m]][$n] == '1') imagefilledrectangle($im, $x, $y, $x+1, $h-$y, $black); return($im); } if(!array_key_exists("q",$_GET) || preg_match("/[^0-9]/", $_GET['q'])) { header("Content-Type: text/html"); echo("Unknown or invalid query, use q=0123456789"); exit(0); } header("Content-Type: image/png"); imagepng(plessey($_GET['q'])); exit(0); ?>