1<?php 2 3namespace Mpdf\Barcode; 4 5/** 6 * POSTNET and PLANET barcodes. 7 * Used by U.S. Postal Service for automated mail sorting 8 */ 9class Postnet extends \Mpdf\Barcode\AbstractBarcode implements \Mpdf\Barcode\BarcodeInterface 10{ 11 12 /** 13 * @param string $code 14 * @param float $xDim 15 * @param float $gapWidth 16 * @param bool $planet 17 */ 18 public function __construct($code, $xDim, $gapWidth, $planet = false) 19 { 20 $this->init($code, $gapWidth, $planet); 21 22 $this->data['nom-X'] = $xDim; 23 $this->data['nom-H'] = 3.175; // Nominal value for Height of Full bar in mm (spec.) 24 $this->data['quietL'] = 3.175; // LEFT Quiet margin = mm (?spec.) 25 $this->data['quietR'] = 3.175; // RIGHT Quiet margin = mm (?spec.) 26 $this->data['quietTB'] = 1.016; // TOP/BOTTOM Quiet margin = mm (?spec.) 27 } 28 29 /** 30 * @param string $code 31 * @param float $gapWidth 32 * @param bool $planet 33 */ 34 private function init($code, $gapWidth, $planet) 35 { 36 // bar lenght 37 if ($planet) { 38 $barlen = [ 39 0 => [1, 1, 2, 2, 2], 40 1 => [2, 2, 2, 1, 1], 41 2 => [2, 2, 1, 2, 1], 42 3 => [2, 2, 1, 1, 2], 43 4 => [2, 1, 2, 2, 1], 44 5 => [2, 1, 2, 1, 2], 45 6 => [2, 1, 1, 2, 2], 46 7 => [1, 2, 2, 2, 1], 47 8 => [1, 2, 2, 1, 2], 48 9 => [1, 2, 1, 2, 2] 49 ]; 50 } else { 51 $barlen = [ 52 0 => [2, 2, 1, 1, 1], 53 1 => [1, 1, 1, 2, 2], 54 2 => [1, 1, 2, 1, 2], 55 3 => [1, 1, 2, 2, 1], 56 4 => [1, 2, 1, 1, 2], 57 5 => [1, 2, 1, 2, 1], 58 6 => [1, 2, 2, 1, 1], 59 7 => [2, 1, 1, 1, 2], 60 8 => [2, 1, 1, 2, 1], 61 9 => [2, 1, 2, 1, 1] 62 ]; 63 } 64 65 $bararray = ['code' => $code, 'maxw' => 0, 'maxh' => 5, 'bcode' => []]; 66 67 $k = 0; 68 $code = str_replace('-', '', $code); 69 $code = str_replace(' ', '', $code); 70 $len = strlen($code); 71 72 // calculate checksum 73 $sum = 0; 74 for ($i = 0; $i < $len; ++$i) { 75 $sum += (int) $code[$i]; 76 } 77 78 $chkd = ($sum % 10); 79 if ($chkd > 0) { 80 $chkd = (10 - $chkd); 81 } 82 83 $code .= $chkd; 84 $checkdigit = $chkd; 85 $len = strlen($code); 86 87 // start bar 88 $bararray['bcode'][$k++] = ['t' => 1, 'w' => 1, 'h' => 5, 'p' => 0]; 89 $bararray['bcode'][$k++] = ['t' => 0, 'w' => $gapWidth, 'h' => 5, 'p' => 0]; 90 $bararray['maxw'] += (1 + $gapWidth); 91 92 for ($i = 0; $i < $len; ++$i) { 93 for ($j = 0; $j < 5; ++$j) { 94 $bh = $barlen[$code[$i]][$j]; 95 if ($bh == 2) { 96 $h = 5; 97 $p = 0; 98 } else { 99 $h = 2; 100 $p = 3; 101 } 102 $bararray['bcode'][$k++] = ['t' => 1, 'w' => 1, 'h' => $h, 'p' => $p]; 103 $bararray['bcode'][$k++] = ['t' => 0, 'w' => $gapWidth, 'h' => 2, 'p' => 0]; 104 $bararray['maxw'] += (1 + $gapWidth); 105 } 106 } 107 108 // end bar 109 $bararray['bcode'][$k++] = ['t' => 1, 'w' => 1, 'h' => 5, 'p' => 0]; 110 $bararray['maxw'] += 1; 111 $bararray['checkdigit'] = $checkdigit; 112 113 $this->data = $bararray; 114 } 115 116 /** 117 * @inheritdoc 118 */ 119 public function getType() 120 { 121 return 'POSTNET'; 122 } 123 124} 125