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
20if('cli' != PHP_SAPI) die('please run from commandline');
21
22// load input images
23$input = glob('pagetools/*.png');
24sort($input);
25$cnt   = count($input);
26if(!$cnt){
27    die("No input images found. This script needs to be called from within the image directory!\n");
28}
29
30// create destination image
31$DST = imagecreatetruecolor(30,$cnt*45*2);
32imagesavealpha($DST, true);
33$C_trans = imagecolorallocatealpha($DST, 0, 0, 0, 127);
34imagefill($DST, 0, 0, $C_trans);
35
36// load highlight color from style.ini
37$ini = parse_ini_file('../style.ini',true);
38$COLOR = hex2rgb($ini['replacements']['__link__']);
39$C_active = imagecolorallocate($DST, $COLOR['r'],$COLOR['g'],$COLOR['b']);
40
41// add all the icons to the sprite image
42for($i=0; $i<$cnt; $i++){
43    $base = $i*90;
44
45    $IN = imagecreatefrompng($input[$i]);
46    imagesavealpha($IN, true);
47    imagecolorscale($IN,$GAMMA);
48    imagecopy($DST,$IN, 0,$base, 0,0, 30,30);
49    imagedestroy($IN);
50
51    $IN = imagecreatefrompng($input[$i]);
52    imagesavealpha($IN, true);
53    imagecolorscale($IN,$GAMMA);
54    imagecopy($DST,$IN, 0,$base+45, 0,0, 30,30);
55    imagedestroy($IN);
56
57    imagelayereffect($DST, IMG_EFFECT_OVERLAY);
58    imagefilledrectangle($DST, 0,$base+45, 30,$base+45+30, $C_active);
59    imagelayereffect($DST, IMG_EFFECT_NORMAL);
60}
61
62// output sprite
63imagepng($DST,'pagetools-sprite.png');
64imagedestroy($DST);
65
66// optimize if possible
67if(is_executable($OPTIPNG)){
68    system("$OPTIPNG -o5 'pagetools-sprite.png'");
69}
70
71/**
72 * Convert a hex color code to an rgb array
73 */
74function hex2rgb($hex) {
75    // strip hash
76    $hex = str_replace('#', '', $hex);
77
78    // normalize short codes
79    if(strlen($hex) == 3){
80        $hex = substr($hex,0,1).
81               substr($hex,0,1).
82               substr($hex,1,1).
83               substr($hex,1,1).
84               substr($hex,2,1).
85               substr($hex,2,1);
86    }
87
88    // calc rgb
89    return ['r' => hexdec(substr($hex, 0, 2)), 'g' => hexdec(substr($hex, 2, 2)), 'b' => hexdec(substr($hex, 4, 2))];
90}
91
92/**
93 * Scale (darken/lighten) a given image
94 *
95 * @param resource $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