-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.inc.php
More file actions
executable file
·331 lines (304 loc) · 7.86 KB
/
Copy pathcolors.inc.php
File metadata and controls
executable file
·331 lines (304 loc) · 7.86 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* This class can be used to get the most common colors in an image.
* It needs one parameter:
* $image - the filename of the image you want to process.
* Optional parameters:
*
* $count - how many colors should be returned. 0 mmeans all. default=20
* $reduce_brightness - reduce (not eliminate) brightness variants? default=true
* $reduce_gradients - reduce (not eliminate) gradient variants? default=true
* $delta - the amount of gap when quantizing color values.
* Lower values mean more accurate colors. default=16
*
* Author: Csongor Zalatnai
*
* Modified By: Kepler Gelotte - Added the gradient and brightness variation
* reduction routines. Kudos to Csongor for an excellent class. The original
* version can be found at:
*
* http://www.phpclasses.org/browse/package/3370.html
*/
class GetMostCommonColors
{
var $PREVIEW_WIDTH = 150;
var $PREVIEW_HEIGHT = 150;
var $error;
/**
* Returns the colors of the image in an array, ordered in descending order, where the keys are the colors, and the values are the count of the color.
*
* @return array
*/
function Get_Color( $img, $count=20, $reduce_brightness=true, $reduce_gradients=true, $delta=16 )
{
if (is_readable( $img ))
{
if ( $delta > 2 )
{
$half_delta = $delta / 2 - 1;
}
else
{
$half_delta = 0;
}
// WE HAVE TO RESIZE THE IMAGE, BECAUSE WE ONLY NEED THE MOST SIGNIFICANT COLORS.
$size = GetImageSize($img);
$scale = 1;
if ($size[0]>0)
$scale = min($this->PREVIEW_WIDTH/$size[0], $this->PREVIEW_HEIGHT/$size[1]);
if ($scale < 1)
{
$width = floor($scale*$size[0]);
$height = floor($scale*$size[1]);
}
else
{
$width = $size[0];
$height = $size[1];
}
$image_resized = imagecreatetruecolor($width, $height);
if ($size[2] == 1)
$image_orig = imagecreatefromgif($img);
if ($size[2] == 2)
$image_orig = imagecreatefromjpeg($img);
if ($size[2] == 3)
$image_orig = imagecreatefrompng($img);
// WE NEED NEAREST NEIGHBOR RESIZING, BECAUSE IT DOESN'T ALTER THE COLORS
imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
$im = $image_resized;
$imgWidth = imagesx($im);
$imgHeight = imagesy($im);
$total_pixel_count = 0;
for ($y=0; $y < $imgHeight; $y++)
{
for ($x=0; $x < $imgWidth; $x++)
{
$total_pixel_count++;
$index = imagecolorat($im,$x,$y);
$colors = imagecolorsforindex($im,$index);
// ROUND THE COLORS, TO REDUCE THE NUMBER OF DUPLICATE COLORS
if ( $delta > 1 )
{
$colors['red'] = intval((($colors['red'])+$half_delta)/$delta)*$delta;
$colors['green'] = intval((($colors['green'])+$half_delta)/$delta)*$delta;
$colors['blue'] = intval((($colors['blue'])+$half_delta)/$delta)*$delta;
if ($colors['red'] >= 256)
{
$colors['red'] = 255;
}
if ($colors['green'] >= 256)
{
$colors['green'] = 255;
}
if ($colors['blue'] >= 256)
{
$colors['blue'] = 255;
}
}
$hex = substr("0".dechex($colors['red']),-2).substr("0".dechex($colors['green']),-2).substr("0".dechex($colors['blue']),-2);
if ( ! isset( $hexarray[$hex] ) )
{
$hexarray[$hex] = 1;
}
else
{
$hexarray[$hex]++;
}
}
}
// Reduce gradient colors
if ( $reduce_gradients )
{
// if you want to *eliminate* gradient variations use:
// ksort( $hexarray );
arsort( $hexarray, SORT_NUMERIC );
$gradients = array();
foreach ($hexarray as $hex => $num)
{
if ( ! isset($gradients[$hex]) )
{
$new_hex = $this->_find_adjacent( $hex, $gradients, $delta );
$gradients[$hex] = $new_hex;
}
else
{
$new_hex = $gradients[$hex];
}
if ($hex != $new_hex)
{
$hexarray[$hex] = 0;
$hexarray[$new_hex] += $num;
}
}
}
// Reduce brightness variations
if ( $reduce_brightness )
{
// if you want to *eliminate* brightness variations use:
// ksort( $hexarray );
arsort( $hexarray, SORT_NUMERIC );
$brightness = array();
foreach ($hexarray as $hex => $num)
{
if ( ! isset($brightness[$hex]) )
{
$new_hex = $this->_normalize( $hex, $brightness, $delta );
$brightness[$hex] = $new_hex;
}
else
{
$new_hex = $brightness[$hex];
}
if ($hex != $new_hex)
{
$hexarray[$hex] = 0;
$hexarray[$new_hex] += $num;
}
}
}
arsort( $hexarray, SORT_NUMERIC );
// convert counts to percentages
foreach ($hexarray as $key => $value)
{
$hexarray[$key] = (float)$value / $total_pixel_count;
}
if ( $count > 0 )
{
// only works in PHP5
// return array_slice( $hexarray, 0, $count, true );
$arr = array();
foreach ($hexarray as $key => $value)
{
if ($count == 0)
{
break;
}
$count--;
$arr[$key] = $value;
}
return $arr;
}
else
{
return $hexarray;
}
}
else
{
$this->error = "Image ".$img." does not exist or is unreadable";
return false;
}
}
function _normalize( $hex, $hexarray, $delta )
{
$lowest = 255;
$highest = 0;
$colors['red'] = hexdec( substr( $hex, 0, 2 ) );
$colors['green'] = hexdec( substr( $hex, 2, 2 ) );
$colors['blue'] = hexdec( substr( $hex, 4, 2 ) );
if ($colors['red'] < $lowest)
{
$lowest = $colors['red'];
}
if ($colors['green'] < $lowest )
{
$lowest = $colors['green'];
}
if ($colors['blue'] < $lowest )
{
$lowest = $colors['blue'];
}
if ($colors['red'] > $highest)
{
$highest = $colors['red'];
}
if ($colors['green'] > $highest )
{
$highest = $colors['green'];
}
if ($colors['blue'] > $highest )
{
$highest = $colors['blue'];
}
// Do not normalize white, black, or shades of grey unless low delta
if ( $lowest == $highest )
{
if ($delta <= 32)
{
if ( $lowest == 0 || $highest >= (255 - $delta) )
{
return $hex;
}
}
else
{
return $hex;
}
}
for (; $highest < 256; $lowest += $delta, $highest += $delta)
{
$new_hex = substr("0".dechex($colors['red'] - $lowest),-2).substr("0".dechex($colors['green'] - $lowest),-2).substr("0".dechex($colors['blue'] - $lowest),-2);
if ( isset( $hexarray[$new_hex] ) )
{
// same color, different brightness - use it instead
return $new_hex;
}
}
return $hex;
}
function _find_adjacent( $hex, $gradients, $delta )
{
$red = hexdec( substr( $hex, 0, 2 ) );
$green = hexdec( substr( $hex, 2, 2 ) );
$blue = hexdec( substr( $hex, 4, 2 ) );
if ($red > $delta)
{
$new_hex = substr("0".dechex($red - $delta),-2).substr("0".dechex($green),-2).substr("0".dechex($blue),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($green > $delta)
{
$new_hex = substr("0".dechex($red),-2).substr("0".dechex($green - $delta),-2).substr("0".dechex($blue),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($blue > $delta)
{
$new_hex = substr("0".dechex($red),-2).substr("0".dechex($green),-2).substr("0".dechex($blue - $delta),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($red < (255 - $delta))
{
$new_hex = substr("0".dechex($red + $delta),-2).substr("0".dechex($green),-2).substr("0".dechex($blue),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($green < (255 - $delta))
{
$new_hex = substr("0".dechex($red),-2).substr("0".dechex($green + $delta),-2).substr("0".dechex($blue),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
if ($blue < (255 - $delta))
{
$new_hex = substr("0".dechex($red),-2).substr("0".dechex($green),-2).substr("0".dechex($blue + $delta),-2);
if ( isset($gradients[$new_hex]) )
{
return $gradients[$new_hex];
}
}
return $hex;
}
}