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