PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/main/img.php

https://gitlab.com/alexprowars/bitrix
PHP | 1267 lines | 1118 code | 85 blank | 64 comment | 127 complexity | 6caf73ac9659a8a301f59c74c3c276c3 MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage main
  6. * @copyright 2001-2014 Bitrix
  7. */
  8. /*******************************************************
  9. Converts ISO to UNICODE
  10. ********************************************************/
  11. function iso2uni ($isoline)
  12. {
  13. $uniline = "";
  14. for ($i = 0, $n = mb_strlen($isoline); $i < $n; $i++)
  15. {
  16. $thischar = mb_substr($isoline, $i, 1);
  17. $charcode = ord($thischar);
  18. $uniline .= ($charcode>175) ? "&#" . (1040+($charcode-176)). ";" : $thischar;
  19. }
  20. return $uniline;
  21. }
  22. /*******************************************************
  23. Creates image to draw on
  24. ********************************************************/
  25. function CreateImageHandle($width, $height, $background="FFFFFF", $truecolor=true)
  26. {
  27. if($truecolor)
  28. {
  29. $im = ImageCreateTrueColor($width,$height);
  30. }
  31. else
  32. {
  33. $im = ImageCreate($width,$height);
  34. }
  35. if (!$im)
  36. {
  37. die ("Cannot Initialize GD image stream");
  38. }
  39. else
  40. {
  41. $dec = ReColor($background);
  42. ImageColorAllocate ($im, $dec[0], $dec[1], $dec[2]);
  43. }
  44. return $im;
  45. }
  46. /******************************************************
  47. Send proper headers for image
  48. *******************************************************/
  49. function ShowImageHeader($ImageHandle)
  50. {
  51. if (ImageTypes() & IMG_PNG)
  52. {
  53. Header("Content-type: image/png");
  54. ImagePng($ImageHandle);
  55. }
  56. elseif(ImageTypes() & IMG_GIF)
  57. {
  58. Header("Content-type: image/gif");
  59. ImageGif($ImageHandle);
  60. }
  61. elseif (ImageTypes() & IMG_JPEG)
  62. {
  63. Header("Content-type: image/jpeg");
  64. ImageJpeg($ImageHandle, "", 0.5);
  65. }
  66. else
  67. {
  68. die("No images support");
  69. }
  70. ImageDestroy ($ImageHandle);
  71. }
  72. /******************************************************
  73. Returns some color
  74. *******************************************************/
  75. function GetArrSaveDecColor($arr)
  76. {
  77. $arrSaveDecColor = array();
  78. foreach($arr as $key => $scolor)
  79. {
  80. $arrSaveDecColor[$key] = hexdec($scolor);
  81. }
  82. asort($arrSaveDecColor);
  83. return $arrSaveDecColor;
  84. }
  85. function GetNextRGB($base_color, $total)
  86. {
  87. $arrSaveColor = getSafeColors();
  88. $tsc = count($arrSaveColor);
  89. if ($total > $tsc)
  90. {
  91. return GetBNextRGB($base_color, $total);
  92. }
  93. elseif ($base_color == '')
  94. {
  95. $res = "1288A0";
  96. }
  97. else
  98. {
  99. $index = 0;
  100. $step = round($tsc/$total);
  101. $dec = hexdec($base_color);
  102. $arrSaveDecColor = GetArrSaveDecColor($arrSaveColor);
  103. foreach($arrSaveDecColor as $key => $sdcolor)
  104. {
  105. if ($dec <= $sdcolor)
  106. {
  107. $index = $key;
  108. break;
  109. }
  110. }
  111. $index = intval($index);
  112. $tsc = $tsc-1;
  113. if ($index + $step > $tsc)
  114. {
  115. $rkey = ($index + $step) - $tsc;
  116. }
  117. else
  118. {
  119. $rkey = $index + $step;
  120. }
  121. $res = $arrSaveColor[$rkey];
  122. }
  123. return $res;
  124. }
  125. function GetBNextRGB($base_color, $total, $start_color = "999900", $end_color = "99FFFF")
  126. {
  127. $step = round((hexdec($end_color) - hexdec($start_color)) / $total);
  128. $dec = intval(hexdec($base_color)) + intval($step);
  129. if ($dec < hexdec($start_color) - $step)
  130. {
  131. $dec = $start_color;
  132. }
  133. elseif ($dec > hexdec($end_color) + $step)
  134. {
  135. $dec = $end_color;
  136. }
  137. elseif ($dec > hexdec("FFFFFF"))
  138. {
  139. $dec = "000000";
  140. }
  141. else
  142. {
  143. $dec = sprintf("%06X", $dec);
  144. }
  145. return $dec;
  146. }
  147. /*******************************************************
  148. Graph data debug
  149. *******************************************************/
  150. function EchoGraphData($arrayX, $MinX, $MaxX, $arrayY, $MinY, $MaxY, $arrX, $arrY, $die=true)
  151. {
  152. echo "<pre>";
  153. echo "--------------------------------------\n";
  154. foreach($arrX as $key => $value)
  155. {
  156. echo date("d.m.Y",$value)." = ".$arrY[$key]."\n";
  157. }
  158. echo "--------------------------------------\n";
  159. echo "Signs of X axis (arrayX):\n";
  160. print_r($arrayX);
  161. echo "MinX: ".$MinX." - ".date("d.m",$MinX)."\n";
  162. echo "MaxX: ".$MaxX." - ".date("d.m",$MaxX)."\n\n";
  163. echo "Signs of Y axis (arrayY):\n";
  164. print_r($arrayY);
  165. echo "MinY: ".$MinY."\n";
  166. echo "MaxY: ".$MaxY."\n\n";
  167. echo "Values of X axis (arrX):\n";
  168. $i = 0;
  169. foreach ($arrX as $d)
  170. {
  171. echo "[".$i."] => ".GetTime($d)." (".$d.")"."\n";
  172. $i++;
  173. }
  174. echo "\nValues of Y axis (arrY):\n";
  175. print_r($arrY);
  176. echo "--------------------------------------\n";
  177. echo "</pre>";
  178. if ($die)
  179. {
  180. die();
  181. }
  182. }
  183. /*******************************************************
  184. Makes proper X axis (date)
  185. *******************************************************/
  186. function GetArrayX($arrX, &$MinX, &$MaxX, $max_grid=15, $min_grid=10)
  187. {
  188. $h = 2;
  189. $MinX = (count($arrX)>0) ? min($arrX) : 0;
  190. $MaxX = (count($arrX)>0) ? max($arrX) : 0;
  191. $period_days = (($MaxX-$MinX)/86400)+1;
  192. if ($period_days>$min_grid)
  193. {
  194. $h = $min_grid;
  195. }
  196. if ($max_grid<$h)
  197. {
  198. $max_grid = $h;
  199. }
  200. $arrOst = array();
  201. for ($i=$max_grid; $i>=$h; $i--)
  202. {
  203. $ost = $period_days%$i;
  204. $arrOst[$i] = $ost;
  205. if ($ost == 0)
  206. {
  207. break;
  208. }
  209. }
  210. $minOst = min($arrOst);
  211. $shiftX = ($period_days/array_search($minOst, $arrOst));
  212. $shiftX = $shiftX*86400;
  213. $unix_date = $MinX;
  214. if(preg_match("/(DD|MM)(.*?)(DD|MM)/",FORMAT_DATE,$arMatch))
  215. {
  216. $strFrmt = str_replace(array("DD","MM"), array("d","m"), $arMatch[0]);
  217. }
  218. else
  219. {
  220. $strFrmt = "d.m";
  221. }
  222. $prev_date = "";
  223. $tmp_arrX = array();
  224. $arrayX = array();
  225. while ($unix_date < $MaxX+$shiftX)
  226. {
  227. if ($prev_date == date("d.m.Y", $unix_date))
  228. {
  229. $unix_date += 3600;
  230. }
  231. $date = date($strFrmt, $unix_date);
  232. $arrayX[] = $date;
  233. $tmp_arrX[] = $unix_date;
  234. $unix_date += $shiftX;
  235. $prev_date = date("d.m.Y", $unix_date);
  236. }
  237. $MinX = MkDateTime(date("d.m.Y", min($tmp_arrX)),"d.m.Y");
  238. $MaxX = MkDateTime(date("d.m.Y", max($tmp_arrX)),"d.m.Y");
  239. return $arrayX;
  240. }
  241. function GetArrayY($arrY, &$MinY, &$MaxY, $max_grid=15, $first_null="Y", $integers=false)
  242. {
  243. $arrayY = array();
  244. $arrY = array_unique($arrY);
  245. if ($first_null=="Y")
  246. {
  247. $arrY[] = 0;
  248. }
  249. asort($arrY);
  250. $MinY = min($arrY);
  251. $MaxY = max($arrY);
  252. if ($MinY==$MaxY)
  253. {
  254. if ($MinY!=0)
  255. {
  256. $arrayY[] = 0;
  257. }
  258. $arrayY[] = $MinY;
  259. $arrayY[] = $MaxY+1;
  260. asort($arrayY);
  261. }
  262. else
  263. {
  264. $shiftY = round(($MaxY-$MinY)/$max_grid);
  265. if($shiftY<=0)
  266. {
  267. if($integers==false)
  268. {
  269. $shiftY = round(($MaxY-$MinY)/$max_grid,3);
  270. }
  271. else
  272. {
  273. $shiftY = 1;
  274. }
  275. }
  276. $i = $MinY;
  277. if ($shiftY>0)
  278. {
  279. while ($i<$MaxY+$shiftY+$shiftY)
  280. {
  281. $arrayY[] = $i;
  282. $i += $shiftY;
  283. }
  284. }
  285. else
  286. {
  287. for ($i=$MinY; $i<=$MaxY+$shiftY+$shiftY; $i++)
  288. {
  289. $arrayY[] = $i;
  290. }
  291. }
  292. }
  293. $MinY = min($arrayY);
  294. $MaxY = max($arrayY);
  295. return $arrayY;
  296. }
  297. /******************************************************************************
  298. * $colorString - Color. Example 'FFFFFF' or '#FF0000'
  299. * ReColor - function converting HEX to DEC color
  300. ******************************************************************************/
  301. function ReColor($colorString)
  302. {
  303. if (!is_string($colorString))
  304. return 0;
  305. if (!preg_match('/^#{0,1}([0-9a-z]{2})([0-9a-z]{2})([0-9a-z]{2})$/i', $colorString, $match))
  306. return 0;
  307. return array(
  308. hexdec($match[1]),
  309. hexdec($match[2]),
  310. hexdec($match[3]),
  311. );
  312. }
  313. function DrawCoordinatGrid($arrayX, $arrayY, $width, $height, $ImageHandle, $bgColor="FFFFFF", $gColor='B1B1B1', $Color="000000", $dD=15, $FontWidth=2, $arrTTF_FONT=false)
  314. {
  315. global $xA, $yA, $xPixelLength, $yPixelLength;
  316. /******************************************************************************
  317. * $k - array performance font size to pixel format
  318. * array index == font size
  319. ******************************************************************************/
  320. $k = array();
  321. $k[1]=5;
  322. $k[2]=2.7;
  323. $k[3]=2.3;
  324. $k[4]=2;
  325. $k[5]=1.7;
  326. $k[6]=1.5;
  327. $k[7]=1.3;
  328. $k[8]=1.1;
  329. $k[9]=1;
  330. $k[10]=0.85;
  331. $k[11]=0.75;
  332. $k[12]=0.7;
  333. $k[13]=0.65;
  334. $k[14]=0.60;
  335. $k[15]=0.55;
  336. $k[16]=0.52;
  337. $arResult = array();
  338. $max_len=0;
  339. $bUseTTFY = false;
  340. $bUseTTFX = false;
  341. if(is_array($arrTTF_FONT) && function_exists("ImageTTFText"))
  342. {
  343. $bUseTTFY = is_array($arrTTF_FONT["Y"] ?? null);
  344. $bUseTTFX = is_array($arrTTF_FONT["X"] ?? null);
  345. }
  346. $ttf_font_y = "";
  347. $ttf_size_y = $ttf_shift_y = $ttf_base_y = 0;
  348. if ($bUseTTFY)
  349. {
  350. $ttf_font_y = $_SERVER["DOCUMENT_ROOT"].$arrTTF_FONT["Y"]["FONT_PATH"];
  351. $ttf_size_y = $arrTTF_FONT["Y"]["FONT_SIZE"];
  352. $ttf_shift_y = $arrTTF_FONT["Y"]["FONT_SHIFT"];
  353. $ttf_base_y = 0;
  354. if (isset($arrTTF_FONT["Y"]["FONT_BASE"])) $ttf_base_y = $arrTTF_FONT["Y"]["FONT_BASE"];
  355. $dlataX = 0;
  356. foreach($arrayY as $value)
  357. {
  358. $bbox = imagettfbbox($ttf_size_y, 0, $ttf_font_y, $value);
  359. $dlataX = max($dlataX, abs($bbox[2] - $bbox[0]) + 1);
  360. }
  361. }
  362. else
  363. {
  364. foreach($arrayY as $value)
  365. $max_len=max($max_len, mb_strlen($value));
  366. $dlataX = $max_len*ImageFontWidth($FontWidth);
  367. }
  368. $arr_bgColor = ReColor($bgColor);
  369. $colorFFFFFF = ImageColorAllocate($ImageHandle,$arr_bgColor[0],$arr_bgColor[1],$arr_bgColor[2]);
  370. $arr_Color = ReColor($Color);
  371. $color000000 = ImageColorAllocate($ImageHandle,$arr_Color[0],$arr_Color[1],$arr_Color[2]);
  372. $arr_gColor = ReColor($gColor);
  373. $colorCOCOCO = ImageColorAllocate($ImageHandle,$arr_gColor[0], $arr_gColor[1], $arr_gColor[2]);
  374. ImageFill($ImageHandle, 0, 0, $colorFFFFFF);
  375. $bForBarDiagram = is_array($arrTTF_FONT) && (($arrTTF_FONT["type"] ?? '') == "bar");
  376. if($bForBarDiagram)
  377. {
  378. $arResult["XBUCKETS"] = array();
  379. }
  380. /*
  381. C
  382. |
  383. |
  384. |
  385. |
  386. |__________________
  387. A B
  388. */
  389. $ttf_font_x = "";
  390. $ttf_size_x = $ttf_shift_x = $ttf_base_x = 0;
  391. $xA = $dD+$dlataX;
  392. if ($bUseTTFX)
  393. {
  394. $ttf_font_x = $_SERVER["DOCUMENT_ROOT"].$arrTTF_FONT["X"]["FONT_PATH"];
  395. $ttf_size_x = $arrTTF_FONT["X"]["FONT_SIZE"];
  396. $ttf_shift_x = $arrTTF_FONT["X"]["FONT_SHIFT"];
  397. if (isset($arrTTF_FONT["X"]["FONT_BASE"]))
  398. {
  399. $ttf_base_x = $arrTTF_FONT["X"]["FONT_BASE"];
  400. }
  401. $yA = $height-$dD-$ttf_shift_x;
  402. }
  403. else
  404. {
  405. $yA = $height-$dD-ImageFontHeight($FontWidth)/2;
  406. }
  407. $xC = $xA;
  408. $yC = $dD;
  409. $xB = $width-$dD;
  410. $yB = $yA;
  411. $GrafWidth = $xB - $xA;
  412. $GrafHeight = $yA - $yC;
  413. $PointsX = max(sizeof($arrayX)+$bForBarDiagram, 2);
  414. $PointsY = max(sizeof($arrayY), 2);
  415. $dX = $GrafWidth/($PointsX-1);
  416. $dY = $GrafHeight/($PointsY-1);
  417. /*
  418. C P1
  419. | |
  420. | |
  421. | |
  422. | |
  423. |___|______________
  424. A P0 B
  425. */
  426. $i=0;
  427. $xP0 = $xA;
  428. $yP0 = $yA;
  429. $yP1 = $yC;
  430. while ($i < $PointsX)
  431. {
  432. if ($i==$PointsX-1)
  433. {
  434. $xP0 = $xB;
  435. }
  436. $style = array (
  437. $colorCOCOCO,
  438. IMG_COLOR_TRANSPARENT,
  439. IMG_COLOR_TRANSPARENT,
  440. );
  441. ImageSetStyle($ImageHandle, $style);
  442. ImageLine($ImageHandle, ceil($xP0), ceil($yP0), ceil($xP0), ceil($yP1), IMG_COLOR_STYLED);
  443. if($bForBarDiagram)
  444. $arResult["XBUCKETS"][$i] = array(ceil($xP0)+1, ceil($xP0+$dX)-1);
  445. $captionX = $arrayX[$i];
  446. $xCaption = $xP0 - mb_strlen($captionX) * $k[$FontWidth] + ($dX*$bForBarDiagram/2);
  447. $yCaption = $yP0;
  448. if ($bUseTTFX)
  449. {
  450. $bbox = imagettfbbox($ttf_size_x, 0, $ttf_font_x, $captionX);
  451. $ttf_width_x = abs($bbox[2] - $bbox[0]) + 1;
  452. $xCaption = $xP0 - $ttf_width_x/2 + ($dX*$bForBarDiagram/2);
  453. $yCaption = $yP0 + $dD + $ttf_shift_x - $ttf_base_x;
  454. $captionX = \Bitrix\Main\Text\Encoding::convertEncoding($captionX, LANG_CHARSET, "UTF-8");
  455. ImageTTFText($ImageHandle, $ttf_size_x, 0, $xCaption, $yCaption, $color000000, $ttf_font_x, $captionX);
  456. }
  457. else ImageString($ImageHandle, $FontWidth, $xCaption, $yCaption+ImageFontHeight($FontWidth)/2, $captionX, $color000000);
  458. $xP0 += $dX;
  459. $i++;
  460. }
  461. /*
  462. C
  463. |
  464. |
  465. |
  466. M1|___________________ M0
  467. |___________________
  468. A B
  469. */
  470. $i=0;
  471. $xM0 = $xB;
  472. $yM0 = $yB;
  473. $xM1 = $xA;
  474. $yM1 = $yA;
  475. while ($i < $PointsY)
  476. {
  477. if ($i==$PointsY-1)
  478. {
  479. $yM0 = $yC;
  480. $yM1 = $yC;
  481. }
  482. if ($yM1>0 && $yM0>0)
  483. {
  484. $style = array (
  485. $colorCOCOCO,
  486. IMG_COLOR_TRANSPARENT,
  487. IMG_COLOR_TRANSPARENT,
  488. );
  489. ImageSetStyle($ImageHandle, $style);
  490. ImageLine($ImageHandle, ceil($xM0), ceil($yM0), ceil($xM1), ceil($yM1), IMG_COLOR_STYLED);
  491. $captionY = $arrayY[$i];
  492. $xCaption = $dlataX;
  493. $yCaption = $yM1-$k[$FontWidth]*3;
  494. if ($bUseTTFY)
  495. {
  496. $captionY = \Bitrix\Main\Text\Encoding::convertEncoding($captionY, LANG_CHARSET, "UTF-8");
  497. $bbox = imagettfbbox($ttf_size_y, 0, $ttf_font_y, $captionY);
  498. $yCaption = $yM1+($ttf_shift_y-$ttf_base_y)/2;
  499. ImageTTFText($ImageHandle, $ttf_size_y, 0, $xCaption-abs($bbox[2]-$bbox[0])-1, $yCaption, $color000000, $ttf_font_y, $captionY);
  500. }
  501. else ImageString($ImageHandle, $FontWidth, $xCaption- mb_strlen($captionY) * ImageFontWidth($FontWidth), $yCaption, $captionY, $color000000);
  502. }
  503. $yM0 -= $dY;
  504. $yM1 -= $dY;
  505. $i++;
  506. }
  507. ImageLine($ImageHandle, ceil($xA), ceil($yA), ceil($xC), ceil($yC), $color000000);
  508. ImageLine($ImageHandle, ceil($xB), ceil($yB), ceil($xA), ceil($yA), $color000000);
  509. $xPixelLength = $xB - $xA;
  510. $yPixelLength = $yA - $yC;
  511. $arResult["VIEWPORT"] = array(ceil($xA), ceil($yA), ceil($xB), ceil($yC));
  512. return $arResult;
  513. }
  514. function Bar_Diagram($ImageHandle, $arData, $MinY, $MaxY, $gridInfo)
  515. {
  516. $max_y = 0;
  517. foreach($arData as $arRecs)
  518. {
  519. $y = max($arRecs["DATA"]);
  520. if($y > $max_y)
  521. $max_y = $y;
  522. }
  523. $scale = ($gridInfo["VIEWPORT"][1] - $gridInfo["VIEWPORT"][3]) / ($MaxY - $MinY);
  524. $xIndex = 0;
  525. foreach($arData as $arRecs)
  526. {
  527. $arPair = $gridInfo["XBUCKETS"][$xIndex];
  528. if (is_array($arPair))
  529. {
  530. $bar_count = count($arRecs["DATA"]);
  531. $bar_width = ceil(($arPair[1] - $arPair[0] - 1) * 0.7 / $bar_count);
  532. $ws_width = round((($arPair[1] - $arPair[0] - 1) - ($bar_width * $bar_count)) / ($bar_count + 1));
  533. foreach($arRecs["DATA"] as $i => $Y)
  534. {
  535. $arColor = ReColor($arRecs["COLORS"][$i][0]);
  536. $color = ImageColorAllocate($ImageHandle, $arColor[0], $arColor[1], $arColor[2]);
  537. $x1 = $arPair[0] + $ws_width + ($bar_width + $ws_width)*$i;
  538. $y1 = round($Y*$scale);
  539. if($y1 > 0)
  540. {
  541. imagefilledrectangle($ImageHandle,
  542. $x1,
  543. $gridInfo["VIEWPORT"][1]-$y1,
  544. $x1 + $bar_width,
  545. $gridInfo["VIEWPORT"][1]-1,
  546. $color);
  547. }
  548. }
  549. }
  550. $xIndex++;
  551. }
  552. }
  553. function Graf($arrayX, $arrayY, $ImageHandle, $MinX, $MaxX, $MinY, $MaxY, $Color='FF0000', $dashed="N", $thikness=2, $antialiase=true)
  554. {
  555. global $xA, $yA, $xPixelLength, $yPixelLength;
  556. if(sizeof($arrayX) != sizeof($arrayY))
  557. {
  558. return;
  559. }
  560. $arr_Color = ReColor($Color);
  561. $color = ImageColorAllocate($ImageHandle, $arr_Color[0], $arr_Color[1], $arr_Color[2]);
  562. $xGrafLength = $MaxX - $MinX;
  563. $yGrafLength = $MaxY - $MinY;
  564. if($antialiase)
  565. {
  566. $bgcolor = imagecolorallocate($ImageHandle, 255, 255, 255);
  567. $fgcolors = imagecolorsforindex($ImageHandle, $color);
  568. $bgcolors = imagecolorsforindex($ImageHandle, $bgcolor);
  569. for( $i = 0; $i < 100; $i++ )
  570. {
  571. imagecolorallocate(
  572. $ImageHandle,
  573. ($fgcolors['red'] + $i*$bgcolors['red'])/($i + 1),
  574. ($fgcolors['green'] + $i*$bgcolors['green'])/($i + 1),
  575. ($fgcolors['blue'] + $i*$bgcolors['blue'])/($i + 1)
  576. );
  577. }
  578. }
  579. $x1 = $y1 = $x2 = $y2 = 0;
  580. for($i = 0, $n = sizeof($arrayX)-1; $i < $n; $i++)
  581. {
  582. if ($xGrafLength>0)
  583. {
  584. $x1 = $xA + ((($arrayX[$i]-$MinX) * $xPixelLength) / $xGrafLength);
  585. $x2 = $xA + ((($arrayX[$i+1]-$MinX) * $xPixelLength) / $xGrafLength);
  586. }
  587. if ($yGrafLength>0)
  588. {
  589. $y1 = $yA - ((($arrayY[$i]-$MinY) * $yPixelLength) / $yGrafLength);
  590. $y2 = $yA - ((($arrayY[$i+1]-$MinY) * $yPixelLength) / $yGrafLength);
  591. }
  592. $x1 = ceil($x1);
  593. $y1 = ceil($y1);
  594. $x2 = ceil($x2);
  595. $y2 = ceil($y2);
  596. if($antialiase)
  597. {
  598. /** @noinspection PhpUndefinedVariableInspection */
  599. _a_draw_line($ImageHandle, $x1, $y1, $x2, $y2, $fgcolors, $dashed, 10, 4);
  600. if($thikness>1)
  601. {
  602. if($y1<$y2)
  603. {
  604. _a_draw_line($ImageHandle, $x1-0.4, $y1+0.4, $x2-0.4, $y2+0.4, $fgcolors, $dashed, 10, 4);
  605. _a_draw_line($ImageHandle, $x1+0.4, $y1-0.4, $x2+0.4, $y2-0.4, $fgcolors, $dashed, 10, 4);
  606. }
  607. else
  608. {
  609. _a_draw_line($ImageHandle, $x1+0.4, $y1+0.4, $x2+0.4, $y2+0.4, $fgcolors, $dashed, 10, 4);
  610. _a_draw_line($ImageHandle, $x1-0.4, $y1-0.4, $x2-0.4, $y2-0.4, $fgcolors, $dashed, 10, 4);
  611. }
  612. }
  613. }
  614. elseif($dashed=="Y")
  615. {
  616. $style = array (
  617. $color,$color,
  618. IMG_COLOR_TRANSPARENT,
  619. IMG_COLOR_TRANSPARENT,
  620. IMG_COLOR_TRANSPARENT
  621. );
  622. ImageSetStyle($ImageHandle, $style);
  623. ImageLine($ImageHandle, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED);
  624. }
  625. else
  626. {
  627. ImageLine($ImageHandle, $x1, $y1, $x2, $y2, $color);
  628. }
  629. }
  630. }
  631. function Draw_Sector($ImageHandle, $start, $end, $color, $diameter, $centerX, $centerY)
  632. {
  633. $radius = $diameter/2;
  634. $dec = ReColor($color);
  635. $color = ImageColorAllocate ($ImageHandle, $dec[0], $dec[1], $dec[2]);
  636. imagearc($ImageHandle, $centerX, $centerY, $diameter, $diameter, 0, 360, $color);
  637. $startX = $centerX + cos(deg2rad($start)) * $radius;
  638. $startY = $centerY + sin(deg2rad($start)) * $radius;
  639. imageline($ImageHandle, $centerX, $centerY, $startX, $startY, $color);
  640. $endX = $centerX + cos(deg2rad($end)) * $radius;
  641. $endY = $centerY + sin(deg2rad($end)) * $radius;
  642. imageline($ImageHandle, $centerX, $centerY, $endX, $endY, $color);
  643. $diff = intval($end - $start);
  644. if ($diff < 180)
  645. {
  646. $x = ($centerX+(($startX + $endX)/2))/2;
  647. $y = ($centerY+(($startY + $endY)/2))/2;
  648. }
  649. else
  650. {
  651. $m_end = $start + $diff/2;
  652. $m_X = $centerX + cos(deg2rad($m_end)) * $radius;
  653. $m_Y = $centerY + sin(deg2rad($m_end)) * $radius;
  654. $x = ($centerX+$m_X)/2;
  655. $y = ($centerY+$m_Y)/2;
  656. //ImageString($ImageHandle, 2, 30, 30, $m_end, $color);
  657. //imagesetpixel($ImageHandle, $m_X, $m_Y, ImageColorAllocate($ImageHandle,"FF", "00", "00"));
  658. }
  659. imagefill ($ImageHandle, $x, $y, $color);
  660. //imagesetpixel($ImageHandle, $x, $y, $color);
  661. }
  662. function Circular_Diagram($ImageHandle, $arr, $background_color, $diameter, $centerX, $centerY, $antialiase=true)
  663. {
  664. if($antialiase)
  665. {
  666. $ImageHandle_Saved = $ImageHandle;
  667. $diameter_saved = $diameter;
  668. $diameter=$diameter*5;
  669. $centerX=$centerX*5;
  670. $centerY=$centerY*5;
  671. $ImageHandle = CreateImageHandle($diameter, $diameter, "FFFFFF", true);
  672. imagefill($ImageHandle, 0, 0, imagecolorallocate($ImageHandle, 255,255,255));
  673. }
  674. $arr2 = array();
  675. $diameterX = $diameter;
  676. $diameterY = intval($diameter*0.6);
  677. if(count($arr)>0)
  678. {
  679. $sum = 0;
  680. foreach($arr as $sector)
  681. {
  682. $sum += $sector["COUNTER"];
  683. }
  684. $degree1=0;
  685. $p=0.0;
  686. $i=0;
  687. foreach($arr as $sector)
  688. {
  689. $p += $sector["COUNTER"]/$sum*360.0;
  690. ++$i;
  691. if ($i==count($arr))
  692. {
  693. $degree2 = 360;
  694. }
  695. else
  696. {
  697. $degree2 = intval($p);
  698. }
  699. if($degree2 > $degree1)
  700. {
  701. $dec = ReColor($sector["COLOR"]);
  702. $arr2[] = array(
  703. "DEGREE_1" => $degree1,
  704. "DEGREE_2" => $degree2,
  705. "COLOR" => $sector["COLOR"],
  706. "IMAGE_COLOR" => ImageColorAllocate ($ImageHandle, $dec[0], $dec[1], $dec[2]),
  707. "IMAGE_DARK" => ImageColorAllocate ($ImageHandle, $dec[0]/1.5, $dec[1]/1.5, $dec[2]/1.5),
  708. );
  709. $degree1 = $degree2;
  710. }
  711. }
  712. if(count($arr2)>0)
  713. {
  714. $h = 15;
  715. if($antialiase)
  716. {
  717. $h = $h * 5;
  718. }
  719. for($i = 0; $i <= $h; $i++)
  720. {
  721. foreach($arr2 as $sector)
  722. {
  723. $degree1 = $sector["DEGREE_1"];
  724. $degree2 = $sector["DEGREE_2"];
  725. $difference = $degree2 - $degree1;
  726. $degree1 -= 180;
  727. $degree1 = $degree1<0?360+$degree1:$degree1;
  728. $degree2 -= 180;
  729. $degree2 = $degree2<0?360+$degree2:$degree2;
  730. $color = $i==$h?$sector["IMAGE_COLOR"]:$sector["IMAGE_DARK"];
  731. if ($difference==360)
  732. imageellipse($ImageHandle, $centerX, $centerY-$i, $diameterX, $diameterY, $color);
  733. else
  734. imagearc($ImageHandle, $centerX, $centerY-$i, $diameterX, $diameterY, $degree1, $degree2, $color);
  735. }
  736. }
  737. $i--;
  738. foreach($arr2 as $sector)
  739. {
  740. $degree1 = $sector["DEGREE_1"];
  741. $degree2 = $sector["DEGREE_2"];
  742. $difference = $degree2 - $degree1;
  743. $degree1 -= 180;
  744. $degree1 = $degree1<0?360+$degree1:$degree1;
  745. $degree2 -= 180;
  746. $degree2 = $degree2<0?360+$degree2:$degree2;
  747. $color = $i==$h?$sector["IMAGE_COLOR"]:$sector["IMAGE_DARK"];
  748. if ($difference==360)
  749. imagefilledellipse($ImageHandle, $centerX, $centerY-$i, $diameterX, $diameterY, $color);
  750. else
  751. {
  752. imagefilledarc($ImageHandle, $centerX, $centerY-$i, $diameterX, $diameterY, $degree1, $degree2, $color, IMG_ARC_PIE);
  753. }
  754. }
  755. }
  756. }
  757. else
  758. {
  759. $dec = ReColor($background_color);
  760. $color= ImageColorAllocate ($ImageHandle, $dec[0], $dec[1], $dec[2]);
  761. imagefilledellipse($ImageHandle, $centerX, $centerY, $diameterX, $diameterY, $color);
  762. }
  763. if($antialiase)
  764. {
  765. /** @noinspection PhpUndefinedVariableInspection */
  766. imagecopyresampled($ImageHandle_Saved, $ImageHandle, 0, 0, 0, 0, $diameter_saved, $diameter_saved, $diameter, $diameter);
  767. }
  768. }
  769. function Clean_Circular_Diagram($ImageHandle, $background_color, $diameter, $centerX, $centerY)
  770. {
  771. $dec = ReColor($background_color);
  772. $color = ImageColorAllocate ($ImageHandle, $dec[0], $dec[1], $dec[2]);
  773. for($i=0;$i<=$diameter;$i++) imagearc($ImageHandle, $centerX, $centerY, $diameter+$i, $diameter+$i, 0, 360, $color);
  774. }
  775. function _a_set_pixel($im, $x, $y, $filled, $fgcolors)
  776. {
  777. $rgb=imagecolorat($im, $x, $y);
  778. $r = ($rgb >> 16) & 0xFF;
  779. $g = ($rgb >> 8) & 0xFF;
  780. $b = $rgb & 0xFF;
  781. $red = round($r + ( $fgcolors['red'] - $r ) * $filled);
  782. $green = round($g + ( $fgcolors['green'] - $g ) * $filled);
  783. $blue = round($b + ( $fgcolors['blue'] - $b ) * $filled);
  784. imagesetpixel($im, $x, $y, imagecolorclosest($im, $red, $green, $blue));
  785. }
  786. function _a_frac($x)
  787. {
  788. $x = doubleval($x);
  789. return $x-floor($x);
  790. }
  791. function _a_draw_line($im, $x1, $y1, $x2, $y2, $fgcolors, $dashed="N", $dash=5, $white=2)
  792. {
  793. $xd = $x2-$x1;
  794. $yd = $y2-$y1;
  795. if($xd==0 && $yd==0)
  796. {
  797. return;
  798. }
  799. if(abs($xd)>abs($yd))
  800. {
  801. $wasexchange = false;
  802. }
  803. else
  804. {
  805. $wasexchange = true;
  806. $tmpreal = $x1;
  807. $x1 = $y1;
  808. $y1 = $tmpreal;
  809. $tmpreal = $x2;
  810. $x2 = $y2;
  811. $y2 = $tmpreal;
  812. $tmpreal = $xd;
  813. $xd = $yd;
  814. $yd = $tmpreal;
  815. }
  816. if( $x1>$x2 )
  817. {
  818. $tmpreal = $x1;
  819. $x1 = $x2;
  820. $x2 = $tmpreal;
  821. $tmpreal = $y1;
  822. $y1 = $y2;
  823. $y2 = $tmpreal;
  824. $xd = $x2-$x1;
  825. $yd = $y2-$y1;
  826. }
  827. $grad = $yd/$xd;
  828. $xend = floor($x1+0.5);
  829. $yend = $y1+$grad*($xend-$x1);
  830. $xgap = 1-_a_frac($x1+0.5);
  831. $ix1 = floor($x1+0.5);
  832. $iy1 = floor($yend);
  833. $brightness1 = (1-_a_frac($yend))*$xgap;
  834. $brightness2 = _a_frac($yend)*$xgap;
  835. if( $wasexchange )
  836. {
  837. _a_set_pixel($im, $iy1, $ix1, $brightness1, $fgcolors);
  838. _a_set_pixel($im, $iy1+1, $ix1, $brightness2, $fgcolors);
  839. }
  840. else
  841. {
  842. _a_set_pixel($im, $ix1, $iy1, $brightness1, $fgcolors);
  843. _a_set_pixel($im, $ix1, $iy1+1, $brightness2, $fgcolors);
  844. }
  845. $yf = $yend+$grad;
  846. $xend = floor($x2+0.5);
  847. $yend = $y2+$grad*($xend-$x2);
  848. $xgap = 1-_a_frac($x2-0.5);
  849. $ix2 = floor($x2+0.5);
  850. $iy2 = floor($yend);
  851. $brightness1 = (1-_a_frac($yend))*$xgap;
  852. $brightness2 = _a_frac($yend)*$xgap;
  853. if( $wasexchange )
  854. {
  855. _a_set_pixel($im, $iy2, $ix2, $brightness1, $fgcolors);
  856. _a_set_pixel($im, $iy2+1, $ix2, $brightness2, $fgcolors);
  857. }
  858. else
  859. {
  860. _a_set_pixel($im, $ix2, $iy2, $brightness1, $fgcolors);
  861. _a_set_pixel($im, $ix2, $iy2+1, $brightness2, $fgcolors);
  862. }
  863. $kk=0;
  864. for($x = $ix1+1; $x <= $ix2-1; $x++)
  865. {
  866. if(($kk % $dash)<($dash-$white))
  867. {
  868. $brightness1 = 1-_a_frac($yf);
  869. $brightness2 = _a_frac($yf);
  870. if( $wasexchange )
  871. {
  872. _a_set_pixel($im, floor($yf), $x, $brightness1, $fgcolors);
  873. _a_set_pixel($im, floor($yf)+1, $x, $brightness2, $fgcolors);
  874. }
  875. else
  876. {
  877. _a_set_pixel($im, $x, floor($yf), $brightness1, $fgcolors);
  878. _a_set_pixel($im, $x, floor($yf)+1, $brightness2, $fgcolors);
  879. }
  880. }
  881. $yf = $yf+$grad;
  882. if($dashed=="Y")
  883. ++$kk;
  884. }
  885. }
  886. function _a_draw_ellipse($im, $x1, $y1, $x2, $y2, $fgcolors, $half=false)
  887. {
  888. if( $x2<$x1 )
  889. {
  890. $t = $x1;
  891. $x1 = $x2;
  892. $x2 = $t;
  893. }
  894. if( $y2<$y1 )
  895. {
  896. $t = $y1;
  897. $y1 = $y2;
  898. $y2 = $t;
  899. }
  900. if( $x2-$x1<$y2-$y1 )
  901. {
  902. $exch = false;
  903. }
  904. else
  905. {
  906. $exch = true;
  907. $t = $x1;
  908. $x1 = $y1;
  909. $y1 = $t;
  910. $t = $x2;
  911. $x2 = $y2;
  912. $y2 = $t;
  913. }
  914. $a = ($x2-$x1)/2;
  915. $b = ($y2-$y1)/2;
  916. $cx = ($x1+$x2)/2;
  917. $cy = ($y1+$y2)/2;
  918. $t = $a*$a/sqrt($a*$a+$b*$b);
  919. $i1 = floor($cx-$t);
  920. $i2 = ceil($cx+$t);
  921. for($ix = $i1; $ix <= $i2; $ix++)
  922. {
  923. if( 1-pow(($ix-$cx)/$a, 2)<0 )
  924. {
  925. continue;
  926. }
  927. $y = $b*sqrt(1-pow(($ix-$cx)/$a, 2));
  928. $iy = ceil($cy+$y);
  929. $f = $iy-$cy-$y;
  930. if( !$exch )
  931. {
  932. if(!$half || $iy>$cx) _a_set_pixel($im, $ix, $iy, 1-$f, $fgcolors);
  933. if(!$half || $iy>$cx) _a_set_pixel($im, $ix, $iy-1, $f, $fgcolors);
  934. }
  935. else
  936. {
  937. if(!$half || $ix>$cx) _a_set_pixel($im, $iy, $ix, 1-$f, $fgcolors);
  938. if(!$half || $ix>$cx) _a_set_pixel($im, $iy-1, $ix, $f, $fgcolors);
  939. }
  940. $iy = floor($cy-$y);
  941. $f = $cy-$y-$iy;
  942. if( !$exch )
  943. {
  944. if(!$half || $iy>$cx) _a_set_pixel($im, $ix, $iy+1, $f, $fgcolors);
  945. if(!$half || $iy>$cx) _a_set_pixel($im, $ix, $iy, 1-$f, $fgcolors);
  946. }
  947. else
  948. {
  949. if(!$half || $ix>$cx) _a_set_pixel($im, $iy+1, $ix, $f, $fgcolors);
  950. if(!$half || $ix>$cx) _a_set_pixel($im, $iy, $ix, 1-$f, $fgcolors);
  951. }
  952. }
  953. $t = $b*$b/sqrt($a*$a+$b*$b);
  954. $i1 = ceil($cy-$t);
  955. $i2 = floor($cy+$t);
  956. for($iy = $i1; $iy <= $i2; $iy++)
  957. {
  958. if( 1-pow(($iy-$cy)/$b, 2)<0 )
  959. {
  960. continue;
  961. }
  962. $x = $a*sqrt(1-pow(($iy-$cy)/$b, 2));
  963. $ix = floor($cx-$x);
  964. $f = $cx-$x-$ix;
  965. if( !$exch )
  966. {
  967. if(!$half || $iy>$cx) _a_set_pixel($im, $ix, $iy, 1-$f, $fgcolors);
  968. if(!$half || $iy>$cx) _a_set_pixel($im, $ix+1, $iy, $f, $fgcolors);
  969. }
  970. else
  971. {
  972. if(!$half || $ix>$cx) _a_set_pixel($im, $iy, $ix, 1-$f, $fgcolors);
  973. if(!$half || $ix>$cx) _a_set_pixel($im, $iy, $ix+1, $f, $fgcolors);
  974. }
  975. $ix = ceil($cx+$x);
  976. $f = $ix-$cx-$x;
  977. if( !$exch )
  978. {
  979. if(!$half || $iy>$cx) _a_set_pixel($im, $ix, $iy, 1-$f, $fgcolors);
  980. if(!$half || $iy>$cx) _a_set_pixel($im, $ix-1, $iy, $f, $fgcolors);
  981. }
  982. else
  983. {
  984. if(!$half || $ix>$cx) _a_set_pixel($im, $iy, $ix, 1-$f, $fgcolors);
  985. if(!$half || $ix>$cx) _a_set_pixel($im, $iy, $ix-1, $f, $fgcolors);
  986. }
  987. }
  988. }
  989. function getSafeColors()
  990. {
  991. static $colors = array(
  992. "CCCCCC",
  993. "999999",
  994. "FF0000",
  995. "FF3333",
  996. "CC0000",
  997. "FF6666",
  998. "CC3333",
  999. "990000",
  1000. "FF9999",
  1001. "CC6666",
  1002. "993333",
  1003. "FFCCCC",
  1004. "CC9999",
  1005. "996666",
  1006. "FF3300",
  1007. "FF6633",
  1008. "CC3300",
  1009. "FF9966",
  1010. "CC6633",
  1011. "993300",
  1012. "FF6600",
  1013. "FF9933",
  1014. "CC6600",
  1015. "FFCC99",
  1016. "CC9966",
  1017. "996633",
  1018. "FF9900",
  1019. "FFCC66",
  1020. "CC9933",
  1021. "996600",
  1022. "CC9900",
  1023. "FFCC33",
  1024. "FFCC00",
  1025. "FFFF00",
  1026. "FFFF33",
  1027. "CCCC00",
  1028. "FFFF66",
  1029. "CCCC33",
  1030. "999900",
  1031. "FFFF99",
  1032. "CCCC66",
  1033. "999933",
  1034. "FFFFCC",
  1035. "CCCC99",
  1036. "999966",
  1037. "A2CA00",
  1038. "CCFF33",
  1039. "99CC00",
  1040. "CCFF66",
  1041. "99CC33",
  1042. "669900",
  1043. "99FF00",
  1044. "99FF33",
  1045. "66CC00",
  1046. "73E600",
  1047. "99CC66",
  1048. "669933",
  1049. "66FF00",
  1050. "99FF66",
  1051. "66CC33",
  1052. "339900",
  1053. "66FF33",
  1054. "33CC00",
  1055. "33FF00",
  1056. "00FF00",
  1057. "33FF33",
  1058. "00CC00",
  1059. "66FF66",
  1060. "33CC33",
  1061. "009900",
  1062. "99FF99",
  1063. "66CC66",
  1064. "339933",
  1065. "00AA00",
  1066. "99CC99",
  1067. "669966",
  1068. "00FF33",
  1069. "33FF66",
  1070. "00CC33",
  1071. "66FF99",
  1072. "33CC66",
  1073. "009933",
  1074. "00FF66",
  1075. "33FF99",
  1076. "00CC66",
  1077. "99FFCC",
  1078. "66CC99",
  1079. "339966",
  1080. "00FF99",
  1081. "66FFCC",
  1082. "33CC99",
  1083. "009966",
  1084. "33FFCC",
  1085. "00CC99",
  1086. "00FFCC",
  1087. "00FFFF",
  1088. "33FFFF",
  1089. "00CCCC",
  1090. "66FFFF",
  1091. "33CCCC",
  1092. "009999",
  1093. "99FFFF",
  1094. "66CCCC",
  1095. "339999",
  1096. "CCFFFF",
  1097. "99CCCC",
  1098. "669999",
  1099. "00CCFF",
  1100. "33CCFF",
  1101. "0099CC",
  1102. "66CCFF",
  1103. "3399CC",
  1104. "006699",
  1105. "0099FF",
  1106. "3399FF",
  1107. "0066CC",
  1108. "99CCFF",
  1109. "6699CC",
  1110. "336699",
  1111. "599BFF",
  1112. "6699FF",
  1113. "3366CC",
  1114. "003399",
  1115. "3366FF",
  1116. "0033CC",
  1117. "0033FF",
  1118. "0000FF",
  1119. "3333FF",
  1120. "0000CC",
  1121. "6666FF",
  1122. "3333CC",
  1123. "000099",
  1124. "9999FF",
  1125. "6666CC",
  1126. "333399",
  1127. "CCCCFF",
  1128. "9999CC",
  1129. "666699",
  1130. "0A7BB8",
  1131. "6633FF",
  1132. "3300CC",
  1133. "9966FF",
  1134. "5997D5",
  1135. "330099",
  1136. "6600FF",
  1137. "9933FF",
  1138. "1AA9F7",
  1139. "CC99FF",
  1140. "9966CC",
  1141. "663399",
  1142. "9900FF",
  1143. "CC66FF",
  1144. "CE37CE",
  1145. "660099",
  1146. "CC33FF",
  1147. "9900CC",
  1148. "CC00FF",
  1149. "FF00FF",
  1150. "FF33FF",
  1151. "CC00CC",
  1152. "FF66FF",
  1153. "CC33CC",
  1154. "990099",
  1155. "FF99FF",
  1156. "CC66CC",
  1157. "993399",
  1158. "FFCCFF",
  1159. "CC99CC",
  1160. "996699",
  1161. "FF00CC",
  1162. "FF33CC",
  1163. "CC0099",
  1164. "FF66CC",
  1165. "CC3399",
  1166. "990066",
  1167. "FF0099",
  1168. "FF3399",
  1169. "CC0066",
  1170. "FF99CC",
  1171. "CC6699",
  1172. "993366",
  1173. "FF0066",
  1174. "FF6699",
  1175. "CC3366",
  1176. "990033",
  1177. "FF3366",
  1178. "CC0033",
  1179. "FF0033",
  1180. );
  1181. return $colors;
  1182. }