-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogo.php
221 lines (198 loc) · 8.34 KB
/
logo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
$id = isset($_GET['id']) ? $_GET['id'] : 0;
// if seed is not set in the url use a random one!
if(!$id) mt_srand();
else mt_srand($id);
// get pixel size (resolution multiplier) default is x1
$pixel_size = isset($_GET['ps']) ? $_GET['ps'] : 1;
// logo size
$width = 120;
$height = 40;
// create the "canvas"
$img = imagecreatetruecolor($width, $height);
// pattern design will have pixel size x1 or x2
$mod_size = !mt_rand(0, 4) ? 2 : 1; // one on four can have mod_size = 2
$col_min = 90;// min color value
$col_max = 240;// max color value
// generate background color
$col_back_R = mt_rand($col_min, $col_max);
$col_back_G = mt_rand($col_min, $col_max);
$col_back_B = mt_rand($col_min, $col_max);
$color_background = imagecolorallocate($img, $col_back_R, $col_back_G, $col_back_B);
// generate foreground color
$col_fore_R = mt_rand($col_min, $col_max);
$col_fore_G = mt_rand($col_min, $col_max);
$col_fore_B = mt_rand($col_min, $col_max);
$color_foreground[0] = imagecolorallocate($img, $col_fore_R, $col_fore_G, $col_fore_B);
$foreground_colors = mt_rand(1, 2);
if($foreground_colors == 2){
// generate third color
$col_third_R = mt_rand($col_min, $col_max);
$col_third_G = mt_rand($col_min, $col_max);
$col_third_B = mt_rand($col_min, $col_max);
$color_foreground[1] = imagecolorallocate($img, $col_third_R, $col_third_G, $col_third_B);
}
// generate eyes color
$col_eyes_R = min(255, 255 + $col_min - (($col_back_R + $col_fore_R) / 2));
$col_eyes_G = min(255, 255 + $col_min - (($col_back_G + $col_fore_G) / 2));
$col_eyes_B = min(255, 255 + $col_min - (($col_back_B + $col_fore_B) / 2));
$color_eyes = imagecolorallocate($img, $col_eyes_R, $col_eyes_G, $col_eyes_B);
// calculate eyes shadow color
$eyes_shadow = 160;
$color_eyes_shadow = imagecolorallocate($img,
max(0, $col_eyes_R - $eyes_shadow),
max(0, $col_eyes_G - $eyes_shadow),
max(0, $col_eyes_B - $eyes_shadow)
);
// generate outline glow color
$tmp_min = $col_min;
$col_outglow_R = mt_rand($col_min, $col_max);
if($col_outglow_R < $tmp_min*1.5) $tmp_min *= 1.5;
$col_outglow_G = mt_rand($tmp_min, $col_max);
if($col_outglow_G < $tmp_min*1.5) $tmp_min *= 1.5;
$col_outglow_B = mt_rand($tmp_min, $col_max);
$outline_glow_color = imagecolorallocatealpha($img, $col_outglow_R, $col_outglow_G, $col_outglow_B, 70);
// backround color
imagefilledrectangle($img, 0, 0, $width, $height, $color_background);
imagecolortransparent($img, $color_background); // toggle transparent background
$eyes = array(
// left eye
0 => array(11,13),
1 => array(12,13),
2 => array(11,14),
3 => array(12,14),
// right eye
4 => array(19,13),
5 => array(20,13),
6 => array(19,14),
7 => array(20,14),
);
$eyes_shadow = array(
// left eye
0 => array(11,12),
1 => array(12,12),
2 => array(11,15),
3 => array(12,15),
4 => array(10,13),
5 => array(10,14),
6 => array(13,13),
7 => array(13,14),
// right eye
8 => array(19,12),
9 => array(20,12),
10 => array(19,15),
11 => array(20,15),
12 => array(18,13),
13 => array(18,14),
14 => array(21,13),
15 => array(21,14),
);
// pattern matrix, max: 6*8 (x*y)
$pattern_matrix = array(
0 => array(0,0,0,0,0,0),
1 => array(0,0,0,0,0,0),
2 => array(0,0,0,0,0,0),
3 => array(0,0,0,0,0,0),
4 => array(0,0,0,0,0,0),
5 => array(0,0,0,0,0,0),
6 => array(0,0,0,0,0,0),
7 => array(0,0,0,0,0,0),
);
// set pattern matrix size and properties
$mat_w = mt_rand(3, 8);
$mat_h = mt_rand(3, 6);
$mirror_horizontal = mt_rand(0, 1);
$mirror_vertical = mt_rand(0, 1);
$pixel_mirrored = false;
// generate the pattern matrix
for( $x = 0; $x < $mat_w; $x++ )
{
for( $y = 0; $y < $mat_h; $y++ )
{
$pixel_mirrored = false;
if( $mirror_horizontal && $y > $mat_h / 2 )
{
$pixel_mirrored = true;
$pattern_matrix[$x][$y] = $pattern_matrix[$x][$mat_h - $y];
}
if( $mirror_vertical && $x > $mat_w / 2 )
{
$pixel_mirrored = true;
$pattern_matrix[$x][$y] = $pattern_matrix[$mat_w - $x][$y];
}
$rand_col = mt_rand(0, $foreground_colors);
if( !$pixel_mirrored && $rand_col > 0)
{
$pattern_matrix[$x][$y] = $rand_col;
}
}
}
// load mask image
$mask = imagecreatefrompng('img/mask.png');
// load shades image
$border = imagecreatefrompng('img/greyscale_borders.png');
// load outline glow image
$outline_glow = imagecreatefrompng('img/outline_glow.png');
// TODO: hardcode the mask and the shades inside an array (like the eyes one)
// create masked image
$img_masked = imagecreatetruecolor($width, $height);
// make $img_masked a transparent image
// turning off alpha blending (to ensure alpha channel information is preserved, rather than removed (blending with the rest of the image in the form of black))
imagealphablending($img_masked, false);
// turning on alpha channel information saving (to ensure the full range of transparency is preserved)
imagesavealpha($img_masked, true);
// transparent background (will be removed after line 185)
$transparent_background = imagecolorallocatealpha( $img_masked, 1, 2, 3, 127 );
imagefilledrectangle($img_masked, 0, 0, $width, $height, $transparent_background);
// apply mask
for( $x = 0; $x < $width; $x++ )
{
for( $y = 0; $y < $height; $y++ )
{
// use mask to choose wich pixels to write on the final image
if(imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) )['alpha'] == 0)
{
// apply mask: using bg or fore colors
$pattern_value = $pattern_matrix[($x/$mod_size)%$mat_w][($y/$mod_size)%$mat_h];
if($pattern_value > 0) imagesetpixel( $img_masked, $x, $y, $color_foreground[$pattern_value - 1]);
else imagesetpixel( $img_masked, $x, $y, imagecolorat( $img, $x, $y ) );
// apply shades
$color_masked = imagecolorsforindex( $img_masked, imagecolorat( $img_masked, $x, $y ) );
$color_border = imagecolorsforindex( $border, imagecolorat( $border, $x, $y ) );
$alphaz = (255 - $color_border['red']);
imagesetpixel( $img_masked, $x, $y, imagecolorallocate(
$img_masked,
max(0, $color_masked[ 'red' ] - $alphaz),
max(0, $color_masked[ 'green' ] - $alphaz),
max(0, $color_masked[ 'blue' ] - $alphaz)
) );
}
else if(imagecolorsforindex( $outline_glow, imagecolorat( $outline_glow, $x, $y ) )['alpha'] == 0)
{
// draw outline glow
imagesetpixel( $img_masked, $x, $y, $outline_glow_color);
}
else
{
// removed pixels outside the mask
imagesetpixel( $img_masked, $x, $y, $transparent_background);
}
}
}
// draw the eyes on the masked image
for( $e = 0; $e < count($eyes); $e++ ) imagesetpixel( $img_masked, $eyes[$e][0], $eyes[$e][1], $color_eyes);
// draw the shadow around the eyes
for( $e = 0; $e < count($eyes_shadow); $e++ ) imagesetpixel( $img_masked, $eyes_shadow[$e][0], $eyes_shadow[$e][1], $color_eyes_shadow);
// apply resolution multiplier (pixel size)
$img_masked = imagescale($img_masked, $width * $pixel_size, $height * $pixel_size, IMG_NEAREST_NEIGHBOUR);
// removing the black from the placeholder
imagecolortransparent($img_masked, $transparent_background);
// set content header: this file will not load like a normal html page, it will output a png file
header("Content-type: image/png");
// output image
imagepng($img_masked);
// destroy images
imagedestroy($img);
imagedestroy($mask);
imagedestroy($border);
imagedestroy($img_masked);