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