public static function compute($code) { if (strlen($code) < 12) { $code = '0' . $code; } return substr(BarcodeEAN::compute($code, 'ean13'), 1); }
private static function _draw($call, $res, $color, $x, $y, $angle, $type, $datas, $width, $height) { $digit = ''; $hri = ''; $code = ''; $crc = true; $rect = false; $b2d = false; if (is_array($datas)) { foreach (array('code' => '', 'crc' => true, 'rect' => false) as $v => $def) { ${$v} = isset($datas[$v]) ? $datas[$v] : $def; } $code = $code; } else { $code = $datas; } if ($code == '') { return false; } $code = (string) $code; $type = strtolower($type); switch ($type) { case 'std25': case 'int25': $digit = BarcodeI25::getDigit($code, $crc, $type); $hri = BarcodeI25::compute($code, $crc, $type); break; case 'ean8': case 'ean13': $digit = BarcodeEAN::getDigit($code, $type); $hri = BarcodeEAN::compute($code, $type); break; case 'upc': $digit = BarcodeUPC::getDigit($code); $hri = BarcodeUPC::compute($code); break; case 'code11': $digit = Barcode11::getDigit($code); $hri = $code; break; case 'code39': $digit = Barcode39::getDigit($code); $hri = $code; break; case 'code93': $digit = Barcode93::getDigit($code, $crc); $hri = $code; break; case 'code128': $digit = Barcode128::getDigit($code); $hri = $code; break; case 'codabar': $digit = BarcodeCodabar::getDigit($code); $hri = $code; break; case 'msi': $digit = BarcodeMSI::getDigit($code, $crc); $hri = BarcodeMSI::compute($code, $crc); break; case 'datamatrix': $digit = BarcodeDatamatrix::getDigit($code, $rect); $hri = $code; $b2d = true; break; } if ($digit == '') { return false; } if ($b2d) { $width = is_null($width) ? 5 : $width; $height = $width; } else { $width = is_null($width) ? 1 : $width; $height = is_null($height) ? 50 : $height; $digit = self::bitStringTo2DArray($digit); } if ($call == 'gd') { $result = self::digitToGDRenderer($res, $color, $x, $y, $angle, $width, $height, $digit); } else { if ($call == 'fpdf') { $result = self::digitToFPDFRenderer($res, $color, $x, $y, $angle, $width, $height, $digit); } } $result['hri'] = $hri; return $result; }
function generate_barcode($first, $code, $terminal) { if ($first) { $len = strlen($code); if ($len > 9) { $code = substr($code, $len - 9); $len = 9; } for ($i = 0; $i < 9 - $len; $i++) { $code = '0' . $code; } $code = '1' . $terminal . $code; } // -------------------------------------------------- // // PROPERTIES // -------------------------------------------------- // // download a ttf font here for example : http://www.dafont.com/fr/nottke.font //$font = './NOTTB___.TTF'; // - - $folio = BarcodeEAN::compute($code, 'ean13'); $fontSize = 10; // GD1 in px ; GD2 in point $marge = 10; // between barcode and hri in pixel $x = 110; // barcode center $y = 40; // barcode center $height = 50; // barcode height in 1D ; module size in 2D $width = 2; // barcode height in 1D ; not use in 2D $angle = 0; // rotation in degrees : nb : non horizontable barcode might not be usable because of pixelisation //$code = '750000000001'; // barcode, of course ;) $type = 'ean13'; // -------------------------------------------------- // // USEFUL // -------------------------------------------------- // /*function drawCross($im, $color, $x, $y){ imageline($im, $x - 10, $y, $x + 10, $y, $color); imageline($im, $x, $y- 10, $x, $y + 10, $color); }*/ // -------------------------------------------------- // // ALLOCATE GD RESSOURCE // -------------------------------------------------- // $im = imagecreatetruecolor(220, 80); $black = ImageColorAllocate($im, 0x0, 0x0, 0x0); $white = ImageColorAllocate($im, 0xff, 0xff, 0xff); $red = ImageColorAllocate($im, 0xff, 0x0, 0x0); $blue = ImageColorAllocate($im, 0x0, 0x0, 0xff); imagefilledrectangle($im, 0, 0, 220, 80, $white); // -------------------------------------------------- // // BARCODE // -------------------------------------------------- // $data = Barcode::gd($im, $black, $x, $y, $angle, $type, array('code' => $code), $width, $height); // -------------------------------------------------- // // HRI // -------------------------------------------------- // if (isset($font)) { $box = imagettfbbox($fontSize, 0, $font, $data['hri']); $len = $box[2] - $box[0]; Barcode::rotate(-$len / 2, $data['height'] / 2 + $fontSize + $marge, $angle, $xt, $yt); imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $blue, $font, $data['hri']); } // -------------------------------------------------- // // ROTATE // -------------------------------------------------- // // Beware ! the rotate function should be use only with right angle // Remove the comment below to see a non right rotation /** / $rot = imagerotate($im, 45, $white); imagedestroy($im); $im = imagecreatetruecolor(900, 300); $black = ImageColorAllocate($im,0x00,0x00,0x00); $white = ImageColorAllocate($im,0xff,0xff,0xff); $red = ImageColorAllocate($im,0xff,0x00,0x00); $blue = ImageColorAllocate($im,0x00,0x00,0xff); imagefilledrectangle($im, 0, 0, 900, 300, $white); // Barcode rotation : 90� $angle = 90; $data = Barcode::gd($im, $black, $x, $y, $angle, $type, array('code'=>$code), $width, $height); Barcode::rotate(-$len / 2, ($data['height'] / 2) + $fontSize + $marge, $angle, $xt, $yt); imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $blue, $font, $data['hri']); imagettftext($im, 10, 0, 60, 290, $black, $font, 'BARCODE ROTATION : 90�'); // barcode rotation : 135 $angle = 135; Barcode::gd($im, $black, $x+300, $y, $angle, $type, array('code'=>$code), $width, $height); Barcode::rotate(-$len / 2, ($data['height'] / 2) + $fontSize + $marge, $angle, $xt, $yt); imagettftext($im, $fontSize, $angle, $x + 300 + $xt, $y + $yt, $blue, $font, $data['hri']); imagettftext($im, 10, 0, 360, 290, $black, $font, 'BARCODE ROTATION : 135�'); // last one : image rotation imagecopy($im, $rot, 580, -50, 0, 0, 300, 300); imagerectangle($im, 0, 0, 299, 299, $black); imagerectangle($im, 299, 0, 599, 299, $black); imagerectangle($im, 599, 0, 899, 299, $black); imagettftext($im, 10, 0, 690, 290, $black, $font, 'IMAGE ROTATION'); /**/ // -------------------------------------------------- // // MIDDLE AXE // -------------------------------------------------- // /*imageline($im, $x, 0, $x, 250, $red); imageline($im, 0, $y, 250, $y, $red);*/ // -------------------------------------------------- // // BARCODE BOUNDARIES // -------------------------------------------------- // /*for($i=1; $i<5; $i++){ drawCross($im, $blue, $data['p'.$i]['x'], $data['p'.$i]['y']); }*/ // -------------------------------------------------- // // GENERATE // -------------------------------------------------- // header('Content-type: image/png'); imagepng($im, $folio . ".png"); imagedestroy($im); return $folio; }