pagetools-build.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // phpcs:ignoreFile -- deprecated and will be removed
  3. /**
  4. * This script generates a sprite from the unprocessed pagetool icons by combining them
  5. * and overlaying a color layer for the active state.
  6. *
  7. * This script requires a current libGD to be available.
  8. *
  9. * The color for the active state is read from the style.ini's __link__ replacement
  10. *
  11. * The final sprite is optimized with optipng if available.
  12. *
  13. * @author Andreas Gohr <andi@splitbrain.org>
  14. * @deprecated 2018-06-15 we no longer use PNG based icons
  15. * @todo Maybe add some more error checking
  16. */
  17. $GAMMA = 0.8;
  18. $OPTIPNG = '/usr/bin/optipng';
  19. if('cli' != php_sapi_name()) die('please run from commandline');
  20. // load input images
  21. $input = glob('pagetools/*.png');
  22. sort($input);
  23. $cnt = count($input);
  24. if(!$cnt){
  25. die("No input images found. This script needs to be called from within the image directory!\n");
  26. }
  27. // create destination image
  28. $DST = imagecreatetruecolor(30,$cnt*45*2);
  29. imagesavealpha($DST, true);
  30. $C_trans = imagecolorallocatealpha($DST, 0, 0, 0, 127);
  31. imagefill($DST, 0, 0, $C_trans);
  32. // load highlight color from style.ini
  33. $ini = parse_ini_file('../style.ini',true);
  34. $COLOR = hex2rgb($ini['replacements']['__link__']);
  35. $C_active = imagecolorallocate($DST, $COLOR['r'],$COLOR['g'],$COLOR['b']);
  36. // add all the icons to the sprite image
  37. for($i=0; $i<$cnt; $i++){
  38. $base = $i*90;
  39. $IN = imagecreatefrompng($input[$i]);
  40. imagesavealpha($IN, true);
  41. imagecolorscale($IN,$GAMMA);
  42. imagecopy($DST,$IN, 0,$base, 0,0, 30,30);
  43. imagedestroy($IN);
  44. $IN = imagecreatefrompng($input[$i]);
  45. imagesavealpha($IN, true);
  46. imagecolorscale($IN,$GAMMA);
  47. imagecopy($DST,$IN, 0,$base+45, 0,0, 30,30);
  48. imagedestroy($IN);
  49. imagelayereffect($DST, IMG_EFFECT_OVERLAY);
  50. imagefilledrectangle($DST, 0,$base+45, 30,$base+45+30, $C_active);
  51. imagelayereffect($DST, IMG_EFFECT_NORMAL);
  52. }
  53. // output sprite
  54. imagepng($DST,'pagetools-sprite.png');
  55. imagedestroy($DST);
  56. // optimize if possible
  57. if(is_executable($OPTIPNG)){
  58. system("$OPTIPNG -o5 'pagetools-sprite.png'");
  59. }
  60. /**
  61. * Convert a hex color code to an rgb array
  62. */
  63. function hex2rgb($hex) {
  64. // strip hash
  65. $hex = str_replace('#', '', $hex);
  66. // normalize short codes
  67. if(strlen($hex) == 3){
  68. $hex = substr($hex,0,1).
  69. substr($hex,0,1).
  70. substr($hex,1,1).
  71. substr($hex,1,1).
  72. substr($hex,2,1).
  73. substr($hex,2,1);
  74. }
  75. // calc rgb
  76. return array(
  77. 'r' => hexdec(substr($hex, 0, 2)),
  78. 'g' => hexdec(substr($hex, 2, 2)),
  79. 'b' => hexdec(substr($hex, 4, 2))
  80. );
  81. }
  82. /**
  83. * Scale (darken/lighten) a given image
  84. *
  85. * @param resource $img The truetype GD image to work on
  86. * @param float $scale Scale the colors by this value ( <1 darkens, >1 lightens)
  87. */
  88. function imagecolorscale(&$img, $scale){
  89. $w = imagesx($img);
  90. $h = imagesy($img);
  91. imagealphablending($img, false);
  92. for($x = 0; $x < $w; $x++){
  93. for($y = 0; $y < $h; $y++){
  94. $rgba = imagecolorat($img, $x, $y);
  95. $a = ($rgba >> 24) & 0xFF;
  96. $r = ($rgba >> 16) & 0xFF;
  97. $g = ($rgba >> 8) & 0xFF;
  98. $b = $rgba & 0xFF;
  99. $r = max(min(round($r*$scale),255),0);
  100. $g = max(min(round($g*$scale),255),0);
  101. $b = max(min(round($b*$scale),255),0);
  102. $color = imagecolorallocatealpha($img, $r, $g, $b, $a);
  103. imagesetpixel($img, $x, $y, $color);
  104. }
  105. }
  106. imagealphablending($img, true);
  107. }