Barcode39.php
234 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CpChart\Barcode; |
| 4 | |
| 5 | use CpChart\Image; |
| 6 | use Exception; |
| 7 | /** |
| 8 | * pBarcode39 - class to create barcodes (39B) |
| 9 | * |
| 10 | * Version : 2.1.4 |
| 11 | * Made by : Jean-Damien POGOLOTTI |
| 12 | * Last Update : 19/01/2014 |
| 13 | * |
| 14 | * This file can be distributed under the license you can find at : |
| 15 | * |
| 16 | * http://www.pchart.net/license |
| 17 | * |
| 18 | * You can find the whole class documentation on the pChart web site. |
| 19 | */ |
| 20 | class Barcode39 |
| 21 | { |
| 22 | /** |
| 23 | * @var array |
| 24 | */ |
| 25 | public $Codes; |
| 26 | /** |
| 27 | * @var array |
| 28 | */ |
| 29 | public $Reverse; |
| 30 | /** |
| 31 | * @var string |
| 32 | */ |
| 33 | public $Result; |
| 34 | /** |
| 35 | * @var Image |
| 36 | */ |
| 37 | public $pChartObject; |
| 38 | /** |
| 39 | * @var integer |
| 40 | */ |
| 41 | public $CRC; |
| 42 | /** |
| 43 | * @var boolean |
| 44 | */ |
| 45 | public $MOD43; |
| 46 | /** |
| 47 | * @param string $filePath |
| 48 | * @param boolean $EnableMOD43 |
| 49 | * @throws Exception |
| 50 | */ |
| 51 | public function __construct($filePath = "", $EnableMOD43 = \false) |
| 52 | { |
| 53 | $this->MOD43 = (bool) $EnableMOD43; |
| 54 | $this->Codes = []; |
| 55 | $this->Reverse = []; |
| 56 | if (!file_exists($filePath)) { |
| 57 | $filePath = sprintf('%s/../../resources/barcode/39.db', __DIR__); |
| 58 | } |
| 59 | $FileHandle = @fopen($filePath, "r"); |
| 60 | if (!$FileHandle) { |
| 61 | throw new Exception("Cannot find barcode database (" . $filePath . ")."); |
| 62 | } |
| 63 | while (!feof($FileHandle)) { |
| 64 | $Buffer = fgets($FileHandle, 4096); |
| 65 | $Buffer = str_replace(chr(10), "", $Buffer); |
| 66 | $Buffer = str_replace(chr(13), "", $Buffer); |
| 67 | $Values = preg_split("/;/", $Buffer); |
| 68 | $this->Codes[$Values[0]] = $Values[1]; |
| 69 | } |
| 70 | fclose($FileHandle); |
| 71 | } |
| 72 | /** |
| 73 | * Return the projected size of a barcode |
| 74 | * |
| 75 | * @param string $TextString |
| 76 | * @param string $Format |
| 77 | * @return array |
| 78 | */ |
| 79 | public function getSize($TextString, $Format = "") |
| 80 | { |
| 81 | $Angle = isset($Format["Angle"]) ? $Format["Angle"] : 0; |
| 82 | $ShowLegend = isset($Format["ShowLegend"]) ? $Format["ShowLegend"] : \false; |
| 83 | $LegendOffset = isset($Format["LegendOffset"]) ? $Format["LegendOffset"] : 5; |
| 84 | $DrawArea = isset($Format["DrawArea"]) ? $Format["DrawArea"] : \false; |
| 85 | $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : 12; |
| 86 | $Height = isset($Format["Height"]) ? $Format["Height"] : 30; |
| 87 | $TextString = $this->encode39($TextString); |
| 88 | $BarcodeLength = strlen($this->Result); |
| 89 | $WOffset = $DrawArea ? 20 : 0; |
| 90 | $HOffset = $ShowLegend ? $FontSize + $LegendOffset + $WOffset : 0; |
| 91 | $X1 = cos($Angle * PI / 180) * ($WOffset + $BarcodeLength); |
| 92 | $Y1 = sin($Angle * PI / 180) * ($WOffset + $BarcodeLength); |
| 93 | $X2 = $X1 + cos(($Angle + 90) * PI / 180) * ($HOffset + $Height); |
| 94 | $Y2 = $Y1 + sin(($Angle + 90) * PI / 180) * ($HOffset + $Height); |
| 95 | $AreaWidth = max(abs($X1), abs($X2)); |
| 96 | $AreaHeight = max(abs($Y1), abs($Y2)); |
| 97 | return ["Width" => $AreaWidth, "Height" => $AreaHeight]; |
| 98 | } |
| 99 | /** |
| 100 | * Create the encoded string |
| 101 | * |
| 102 | * @param string $Value |
| 103 | * @return string |
| 104 | */ |
| 105 | public function encode39($Value) |
| 106 | { |
| 107 | $this->Result = "100101101101" . "0"; |
| 108 | $TextString = ""; |
| 109 | for ($i = 1; $i <= strlen($Value); $i++) { |
| 110 | $CharCode = ord($this->mid($Value, $i, 1)); |
| 111 | if ($CharCode >= 97 && $CharCode <= 122) { |
| 112 | $CharCode = $CharCode - 32; |
| 113 | } |
| 114 | if (isset($this->Codes[chr($CharCode)])) { |
| 115 | $this->Result = $this->Result . $this->Codes[chr($CharCode)] . "0"; |
| 116 | $TextString = $TextString . chr($CharCode); |
| 117 | } |
| 118 | } |
| 119 | if ($this->MOD43) { |
| 120 | $Checksum = $this->checksum($TextString); |
| 121 | $this->Result = $this->Result . $this->Codes[$Checksum] . "0"; |
| 122 | } |
| 123 | $this->Result = $this->Result . "100101101101"; |
| 124 | $TextString = "*" . $TextString . "*"; |
| 125 | return $TextString; |
| 126 | } |
| 127 | /** |
| 128 | * Create the encoded string |
| 129 | * @param Image $Object |
| 130 | * @param type $Value |
| 131 | * @param type $X |
| 132 | * @param type $Y |
| 133 | * @param array $Format |
| 134 | */ |
| 135 | public function draw(Image $Object, $Value, $X, $Y, $Format = []) |
| 136 | { |
| 137 | $this->pChartObject = $Object; |
| 138 | $R = isset($Format["R"]) ? $Format["R"] : 0; |
| 139 | $G = isset($Format["G"]) ? $Format["G"] : 0; |
| 140 | $B = isset($Format["B"]) ? $Format["B"] : 0; |
| 141 | $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; |
| 142 | $Height = isset($Format["Height"]) ? $Format["Height"] : 30; |
| 143 | $Angle = isset($Format["Angle"]) ? $Format["Angle"] : 0; |
| 144 | $ShowLegend = isset($Format["ShowLegend"]) ? $Format["ShowLegend"] : \false; |
| 145 | $LegendOffset = isset($Format["LegendOffset"]) ? $Format["LegendOffset"] : 5; |
| 146 | $DrawArea = isset($Format["DrawArea"]) ? $Format["DrawArea"] : \false; |
| 147 | $AreaR = isset($Format["AreaR"]) ? $Format["AreaR"] : 255; |
| 148 | $AreaG = isset($Format["AreaG"]) ? $Format["AreaG"] : 255; |
| 149 | $AreaB = isset($Format["AreaB"]) ? $Format["AreaB"] : 255; |
| 150 | $AreaBorderR = isset($Format["AreaBorderR"]) ? $Format["AreaBorderR"] : $AreaR; |
| 151 | $AreaBorderG = isset($Format["AreaBorderG"]) ? $Format["AreaBorderG"] : $AreaG; |
| 152 | $AreaBorderB = isset($Format["AreaBorderB"]) ? $Format["AreaBorderB"] : $AreaB; |
| 153 | $TextString = $this->encode39($Value); |
| 154 | if ($DrawArea) { |
| 155 | $X1 = $X + cos(($Angle - 135) * PI / 180) * 10; |
| 156 | $Y1 = $Y + sin(($Angle - 135) * PI / 180) * 10; |
| 157 | $X2 = $X1 + cos($Angle * PI / 180) * (strlen($this->Result) + 20); |
| 158 | $Y2 = $Y1 + sin($Angle * PI / 180) * (strlen($this->Result) + 20); |
| 159 | if ($ShowLegend) { |
| 160 | $X3 = $X2 + cos(($Angle + 90) * PI / 180) * ($Height + $LegendOffset + $this->pChartObject->FontSize + 10); |
| 161 | $Y3 = $Y2 + sin(($Angle + 90) * PI / 180) * ($Height + $LegendOffset + $this->pChartObject->FontSize + 10); |
| 162 | } else { |
| 163 | $X3 = $X2 + cos(($Angle + 90) * PI / 180) * ($Height + 20); |
| 164 | $Y3 = $Y2 + sin(($Angle + 90) * PI / 180) * ($Height + 20); |
| 165 | } |
| 166 | $X4 = $X3 + cos(($Angle + 180) * PI / 180) * (strlen($this->Result) + 20); |
| 167 | $Y4 = $Y3 + sin(($Angle + 180) * PI / 180) * (strlen($this->Result) + 20); |
| 168 | $Polygon = [$X1, $Y1, $X2, $Y2, $X3, $Y3, $X4, $Y4]; |
| 169 | $Settings = ["R" => $AreaR, "G" => $AreaG, "B" => $AreaB, "BorderR" => $AreaBorderR, "BorderG" => $AreaBorderG, "BorderB" => $AreaBorderB]; |
| 170 | $this->pChartObject->drawPolygon($Polygon, $Settings); |
| 171 | } |
| 172 | for ($i = 1; $i <= strlen($this->Result); $i++) { |
| 173 | if ($this->mid($this->Result, $i, 1) == 1) { |
| 174 | $X1 = $X + cos($Angle * PI / 180) * $i; |
| 175 | $Y1 = $Y + sin($Angle * PI / 180) * $i; |
| 176 | $X2 = $X1 + cos(($Angle + 90) * PI / 180) * $Height; |
| 177 | $Y2 = $Y1 + sin(($Angle + 90) * PI / 180) * $Height; |
| 178 | $Settings = ["R" => $R, "G" => $G, "B" => $B, "Alpha" => $Alpha]; |
| 179 | $this->pChartObject->drawLine($X1, $Y1, $X2, $Y2, $Settings); |
| 180 | } |
| 181 | } |
| 182 | if ($ShowLegend) { |
| 183 | $X1 = $X + cos($Angle * PI / 180) * (strlen($this->Result) / 2); |
| 184 | $Y1 = $Y + sin($Angle * PI / 180) * (strlen($this->Result) / 2); |
| 185 | $LegendX = $X1 + cos(($Angle + 90) * PI / 180) * ($Height + $LegendOffset); |
| 186 | $LegendY = $Y1 + sin(($Angle + 90) * PI / 180) * ($Height + $LegendOffset); |
| 187 | $Settings = ["R" => $R, "G" => $G, "B" => $B, "Alpha" => $Alpha, "Angle" => -$Angle, "Align" => TEXT_ALIGN_TOPMIDDLE]; |
| 188 | $this->pChartObject->drawText($LegendX, $LegendY, $TextString, $Settings); |
| 189 | } |
| 190 | } |
| 191 | /** |
| 192 | * @param string $string |
| 193 | * @return string |
| 194 | */ |
| 195 | public function checksum($string) |
| 196 | { |
| 197 | $checksum = 0; |
| 198 | $length = strlen($string); |
| 199 | $charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%'; |
| 200 | for ($i = 0; $i < $length; ++$i) { |
| 201 | $checksum += strpos($charset, $string[$i]); |
| 202 | } |
| 203 | return substr($charset, $checksum % 43, 1); |
| 204 | } |
| 205 | /** |
| 206 | * @param string $value |
| 207 | * @param int $NbChar |
| 208 | * @return string |
| 209 | */ |
| 210 | public function left($value, $NbChar) |
| 211 | { |
| 212 | return substr($value, 0, $NbChar); |
| 213 | } |
| 214 | /** |
| 215 | * @param string $value |
| 216 | * @param int $NbChar |
| 217 | * @return string|false |
| 218 | */ |
| 219 | public function right($value, $NbChar) |
| 220 | { |
| 221 | return substr($value, strlen($value) - $NbChar, $NbChar); |
| 222 | } |
| 223 | /** |
| 224 | * @param string $value |
| 225 | * @param int $Depart |
| 226 | * @param int $NbChar |
| 227 | * @return string |
| 228 | */ |
| 229 | public function mid($value, $Depart, $NbChar) |
| 230 | { |
| 231 | return substr($value, $Depart - 1, $NbChar); |
| 232 | } |
| 233 | } |
| 234 |