1 | <?php |
---|
2 | /***************************************************************************\ |
---|
3 | * SPIP, Systeme de publication pour l'internet * |
---|
4 | * * |
---|
5 | * Copyright (c) 2001-2015 * |
---|
6 | * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James * |
---|
7 | * * |
---|
8 | * Ce programme est un logiciel libre distribue sous licence GNU/GPL. * |
---|
9 | * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. * |
---|
10 | \***************************************************************************/ |
---|
11 | |
---|
12 | if (!defined('_ECRIRE_INC_VERSION')) return; |
---|
13 | |
---|
14 | // librairie de base du core |
---|
15 | include_spip('inc/filtres_images_lib_mini'); |
---|
16 | |
---|
17 | function multiple_de_trois($val) { |
---|
18 | return intval(round($val / 3) * 3); |
---|
19 | } |
---|
20 | |
---|
21 | /** |
---|
22 | * Transformation d'une couleur vectorielle RGB en HSV |
---|
23 | * RGB entiers entre 0 et 255 |
---|
24 | * HSV float entre 0 et 1 |
---|
25 | * |
---|
26 | * @param int $R |
---|
27 | * @param int $G |
---|
28 | * @param int $B |
---|
29 | * @return array |
---|
30 | */ |
---|
31 | function _couleur_rgb2hsv ($R, $G, $B) { |
---|
32 | $var_R = ( $R / 255 ) ; //Where RGB values = 0 ÷ 255 |
---|
33 | $var_G = ( $G / 255 ); |
---|
34 | $var_B = ( $B / 255 ); |
---|
35 | |
---|
36 | $var_Min = min( $var_R, $var_G, $var_B ) ; //Min. value of RGB |
---|
37 | $var_Max = max( $var_R, $var_G, $var_B ) ; //Max. value of RGB |
---|
38 | $del_Max = $var_Max - $var_Min ; //Delta RGB value |
---|
39 | |
---|
40 | $V = $var_Max; |
---|
41 | $L = ( $var_Max + $var_Min ) / 2; |
---|
42 | |
---|
43 | if ( $del_Max == 0 ) //This is a gray, no chroma... |
---|
44 | { |
---|
45 | $H = 0 ; //HSL results = 0 ÷ 1 |
---|
46 | $S = 0 ; |
---|
47 | } |
---|
48 | else //Chromatic data... |
---|
49 | { |
---|
50 | $S = $del_Max / $var_Max; |
---|
51 | |
---|
52 | $del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
53 | $del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
54 | $del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
55 | |
---|
56 | if ( $var_R == $var_Max ) $H = $del_B - $del_G; |
---|
57 | else if ( $var_G == $var_Max ) $H = ( 1 / 3 ) + $del_R - $del_B; |
---|
58 | else if ( $var_B == $var_Max ) $H = ( 2 / 3 ) + $del_G - $del_R; |
---|
59 | |
---|
60 | if ( $H < 0 ) $H = $H + 1; |
---|
61 | if ( $H > 1 ) $H = $H - 1; |
---|
62 | } |
---|
63 | |
---|
64 | $ret["h"] = $H; |
---|
65 | $ret["s"] = $S; |
---|
66 | $ret["v"] = $V; |
---|
67 | |
---|
68 | return $ret; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Transformation d'une couleur vectorielle HSV en RGB |
---|
73 | * HSV float entre 0 et 1 |
---|
74 | * RGB entiers entre 0 et 255 |
---|
75 | * |
---|
76 | * @param float $H |
---|
77 | * @param float $S |
---|
78 | * @param float $V |
---|
79 | * @return array |
---|
80 | */ |
---|
81 | function _couleur_hsv2rgb ($H, $S, $V) { |
---|
82 | |
---|
83 | if ( $S == 0 ) //HSV values = 0 ÷ 1 |
---|
84 | { |
---|
85 | $R = $V * 255; |
---|
86 | $G = $V * 255; |
---|
87 | $B = $V * 255; |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | $var_h = $H * 6; |
---|
92 | if ( $var_h == 6 ) $var_h = 0 ; //H must be < 1 |
---|
93 | $var_i = floor( $var_h ) ; //Or ... var_i = floor( var_h ) |
---|
94 | $var_1 = $V * ( 1 - $S ); |
---|
95 | $var_2 = $V * ( 1 - $S * ( $var_h - $var_i ) ); |
---|
96 | $var_3 = $V * ( 1 - $S * ( 1 - ( $var_h - $var_i ) ) ); |
---|
97 | |
---|
98 | |
---|
99 | if ( $var_i == 0 ) { $var_r = $V ; $var_g = $var_3 ; $var_b = $var_1 ; } |
---|
100 | else if ( $var_i == 1 ) { $var_r = $var_2 ; $var_g = $V ; $var_b = $var_1 ; } |
---|
101 | else if ( $var_i == 2 ) { $var_r = $var_1 ; $var_g = $V ; $var_b = $var_3 ; } |
---|
102 | else if ( $var_i == 3 ) { $var_r = $var_1 ; $var_g = $var_2 ; $var_b = $V ; } |
---|
103 | else if ( $var_i == 4 ) { $var_r = $var_3 ; $var_g = $var_1 ; $var_b = $V ; } |
---|
104 | else { $var_r = $V ; $var_g = $var_1 ; $var_b = $var_2; } |
---|
105 | |
---|
106 | $R = $var_r * 255; //RGB results = 0 ÷ 255 |
---|
107 | $G = $var_g * 255; |
---|
108 | $B = $var_b * 255; |
---|
109 | } |
---|
110 | $ret["r"] = floor($R); |
---|
111 | $ret["g"] = floor($G); |
---|
112 | $ret["b"] = floor($B); |
---|
113 | |
---|
114 | return $ret; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | /** |
---|
119 | * Transformation d'une couleur RGB en HSL |
---|
120 | * HSL float entre 0 et 1 |
---|
121 | * RGB entiers entre 0 et 255 |
---|
122 | * |
---|
123 | * @param int $R |
---|
124 | * @param int $G |
---|
125 | * @param int $B |
---|
126 | * @return array |
---|
127 | */ |
---|
128 | function _couleur_rgb2hsl ($R, $G, $B) { |
---|
129 | $var_R = ( $R / 255 ) ; //Where RGB values = 0 ÷ 255 |
---|
130 | $var_G = ( $G / 255 ); |
---|
131 | $var_B = ( $B / 255 ); |
---|
132 | |
---|
133 | $var_Min = min( $var_R, $var_G, $var_B ) ; //Min. value of RGB |
---|
134 | $var_Max = max( $var_R, $var_G, $var_B ) ; //Max. value of RGB |
---|
135 | $del_Max = $var_Max - $var_Min ; //Delta RGB value |
---|
136 | |
---|
137 | $L = ( $var_Max + $var_Min ) / 2; |
---|
138 | |
---|
139 | if ( $del_Max == 0 ) //This is a gray, no chroma... |
---|
140 | { |
---|
141 | $H = 0 ; //HSL results = 0 ÷ 1 |
---|
142 | $S = 0 ; |
---|
143 | } |
---|
144 | else //Chromatic data... |
---|
145 | { |
---|
146 | if ($L < 0.5 ) $S = $del_Max / ( $var_Max+ $var_Min); |
---|
147 | else $S = $del_Max/ ( 2 - $var_Max - $var_Min); |
---|
148 | |
---|
149 | $del_R = ( ( ( $var_Max- $var_R) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
150 | $del_G = ( ( ( $var_Max- $var_G) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
151 | $del_B = ( ( ( $var_Max- $var_B) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
152 | |
---|
153 | if ( $var_R == $var_Max) $H= $del_B - $del_G; |
---|
154 | else if ( $var_G == $var_Max) $H= ( 1 / 3 ) + $del_R - $del_B; |
---|
155 | else if ( $var_B == $var_Max) $H= ( 2 / 3 ) + $del_G - $del_R; |
---|
156 | |
---|
157 | if ( $H < 0 ) $H+= 1; |
---|
158 | if ( $H > 1 ) $H-= 1; |
---|
159 | } |
---|
160 | |
---|
161 | $ret["h"] = $H; |
---|
162 | $ret["s"] = $S; |
---|
163 | $ret["l"] = $L; |
---|
164 | |
---|
165 | return $ret; |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * Calcul d'une composante R, G ou B |
---|
170 | * |
---|
171 | * @param unknown_type $v1 |
---|
172 | * @param unknown_type $v2 |
---|
173 | * @param unknown_type $vH |
---|
174 | * @return float |
---|
175 | */ |
---|
176 | function hue_2_rgb( $v1, $v2, $vH ) { |
---|
177 | if ( $vH < 0 ) $vH += 1; |
---|
178 | if ( $vH > 1 ) $vH -= 1; |
---|
179 | if ( ( 6 * $vH ) < 1 ) return ( $v1 + ( $v2 - $v1 ) * 6 * $vH ); |
---|
180 | if ( ( 2 * $vH ) < 1 ) return ( $v2 ); |
---|
181 | if ( ( 3 * $vH ) < 2 ) return ( $v1 + ( $v2 - $v1 ) * ( ( 2 / 3 ) - $vH ) * 6 ); |
---|
182 | return ( $v1 ); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | /** |
---|
187 | * Transformation d'une couleur HSL en RGB |
---|
188 | * HSL float entre 0 et 1 |
---|
189 | * RGB entiers entre 0 et 255 |
---|
190 | * |
---|
191 | * @param float $H |
---|
192 | * @param float $S |
---|
193 | * @param float $L |
---|
194 | * @return array |
---|
195 | */ |
---|
196 | function _couleur_hsl2rgb ($H, $S, $L) { |
---|
197 | |
---|
198 | if ( $S == 0 ) //HSV values = 0 -> 1 |
---|
199 | { |
---|
200 | $R = $L * 255; |
---|
201 | $G = $L * 255; |
---|
202 | $B = $L * 255; |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | if ( $L < 0.5 ) $var_2 = $L * ( 1 + $S ); |
---|
207 | else $var_2 = ( $L + $S ) - ( $S * $L ); |
---|
208 | |
---|
209 | $var_1 = 2 * $L - $var_2; |
---|
210 | |
---|
211 | $R = 255 * hue_2_rgb( $var_1, $var_2, $H + ( 1 / 3 ) ) ; |
---|
212 | $G = 255 * hue_2_rgb( $var_1, $var_2, $H ); |
---|
213 | $B = 255 * hue_2_rgb( $var_1, $var_2, $H - ( 1 / 3 ) ); |
---|
214 | } |
---|
215 | $ret["r"] = floor($R); |
---|
216 | $ret["g"] = floor($G); |
---|
217 | $ret["b"] = floor($B); |
---|
218 | |
---|
219 | return $ret; |
---|
220 | } |
---|
221 | |
---|
222 | // A partir d'une image, |
---|
223 | // recupere une couleur |
---|
224 | // renvoit sous la forme hexadecimale ("F26C4E" par exemple). |
---|
225 | // Par defaut, la couleur choisie se trouve un peu au-dessus du centre de l'image. |
---|
226 | // On peut forcer un point en fixant $x et $y, entre 0 et 20. |
---|
227 | // http://code.spip.net/@image_couleur_extraire |
---|
228 | |
---|
229 | function _image_couleur_extraire($img, $x = 10, $y = 6) { |
---|
230 | static $couleur_extraite = array(); |
---|
231 | |
---|
232 | if (isset($couleur_extraite["$img-$x-$y"])) |
---|
233 | return $couleur_extraite["$img-$x-$y"]; |
---|
234 | |
---|
235 | // valeur par defaut si l'image ne peut etre lue |
---|
236 | $defaut = "F26C4E"; |
---|
237 | |
---|
238 | $cache = _image_valeurs_trans($img, "coul-$x-$y", "txt"); |
---|
239 | if (!$cache) |
---|
240 | return $couleur_extraite["$img-$x-$y"] = $defaut; |
---|
241 | |
---|
242 | |
---|
243 | $fichier = $cache["fichier"]; |
---|
244 | $dest = $cache["fichier_dest"]; |
---|
245 | |
---|
246 | if (isset($couleur_extraite["$fichier-$x-$y"])) |
---|
247 | return $couleur_extraite["$fichier-$x-$y"]; |
---|
248 | |
---|
249 | $creer = $cache["creer"]; |
---|
250 | |
---|
251 | if ($creer) { |
---|
252 | if (@file_exists($fichier)) { |
---|
253 | $width = $cache["largeur"]; |
---|
254 | $height = $cache["hauteur"]; |
---|
255 | |
---|
256 | $newwidth = 20; |
---|
257 | $newheight = 20; |
---|
258 | |
---|
259 | $thumb = imagecreate($newwidth, $newheight); |
---|
260 | |
---|
261 | $source = $cache["fonction_imagecreatefrom"]($fichier); |
---|
262 | |
---|
263 | imagepalettetotruecolor($source); |
---|
264 | |
---|
265 | imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); |
---|
266 | |
---|
267 | do { |
---|
268 | // get a color |
---|
269 | $color_index = imagecolorat($thumb, $x, $y); |
---|
270 | |
---|
271 | // make it human readable |
---|
272 | $color_tran = imagecolorsforindex($thumb, $color_index); |
---|
273 | $x++; $y++; |
---|
274 | } while ($color_tran['alpha']==127 AND $x<$newwidth AND $y<$newheight); |
---|
275 | |
---|
276 | $couleur = _couleur_dec_to_hex($color_tran["red"], $color_tran["green"], $color_tran["blue"]); |
---|
277 | } |
---|
278 | else { |
---|
279 | $couleur = $defaut; |
---|
280 | } |
---|
281 | |
---|
282 | // Mettre en cache le resultat |
---|
283 | $couleur_extraite["$fichier-$x-$y"] = $couleur; |
---|
284 | ecrire_fichier($dest,$couleur_extraite["$fichier-$x-$y"]); |
---|
285 | } |
---|
286 | else { |
---|
287 | lire_fichier($dest,$couleur_extraite["$fichier-$x-$y"]); |
---|
288 | } |
---|
289 | |
---|
290 | return $couleur_extraite["$img-$x-$y"]=$couleur_extraite["$fichier-$x-$y"]; |
---|
291 | } |
---|
292 | |
---|
293 | // $src_img - a GD image resource |
---|
294 | // $angle - degrees to rotate clockwise, in degrees |
---|
295 | // returns a GD image resource |
---|
296 | // script de php.net lourdement corrig'e |
---|
297 | // (le bicubic deconnait completement, |
---|
298 | // et j'ai ajoute la ponderation par la distance au pixel) |
---|
299 | function _image_distance_pixel($xo, $yo, $x0, $y0) { |
---|
300 | $vx = $xo - $x0; |
---|
301 | $vy = $yo - $y0; |
---|
302 | $d = 1 - (sqrt(($vx)*($vx) + ($vy)*($vy)) / sqrt(2)); |
---|
303 | return $d; |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | /** |
---|
308 | * Decale une composante de couleur |
---|
309 | * entier de 0 a 255 |
---|
310 | * |
---|
311 | * @param int $coul |
---|
312 | * @param int $gamma |
---|
313 | * @return int |
---|
314 | */ |
---|
315 | function _image_decale_composante($coul, $gamma) { |
---|
316 | $coul = $coul + $gamma; |
---|
317 | |
---|
318 | if ($coul > 255) $coul = 255; |
---|
319 | if ($coul < 0) $coul = 0; |
---|
320 | return $coul; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * Decalage d'une composante de couleur en sepia |
---|
325 | * entier de 0 a 255 |
---|
326 | * |
---|
327 | * @param int $coul |
---|
328 | * @param int $val |
---|
329 | * @return int |
---|
330 | */ |
---|
331 | function _image_decale_composante_127($coul, $val) { |
---|
332 | if ($coul < 127) $y = round((($coul - 127) / 127) * $val) + $val; |
---|
333 | else if ($coul >= 127) $y = round((($coul - 127) / 128) * (255-$val)) + $val; |
---|
334 | else $y= $coul; |
---|
335 | |
---|
336 | if ($y < 0) $y = 0; |
---|
337 | if ($y > 255) $y = 255; |
---|
338 | return $y; |
---|
339 | } |
---|
340 | |
---|
341 | ?> |
---|