Barcode
1 year ago
Chart
1 year ago
BaseDraw.php
1 year ago
Cache.php
1 year ago
Data.php
1 year ago
Draw.php
1 year ago
Image.php
1 year ago
Image.php
585 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CpChart; |
| 4 | |
| 5 | /** |
| 6 | * Image - The actual class to do most of the drawing. Extends the Draw class |
| 7 | * with the bulk of drawing methods. |
| 8 | * |
| 9 | * Version : 2.1.4 |
| 10 | * Made by : Jean-Damien POGOLOTTI |
| 11 | * Last Update : 19/01/2014 |
| 12 | * |
| 13 | * This file can be distributed under the license you can find at : |
| 14 | * |
| 15 | * http://www.pchart.net/license |
| 16 | * |
| 17 | * You can find the whole class documentation on the pChart web site. |
| 18 | */ |
| 19 | class Image extends \CpChart\Draw |
| 20 | { |
| 21 | /** |
| 22 | * @param int $XSize |
| 23 | * @param int $YSize |
| 24 | * @param Data $DataSet |
| 25 | * @param boolean $TransparentBackground |
| 26 | */ |
| 27 | public function __construct($XSize, $YSize, \CpChart\Data $DataSet = null, $TransparentBackground = \false) |
| 28 | { |
| 29 | parent::__construct(); |
| 30 | $this->TransparentBackground = $TransparentBackground; |
| 31 | $this->DataSet = null !== $DataSet ? $DataSet : new \CpChart\Data(); |
| 32 | $this->XSize = $XSize; |
| 33 | $this->YSize = $YSize; |
| 34 | $this->Picture = imagecreatetruecolor($XSize, $YSize); |
| 35 | if ($this->TransparentBackground) { |
| 36 | imagealphablending($this->Picture, \false); |
| 37 | imagefilledrectangle($this->Picture, 0, 0, $XSize, $YSize, imagecolorallocatealpha($this->Picture, 255, 255, 255, 127)); |
| 38 | imagealphablending($this->Picture, \true); |
| 39 | imagesavealpha($this->Picture, \true); |
| 40 | } else { |
| 41 | $C_White = $this->AllocateColor($this->Picture, 255, 255, 255); |
| 42 | imagefilledrectangle($this->Picture, 0, 0, $XSize, $YSize, $C_White); |
| 43 | } |
| 44 | } |
| 45 | /** |
| 46 | * Enable / Disable and set shadow properties |
| 47 | * @param boolean $Enabled |
| 48 | * @param array $Format |
| 49 | */ |
| 50 | public function setShadow($Enabled = \true, array $Format = []) |
| 51 | { |
| 52 | $X = isset($Format["X"]) ? $Format["X"] : 2; |
| 53 | $Y = isset($Format["Y"]) ? $Format["Y"] : 2; |
| 54 | $R = isset($Format["R"]) ? $Format["R"] : 0; |
| 55 | $G = isset($Format["G"]) ? $Format["G"] : 0; |
| 56 | $B = isset($Format["B"]) ? $Format["B"] : 0; |
| 57 | $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 10; |
| 58 | $this->Shadow = $Enabled; |
| 59 | $this->ShadowX = $X; |
| 60 | $this->ShadowY = $Y; |
| 61 | $this->ShadowR = $R; |
| 62 | $this->ShadowG = $G; |
| 63 | $this->ShadowB = $B; |
| 64 | $this->Shadowa = $Alpha; |
| 65 | } |
| 66 | /** |
| 67 | * Set the graph area position |
| 68 | * @param int $X1 |
| 69 | * @param int $Y1 |
| 70 | * @param int $X2 |
| 71 | * @param int $Y2 |
| 72 | * @return int|null |
| 73 | */ |
| 74 | public function setGraphArea($X1, $Y1, $X2, $Y2) |
| 75 | { |
| 76 | if ($X2 < $X1 || $X1 == $X2 || $Y2 < $Y1 || $Y1 == $Y2) { |
| 77 | return -1; |
| 78 | } |
| 79 | $this->GraphAreaX1 = $X1; |
| 80 | $this->DataSet->Data["GraphArea"]["X1"] = $X1; |
| 81 | $this->GraphAreaY1 = $Y1; |
| 82 | $this->DataSet->Data["GraphArea"]["Y1"] = $Y1; |
| 83 | $this->GraphAreaX2 = $X2; |
| 84 | $this->DataSet->Data["GraphArea"]["X2"] = $X2; |
| 85 | $this->GraphAreaY2 = $Y2; |
| 86 | $this->DataSet->Data["GraphArea"]["Y2"] = $Y2; |
| 87 | } |
| 88 | /** |
| 89 | * Return the width of the picture |
| 90 | * @return int |
| 91 | */ |
| 92 | public function getWidth() |
| 93 | { |
| 94 | return $this->XSize; |
| 95 | } |
| 96 | /** |
| 97 | * Return the heigth of the picture |
| 98 | * @return int |
| 99 | */ |
| 100 | public function getHeight() |
| 101 | { |
| 102 | return $this->YSize; |
| 103 | } |
| 104 | /** |
| 105 | * Render the picture to a file |
| 106 | * @param string $FileName |
| 107 | */ |
| 108 | public function render($FileName) |
| 109 | { |
| 110 | if ($this->TransparentBackground) { |
| 111 | imagealphablending($this->Picture, \false); |
| 112 | imagesavealpha($this->Picture, \true); |
| 113 | } |
| 114 | imagepng($this->Picture, $FileName); |
| 115 | } |
| 116 | public function __toString() |
| 117 | { |
| 118 | if ($this->TransparentBackground) { |
| 119 | imagealphablending($this->Picture, \false); |
| 120 | imagesavealpha($this->Picture, \true); |
| 121 | } |
| 122 | ob_start(); |
| 123 | imagepng($this->Picture); |
| 124 | return ob_get_clean(); |
| 125 | } |
| 126 | public function toDataURI() |
| 127 | { |
| 128 | return 'data:image/png;base64,' . base64_encode($this->__toString()); |
| 129 | } |
| 130 | /** |
| 131 | * Render the picture to a web browser stream |
| 132 | * @param boolean $BrowserExpire |
| 133 | */ |
| 134 | public function stroke($BrowserExpire = \false) |
| 135 | { |
| 136 | if ($this->TransparentBackground) { |
| 137 | imagealphablending($this->Picture, \false); |
| 138 | imagesavealpha($this->Picture, \true); |
| 139 | } |
| 140 | if ($BrowserExpire) { |
| 141 | header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
| 142 | header("Cache-Control: no-cache"); |
| 143 | header("Pragma: no-cache"); |
| 144 | } |
| 145 | header('Content-type: image/png'); |
| 146 | imagepng($this->Picture); |
| 147 | } |
| 148 | /** |
| 149 | * Automatic output method based on the calling interface |
| 150 | * @param string $FileName |
| 151 | */ |
| 152 | public function autoOutput($FileName = "output.png") |
| 153 | { |
| 154 | if (php_sapi_name() == "cli") { |
| 155 | $this->Render($FileName); |
| 156 | } else { |
| 157 | $this->Stroke(); |
| 158 | } |
| 159 | } |
| 160 | /** |
| 161 | * Return the length between two points |
| 162 | * @param int $X1 |
| 163 | * @param int $Y1 |
| 164 | * @param int $X2 |
| 165 | * @param int $Y2 |
| 166 | * @return float |
| 167 | */ |
| 168 | public function getLength($X1, $Y1, $X2, $Y2) |
| 169 | { |
| 170 | return sqrt(pow(max($X1, $X2) - min($X1, $X2), 2) + pow(max($Y1, $Y2) - min($Y1, $Y2), 2)); |
| 171 | } |
| 172 | /** |
| 173 | * Return the orientation of a line |
| 174 | * @param int $X1 |
| 175 | * @param int $Y1 |
| 176 | * @param int $X2 |
| 177 | * @param int $Y2 |
| 178 | * @return int |
| 179 | */ |
| 180 | public function getAngle($X1, $Y1, $X2, $Y2) |
| 181 | { |
| 182 | $Opposite = $Y2 - $Y1; |
| 183 | $Adjacent = $X2 - $X1; |
| 184 | $Angle = rad2deg(atan2($Opposite, $Adjacent)); |
| 185 | if ($Angle > 0) { |
| 186 | return $Angle; |
| 187 | } else { |
| 188 | return 360 - abs($Angle); |
| 189 | } |
| 190 | } |
| 191 | /** |
| 192 | * Return the surrounding box of text area |
| 193 | * @param int $X |
| 194 | * @param int $Y |
| 195 | * @param string $FontName |
| 196 | * @param int $FontSize |
| 197 | * @param int $Angle |
| 198 | * @param int $Text |
| 199 | * @return array |
| 200 | */ |
| 201 | public function getTextBox($X, $Y, $FontName, $FontSize, $Angle, $Text) |
| 202 | { |
| 203 | $coords = imagettfbbox($FontSize, 0, $this->loadFont($FontName, 'fonts'), $Text); |
| 204 | $a = deg2rad($Angle); |
| 205 | $ca = cos($a); |
| 206 | $sa = sin($a); |
| 207 | $RealPos = []; |
| 208 | for ($i = 0; $i < 7; $i += 2) { |
| 209 | $RealPos[$i / 2]["X"] = $X + round($coords[$i] * $ca + $coords[$i + 1] * $sa); |
| 210 | $RealPos[$i / 2]["Y"] = $Y + round($coords[$i + 1] * $ca - $coords[$i] * $sa); |
| 211 | } |
| 212 | $RealPos[TEXT_ALIGN_BOTTOMLEFT]["X"] = $RealPos[0]["X"]; |
| 213 | $RealPos[TEXT_ALIGN_BOTTOMLEFT]["Y"] = $RealPos[0]["Y"]; |
| 214 | $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["X"] = $RealPos[1]["X"]; |
| 215 | $RealPos[TEXT_ALIGN_BOTTOMRIGHT]["Y"] = $RealPos[1]["Y"]; |
| 216 | $RealPos[TEXT_ALIGN_TOPLEFT]["X"] = $RealPos[3]["X"]; |
| 217 | $RealPos[TEXT_ALIGN_TOPLEFT]["Y"] = $RealPos[3]["Y"]; |
| 218 | $RealPos[TEXT_ALIGN_TOPRIGHT]["X"] = $RealPos[2]["X"]; |
| 219 | $RealPos[TEXT_ALIGN_TOPRIGHT]["Y"] = $RealPos[2]["Y"]; |
| 220 | $RealPos[TEXT_ALIGN_BOTTOMMIDDLE]["X"] = ($RealPos[1]["X"] - $RealPos[0]["X"]) / 2 + $RealPos[0]["X"]; |
| 221 | $RealPos[TEXT_ALIGN_BOTTOMMIDDLE]["Y"] = ($RealPos[0]["Y"] - $RealPos[1]["Y"]) / 2 + $RealPos[1]["Y"]; |
| 222 | $RealPos[TEXT_ALIGN_TOPMIDDLE]["X"] = ($RealPos[2]["X"] - $RealPos[3]["X"]) / 2 + $RealPos[3]["X"]; |
| 223 | $RealPos[TEXT_ALIGN_TOPMIDDLE]["Y"] = ($RealPos[3]["Y"] - $RealPos[2]["Y"]) / 2 + $RealPos[2]["Y"]; |
| 224 | $RealPos[TEXT_ALIGN_MIDDLELEFT]["X"] = ($RealPos[0]["X"] - $RealPos[3]["X"]) / 2 + $RealPos[3]["X"]; |
| 225 | $RealPos[TEXT_ALIGN_MIDDLELEFT]["Y"] = ($RealPos[0]["Y"] - $RealPos[3]["Y"]) / 2 + $RealPos[3]["Y"]; |
| 226 | $RealPos[TEXT_ALIGN_MIDDLERIGHT]["X"] = ($RealPos[1]["X"] - $RealPos[2]["X"]) / 2 + $RealPos[2]["X"]; |
| 227 | $RealPos[TEXT_ALIGN_MIDDLERIGHT]["Y"] = ($RealPos[1]["Y"] - $RealPos[2]["Y"]) / 2 + $RealPos[2]["Y"]; |
| 228 | $RealPos[TEXT_ALIGN_MIDDLEMIDDLE]["X"] = ($RealPos[1]["X"] - $RealPos[3]["X"]) / 2 + $RealPos[3]["X"]; |
| 229 | $RealPos[TEXT_ALIGN_MIDDLEMIDDLE]["Y"] = ($RealPos[0]["Y"] - $RealPos[2]["Y"]) / 2 + $RealPos[2]["Y"]; |
| 230 | return $RealPos; |
| 231 | } |
| 232 | /** |
| 233 | * Set current font properties |
| 234 | * @param array $Format |
| 235 | */ |
| 236 | public function setFontProperties($Format = []) |
| 237 | { |
| 238 | $R = isset($Format["R"]) ? $Format["R"] : -1; |
| 239 | $G = isset($Format["G"]) ? $Format["G"] : -1; |
| 240 | $B = isset($Format["B"]) ? $Format["B"] : -1; |
| 241 | $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100; |
| 242 | $FontName = isset($Format["FontName"]) ? $Format["FontName"] : null; |
| 243 | $FontSize = isset($Format["FontSize"]) ? $Format["FontSize"] : null; |
| 244 | if ($R != -1) { |
| 245 | $this->FontColorR = $R; |
| 246 | } |
| 247 | if ($G != -1) { |
| 248 | $this->FontColorG = $G; |
| 249 | } |
| 250 | if ($B != -1) { |
| 251 | $this->FontColorB = $B; |
| 252 | } |
| 253 | if ($Alpha != null) { |
| 254 | $this->FontColorA = $Alpha; |
| 255 | } |
| 256 | if ($FontName != null) { |
| 257 | $this->FontName = $this->loadFont($FontName, 'fonts'); |
| 258 | } |
| 259 | if ($FontSize != null) { |
| 260 | $this->FontSize = $FontSize; |
| 261 | } |
| 262 | } |
| 263 | /** |
| 264 | * Returns the 1st decimal values (used to correct AA bugs) |
| 265 | * @param mixed $Value |
| 266 | * @return mixed |
| 267 | */ |
| 268 | public function getFirstDecimal($Value) |
| 269 | { |
| 270 | $Values = preg_split("/\\./", $Value); |
| 271 | if (isset($Values[1])) { |
| 272 | return substr($Values[1], 0, 1); |
| 273 | } else { |
| 274 | return 0; |
| 275 | } |
| 276 | } |
| 277 | /** |
| 278 | * Attach a dataset to your pChart Object |
| 279 | * @param Data $DataSet |
| 280 | */ |
| 281 | public function setDataSet(\CpChart\Data $DataSet) |
| 282 | { |
| 283 | $this->DataSet = $DataSet; |
| 284 | } |
| 285 | /** |
| 286 | * Print attached dataset contents to STDOUT |
| 287 | */ |
| 288 | public function printDataSet() |
| 289 | { |
| 290 | print_r($this->DataSet); |
| 291 | } |
| 292 | /** |
| 293 | * Initialise the image map methods |
| 294 | * @param string $Name |
| 295 | * @param int $StorageMode |
| 296 | * @param string $UniqueID |
| 297 | * @param string $StorageFolder |
| 298 | */ |
| 299 | public function initialiseImageMap($Name = "pChart", $StorageMode = IMAGE_MAP_STORAGE_SESSION, $UniqueID = "imageMap", $StorageFolder = "tmp") |
| 300 | { |
| 301 | $this->ImageMapIndex = $Name; |
| 302 | $this->ImageMapStorageMode = $StorageMode; |
| 303 | if ($StorageMode == IMAGE_MAP_STORAGE_SESSION) { |
| 304 | if (!isset($_SESSION)) { |
| 305 | session_start(); |
| 306 | } |
| 307 | $_SESSION[$this->ImageMapIndex] = null; |
| 308 | } elseif ($StorageMode == IMAGE_MAP_STORAGE_FILE) { |
| 309 | $this->ImageMapFileName = $UniqueID; |
| 310 | $this->ImageMapStorageFolder = $StorageFolder; |
| 311 | $path = sprintf("%s/%s.map", $StorageFolder, $UniqueID); |
| 312 | if (file_exists($path)) { |
| 313 | unlink($path); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | /** |
| 318 | * Add a zone to the image map |
| 319 | * |
| 320 | * @param string $Type |
| 321 | * @param string $Plots |
| 322 | * @param string|null $Color |
| 323 | * @param string $Title |
| 324 | * @param string $Message |
| 325 | * @param boolean $HTMLEncode |
| 326 | */ |
| 327 | public function addToImageMap($Type, $Plots, $Color = null, $Title = null, $Message = null, $HTMLEncode = \false) |
| 328 | { |
| 329 | if ($this->ImageMapStorageMode == null) { |
| 330 | $this->initialiseImageMap(); |
| 331 | } |
| 332 | /* Encode the characters in the imagemap in HTML standards */ |
| 333 | $Title = htmlentities(str_replace("€", "\\u20AC", $Title), \ENT_QUOTES, "ISO-8859-15"); |
| 334 | if ($HTMLEncode) { |
| 335 | $Message = str_replace(">", ">", str_replace("<", "<", htmlentities($Message, \ENT_QUOTES, "ISO-8859-15"))); |
| 336 | } |
| 337 | if ($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION) { |
| 338 | if (!isset($_SESSION)) { |
| 339 | $this->initialiseImageMap(); |
| 340 | } |
| 341 | $_SESSION[$this->ImageMapIndex][] = [$Type, $Plots, $Color, $Title, $Message]; |
| 342 | } elseif ($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE) { |
| 343 | $Handle = fopen(sprintf("%s/%s.map", $this->ImageMapStorageFolder, $this->ImageMapFileName), 'a'); |
| 344 | fwrite($Handle, sprintf("%s%s%s%s%s%s%s%s%s\r\n", $Type, IMAGE_MAP_DELIMITER, $Plots, IMAGE_MAP_DELIMITER, $Color, IMAGE_MAP_DELIMITER, $Title, IMAGE_MAP_DELIMITER, $Message)); |
| 345 | fclose($Handle); |
| 346 | } |
| 347 | } |
| 348 | /** |
| 349 | * Remove VOID values from an imagemap custom values array |
| 350 | * @param string $SerieName |
| 351 | * @param array $Values |
| 352 | * @return array |
| 353 | */ |
| 354 | public function removeVOIDFromArray($SerieName, array $Values) |
| 355 | { |
| 356 | if (!isset($this->DataSet->Data["Series"][$SerieName])) { |
| 357 | return -1; |
| 358 | } |
| 359 | $Result = []; |
| 360 | foreach ($this->DataSet->Data["Series"][$SerieName]["Data"] as $Key => $Value) { |
| 361 | if ($Value != VOID && isset($Values[$Key])) { |
| 362 | $Result[] = $Values[$Key]; |
| 363 | } |
| 364 | } |
| 365 | return $Result; |
| 366 | } |
| 367 | /** |
| 368 | * Replace the title of one image map serie |
| 369 | * @param string $OldTitle |
| 370 | * @param string|array $NewTitle |
| 371 | * @return null|int |
| 372 | */ |
| 373 | public function replaceImageMapTitle($OldTitle, $NewTitle) |
| 374 | { |
| 375 | if ($this->ImageMapStorageMode == null) { |
| 376 | return -1; |
| 377 | } |
| 378 | if (is_array($NewTitle)) { |
| 379 | $NewTitle = $this->removeVOIDFromArray($OldTitle, $NewTitle); |
| 380 | } |
| 381 | if ($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION) { |
| 382 | if (!isset($_SESSION)) { |
| 383 | return -1; |
| 384 | } |
| 385 | if (is_array($NewTitle)) { |
| 386 | $ID = 0; |
| 387 | foreach ($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { |
| 388 | if ($Settings[3] == $OldTitle && isset($NewTitle[$ID])) { |
| 389 | $_SESSION[$this->ImageMapIndex][$Key][3] = $NewTitle[$ID]; |
| 390 | $ID++; |
| 391 | } |
| 392 | } |
| 393 | } else { |
| 394 | foreach ($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { |
| 395 | if ($Settings[3] == $OldTitle) { |
| 396 | $_SESSION[$this->ImageMapIndex][$Key][3] = $NewTitle; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } elseif ($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE) { |
| 401 | $TempArray = []; |
| 402 | $Handle = $this->openFileHandle(); |
| 403 | if ($Handle) { |
| 404 | while (($Buffer = fgets($Handle, 4096)) !== \false) { |
| 405 | $Fields = preg_split(sprintf("/%s/", IMAGE_MAP_DELIMITER), str_replace([chr(10), chr(13)], "", $Buffer)); |
| 406 | $TempArray[] = [$Fields[0], $Fields[1], $Fields[2], $Fields[3], $Fields[4]]; |
| 407 | } |
| 408 | fclose($Handle); |
| 409 | if (is_array($NewTitle)) { |
| 410 | $ID = 0; |
| 411 | foreach ($TempArray as $Key => $Settings) { |
| 412 | if ($Settings[3] == $OldTitle && isset($NewTitle[$ID])) { |
| 413 | $TempArray[$Key][3] = $NewTitle[$ID]; |
| 414 | $ID++; |
| 415 | } |
| 416 | } |
| 417 | } else { |
| 418 | foreach ($TempArray as $Key => $Settings) { |
| 419 | if ($Settings[3] == $OldTitle) { |
| 420 | $TempArray[$Key][3] = $NewTitle; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | $Handle = $this->openFileHandle("w"); |
| 425 | foreach ($TempArray as $Key => $Settings) { |
| 426 | fwrite($Handle, sprintf("%s%s%s%s%s%s%s%s%s\r\n", $Settings[0], IMAGE_MAP_DELIMITER, $Settings[1], IMAGE_MAP_DELIMITER, $Settings[2], IMAGE_MAP_DELIMITER, $Settings[3], IMAGE_MAP_DELIMITER, $Settings[4])); |
| 427 | } |
| 428 | fclose($Handle); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | /** |
| 433 | * Replace the values of the image map contents |
| 434 | * @param string $Title |
| 435 | * @param array $Values |
| 436 | * @return null|int |
| 437 | */ |
| 438 | public function replaceImageMapValues($Title, array $Values) |
| 439 | { |
| 440 | if ($this->ImageMapStorageMode == null) { |
| 441 | return -1; |
| 442 | } |
| 443 | $Values = $this->removeVOIDFromArray($Title, $Values); |
| 444 | $ID = 0; |
| 445 | if ($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION) { |
| 446 | if (!isset($_SESSION)) { |
| 447 | return -1; |
| 448 | } |
| 449 | foreach ($_SESSION[$this->ImageMapIndex] as $Key => $Settings) { |
| 450 | if ($Settings[3] == $Title) { |
| 451 | if (isset($Values[$ID])) { |
| 452 | $_SESSION[$this->ImageMapIndex][$Key][4] = $Values[$ID]; |
| 453 | } |
| 454 | $ID++; |
| 455 | } |
| 456 | } |
| 457 | } elseif ($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE) { |
| 458 | $TempArray = []; |
| 459 | $Handle = $this->openFileHandle(); |
| 460 | if ($Handle) { |
| 461 | while (($Buffer = fgets($Handle, 4096)) !== \false) { |
| 462 | $Fields = preg_split("/" . IMAGE_MAP_DELIMITER . "/", str_replace([chr(10), chr(13)], "", $Buffer)); |
| 463 | $TempArray[] = [$Fields[0], $Fields[1], $Fields[2], $Fields[3], $Fields[4]]; |
| 464 | } |
| 465 | fclose($Handle); |
| 466 | foreach ($TempArray as $Key => $Settings) { |
| 467 | if ($Settings[3] == $Title) { |
| 468 | if (isset($Values[$ID])) { |
| 469 | $TempArray[$Key][4] = $Values[$ID]; |
| 470 | } |
| 471 | $ID++; |
| 472 | } |
| 473 | } |
| 474 | $Handle = $this->openFileHandle("w"); |
| 475 | foreach ($TempArray as $Key => $Settings) { |
| 476 | fwrite($Handle, sprintf("%s%s%s%s%s%s%s%s%s\r\n", $Settings[0], IMAGE_MAP_DELIMITER, $Settings[1], IMAGE_MAP_DELIMITER, $Settings[2], IMAGE_MAP_DELIMITER, $Settings[3], IMAGE_MAP_DELIMITER, $Settings[4])); |
| 477 | } |
| 478 | fclose($Handle); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | /** |
| 483 | * Dump the image map |
| 484 | * @param string $Name |
| 485 | * @param int $StorageMode |
| 486 | * @param string $UniqueID |
| 487 | * @param string $StorageFolder |
| 488 | */ |
| 489 | public function dumpImageMap($Name = "pChart", $StorageMode = IMAGE_MAP_STORAGE_SESSION, $UniqueID = "imageMap", $StorageFolder = "tmp") |
| 490 | { |
| 491 | $this->ImageMapIndex = $Name; |
| 492 | $this->ImageMapStorageMode = $StorageMode; |
| 493 | if ($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_SESSION) { |
| 494 | if (!isset($_SESSION)) { |
| 495 | session_start(); |
| 496 | } |
| 497 | if ($_SESSION[$Name] != null) { |
| 498 | foreach ($_SESSION[$Name] as $Key => $Params) { |
| 499 | echo $Params[0] . IMAGE_MAP_DELIMITER . $Params[1] . IMAGE_MAP_DELIMITER . $Params[2] . IMAGE_MAP_DELIMITER . $Params[3] . IMAGE_MAP_DELIMITER . $Params[4] . "\r\n"; |
| 500 | } |
| 501 | } |
| 502 | } elseif ($this->ImageMapStorageMode == IMAGE_MAP_STORAGE_FILE) { |
| 503 | if (file_exists($StorageFolder . "/" . $UniqueID . ".map")) { |
| 504 | $Handle = @fopen($StorageFolder . "/" . $UniqueID . ".map", "r"); |
| 505 | if ($Handle) { |
| 506 | while (($Buffer = fgets($Handle, 4096)) !== \false) { |
| 507 | echo $Buffer; |
| 508 | } |
| 509 | } |
| 510 | fclose($Handle); |
| 511 | if ($this->ImageMapAutoDelete) { |
| 512 | unlink($StorageFolder . "/" . $UniqueID . ".map"); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | /* When the image map is returned to the client, the script ends */ |
| 517 | exit; |
| 518 | } |
| 519 | /** |
| 520 | * Return the HTML converted color from the RGB composite values |
| 521 | * @param int $R |
| 522 | * @param int $G |
| 523 | * @param int $B |
| 524 | * @return string |
| 525 | */ |
| 526 | public function toHTMLColor($R, $G, $B) |
| 527 | { |
| 528 | $R = intval($R); |
| 529 | $G = intval($G); |
| 530 | $B = intval($B); |
| 531 | $R = dechex($R < 0 ? 0 : ($R > 255 ? 255 : $R)); |
| 532 | $G = dechex($G < 0 ? 0 : ($G > 255 ? 255 : $G)); |
| 533 | $B = dechex($B < 0 ? 0 : ($B > 255 ? 255 : $B)); |
| 534 | $Color = "#" . (strlen($R) < 2 ? '0' : '') . $R; |
| 535 | $Color .= (strlen($G) < 2 ? '0' : '') . $G; |
| 536 | $Color .= (strlen($B) < 2 ? '0' : '') . $B; |
| 537 | return $Color; |
| 538 | } |
| 539 | /** |
| 540 | * Reverse an array of points |
| 541 | * @param array $Plots |
| 542 | * @return array |
| 543 | */ |
| 544 | public function reversePlots(array $Plots) |
| 545 | { |
| 546 | $Result = []; |
| 547 | for ($i = count($Plots) - 2; $i >= 0; $i = $i - 2) { |
| 548 | $Result[] = $Plots[$i]; |
| 549 | $Result[] = $Plots[$i + 1]; |
| 550 | } |
| 551 | return $Result; |
| 552 | } |
| 553 | /** |
| 554 | * Mirror Effect |
| 555 | * @param int $X |
| 556 | * @param int $Y |
| 557 | * @param int $Width |
| 558 | * @param int $Height |
| 559 | * @param array $Format |
| 560 | */ |
| 561 | public function drawAreaMirror($X, $Y, $Width, $Height, array $Format = []) |
| 562 | { |
| 563 | $StartAlpha = isset($Format["StartAlpha"]) ? $Format["StartAlpha"] : 80; |
| 564 | $EndAlpha = isset($Format["EndAlpha"]) ? $Format["EndAlpha"] : 0; |
| 565 | $AlphaStep = ($StartAlpha - $EndAlpha) / $Height; |
| 566 | $Picture = imagecreatetruecolor($this->XSize, $this->YSize); |
| 567 | imagecopy($Picture, $this->Picture, 0, 0, 0, 0, $this->XSize, $this->YSize); |
| 568 | for ($i = 1; $i <= $Height; $i++) { |
| 569 | if ($Y + ($i - 1) < $this->YSize && $Y - $i > 0) { |
| 570 | imagecopymerge($Picture, $this->Picture, $X, $Y + ($i - 1), $X, $Y - $i, $Width, 1, $StartAlpha - $AlphaStep * $i); |
| 571 | } |
| 572 | } |
| 573 | imagecopy($this->Picture, $Picture, 0, 0, 0, 0, $this->XSize, $this->YSize); |
| 574 | } |
| 575 | /** |
| 576 | * Open a handle to image storage file. |
| 577 | * @param string $mode |
| 578 | * @return resource |
| 579 | */ |
| 580 | private function openFileHandle($mode = "r") |
| 581 | { |
| 582 | return @fopen(sprintf("%s/%s.map", $this->ImageMapStorageFolder, $this->ImageMapFileName), $mode); |
| 583 | } |
| 584 | } |
| 585 |