PageRenderTime 31ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/src/drupal_custom/theme/ibp/icon.php

https://github.com/strandls/augmentedmaps
PHP | 112 lines | 61 code | 18 blank | 33 comment | 10 complexity | c6313cb0e3420ea9fb5341fcd45ce780 MD5 | raw file
  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. /**
  4. * Serves out required icon in the required color and border
  5. **/
  6. $step = 50;
  7. $iconDirectoryPath = "images/icons/";
  8. $existingFill = hex2int("00ff00");
  9. $existingDarkBorder = hex2int("474747");
  10. $existingLightBorder = hex2int("B4B4B4");
  11. $fill = hex2int("53E26E");
  12. $lightBorder = hex2int("B4B4B4");
  13. $darkBorder = hex2int("474747");
  14. $themeName = "";
  15. $layerName = "";
  16. $iconName = "proto.png";
  17. if($_GET["theme"] != "") {
  18. $iconName = $_GET["theme"] . ".png";
  19. }
  20. elseif($_GET["layer"] != "") {
  21. $iconName = $_GET["layer"] . ".png";
  22. }
  23. elseif($_GET["icon"] != "") {
  24. $iconName = $_GET["icon"] . ".png";
  25. }
  26. if(! file_exists($iconDirectoryPath . $iconName)) {
  27. $iconName = "proto.png";
  28. }
  29. if($_GET["fill"] != "") {
  30. $fill = hex2int($_GET["fill"]);
  31. }
  32. // Compute the dark and light shades
  33. /*$darkBorder['r'] = $fill['r'] - $step;
  34. if($darkBorderBorder['r'] < 0){
  35. $darkBorder['r'] = 0;
  36. }
  37. $darkBorder['g'] = $fill['g'] - $step;
  38. if($darkBorderBorder['g'] < 0){
  39. $darkBorder['g'] = 0;
  40. }
  41. $darkBorder['b'] = $fill['b'] - $step;
  42. if($darkBorderBorder['b'] < 0){
  43. $darkBorder['b'] = 0;
  44. }*/
  45. $lightBorder['r'] = $fill['r'] + $step;
  46. if($lightBorder['r'] > 255){
  47. $lightBorder['r'] = 255;
  48. }
  49. $lightBorder['g'] = $fill['g'] + $step;
  50. if($lightBorder['g'] > 255){
  51. $lightBorder['g'] = 255;
  52. }
  53. $lightBorder['b'] = $fill['b'] + $step;
  54. if($lightBorder['b'] > 255){
  55. $lightBorder['b'] = 255;
  56. }
  57. // For now, all borders are dark
  58. //$lightBorder = "000000";
  59. $darkBorder = "000000";
  60. $image = imageCreateFromPNG($iconDirectoryPath . $iconName);
  61. $phpFillIndex = imageColorClosest($image,$existingFill['r'],$existingFill['g'],$existingFill['b']);
  62. imageColorSet($image,$phpFillIndex,$fill['r'],$fill['g'],$fill['b']);
  63. $phpDarkBorderIndex = imageColorClosest($image,$existingDarkBorder['r'],$existingDarkBorder['g'],$existingDarkBorder['b']);
  64. imageColorSet($image,$phpDarkBorderIndex,$darkBorder['r'],$darkBorder['g'],$darkBorder['b']);
  65. $phpLightBorderIndex = imageColorClosest($image,$existingLightBorder['r'],$existingLightBorder['g'],$existingLightBorder['b']);
  66. imageColorSet($image,$phpLightBorderIndex,$lightBorder['r'],$lightBorder['g'],$lightBorder['b']);
  67. header('Content-type: image/png');
  68. imagePNG($image);
  69. imageDestroy($image);
  70. /**
  71. * @param $hex string 6-digit hexadecimal color
  72. * @return array 3 elements 'r', 'g', & 'b' = int color values
  73. * @desc Converts a 6 digit hexadecimal number into an array of
  74. * 3 integer values ('r' => red value, 'g' => green, 'b' => blue)
  75. */
  76. function hex2int($hex) {
  77. return array( 'r' => hexdec(substr($hex, 0, 2)), // 1st pair of digits
  78. 'g' => hexdec(substr($hex, 2, 2)), // 2nd pair
  79. 'b' => hexdec(substr($hex, 4, 2)) // 3rd pair
  80. );
  81. }
  82. /**
  83. * @param $input string 6-digit hexadecimal string to be validated
  84. * @param $default string default color to be returned if $input isn't valid
  85. * @return string the validated 6-digit hexadecimal color
  86. * @desc returns $input if it is a valid hexadecimal color,
  87. * otherwise returns $default (which defaults to black)
  88. */
  89. function validHexColor($input = '000000', $default = '000000') {
  90. // A valid Hexadecimal color is exactly 6 characters long
  91. // and eigher a digit or letter from a to f
  92. return (eregi('^[0-9a-f]{6}$', $input)) ? $input : $default ;
  93. }
  94. ?>