1 | <?php |
---|
2 | |
---|
3 | if (!defined("_ECRIRE_INC_VERSION")) return; |
---|
4 | |
---|
5 | include_spip('inc/filtres_images_lib_mini'); |
---|
6 | |
---|
7 | /* ---------- fonctions de Paris Beyrouth ------------------------- */ |
---|
8 | |
---|
9 | /* |
---|
10 | * |
---|
11 | * Fonctions pour roue chromatique |
---|
12 | * http://www.paris-beyrouth.org/Une-palette-de-couleurs |
---|
13 | * |
---|
14 | */ |
---|
15 | function image_rgb2hsv ($R,$G,$B) { |
---|
16 | $var_R = ( $R / 255 ) ; //Where RGB values = 0 to 255 |
---|
17 | $var_G = ( $G / 255 ); |
---|
18 | $var_B = ( $B / 255 ); |
---|
19 | |
---|
20 | $var_Min = min( $var_R, $var_G, $var_B ) ; //Min. value of RGB |
---|
21 | $var_Max = max( $var_R, $var_G, $var_B ) ; //Max. value of RGB |
---|
22 | $del_Max = $var_Max - $var_Min ; //Delta RGB value |
---|
23 | |
---|
24 | $V = $var_Max; |
---|
25 | $L = ( $var_Max + $var_Min ) / 2; |
---|
26 | |
---|
27 | if ( $del_Max == 0 ) //This is a gray, no chroma... |
---|
28 | { |
---|
29 | $H = 0 ; //HSL results = 0 to 1 |
---|
30 | $S = 0 ; |
---|
31 | } |
---|
32 | else //Chromatic data... |
---|
33 | { |
---|
34 | $S = $del_Max / $var_Max; |
---|
35 | |
---|
36 | $del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
37 | $del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
38 | $del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
39 | |
---|
40 | if ( $var_R == $var_Max ) $H = $del_B - $del_G; |
---|
41 | else if ( $var_G == $var_Max ) $H = ( 1 / 3 ) + $del_R - $del_B; |
---|
42 | else if ( $var_B == $var_Max ) $H = ( 2 / 3 ) + $del_G - $del_R; |
---|
43 | |
---|
44 | if ( $H < 0 ) $H = $H + 1; |
---|
45 | if ( $H > 1 ) $H = $H - 1; |
---|
46 | } |
---|
47 | |
---|
48 | $ret["h"] = $H; |
---|
49 | $ret["s"] = $S; |
---|
50 | $ret["v"] = $V; |
---|
51 | |
---|
52 | return $ret; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /* |
---|
57 | * http://www.paris-beyrouth.org/Une-palette-de-couleurs |
---|
58 | */ |
---|
59 | function image_hsv2rgb ($H,$S,$V) { |
---|
60 | if ( $S == 0 ) //HSV values = 0 to 1 |
---|
61 | { |
---|
62 | $R = $V * 255; |
---|
63 | $G = $V * 255; |
---|
64 | $B = $V * 255; |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | $var_h = $H * 6; |
---|
69 | if ( $var_h == 6 ) $var_h = 0 ; //H must be < 1 |
---|
70 | $var_i = floor( $var_h ) ; //Or ... var_i = floor( var_h ) |
---|
71 | $var_1 = $V * ( 1 - $S ); |
---|
72 | $var_2 = $V * ( 1 - $S * ( $var_h - $var_i ) ); |
---|
73 | $var_3 = $V * ( 1 - $S * ( 1 - ( $var_h - $var_i ) ) ); |
---|
74 | |
---|
75 | if ( $var_i == 0 ) { $var_r = $V ; $var_g = $var_3 ; $var_b = $var_1 ; } |
---|
76 | else if ( $var_i == 1 ) { $var_r = $var_2 ; $var_g = $V ; $var_b = $var_1 ; } |
---|
77 | else if ( $var_i == 2 ) { $var_r = $var_1 ; $var_g = $V ; $var_b = $var_3 ; } |
---|
78 | else if ( $var_i == 3 ) { $var_r = $var_1 ; $var_g = $var_2 ; $var_b = $V ; } |
---|
79 | else if ( $var_i == 4 ) { $var_r = $var_3 ; $var_g = $var_1 ; $var_b = $V ; } |
---|
80 | else { $var_r = $V ; $var_g = $var_1 ; $var_b = $var_2; } |
---|
81 | |
---|
82 | $R = $var_r * 255; //RGB results = 0 to 255 |
---|
83 | $G = $var_g * 255; |
---|
84 | $B = $var_b * 255; |
---|
85 | } |
---|
86 | $ret["r"] = floor($R); |
---|
87 | $ret["g"] = floor($G); |
---|
88 | $ret["b"] = floor($B); |
---|
89 | |
---|
90 | return $ret; |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * http://www.paris-beyrouth.org/Des-titres-en-relief |
---|
95 | */ |
---|
96 | function image_estampage_alpha($im, $trait=1, $prof=1) |
---|
97 | { |
---|
98 | $dec1 = floor($trait/2); |
---|
99 | $dec2 = ceil($trait/2); |
---|
100 | |
---|
101 | $image = _image_valeurs_trans($im, "estampage-$trait-$prof"); |
---|
102 | if (!$image) return(""); |
---|
103 | |
---|
104 | |
---|
105 | $x_i = $image["largeur"]; |
---|
106 | $y_i = $image["hauteur"]; |
---|
107 | $im = $image["fichier"]; |
---|
108 | $dest = $image["fichier_dest"]; |
---|
109 | $creer = $image["creer"]; |
---|
110 | |
---|
111 | if ($creer) { |
---|
112 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
113 | $im_ = imagecreatetruecolor($x_i, $y_i); |
---|
114 | @imagealphablending($im_, false); |
---|
115 | @imagesavealpha($im_,true); |
---|
116 | $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 ); |
---|
117 | imagefill ($im_, 0, 0, $color_t); |
---|
118 | |
---|
119 | for ($x = 0; $x < $x_i; $x++) { |
---|
120 | for ($y=0; $y < $y_i; $y++) { |
---|
121 | |
---|
122 | $rgb = ImageColorAt($im, $x, $y); |
---|
123 | $a = ($rgb >> 24) & 0xFF; |
---|
124 | |
---|
125 | $x1 = $x+$dec1; |
---|
126 | $y1 = $y+$dec1; |
---|
127 | if ($x1 < 0 OR $x1 >= $x_i) $x1 = $x; |
---|
128 | if ($y1 < 0 OR $y1 >= $y_i) $y1 = $y; |
---|
129 | $rgb1 = ImageColorAt($im, $x1, $y1); |
---|
130 | $a1 = ($rgb1 >> 24) & 0xFF; |
---|
131 | |
---|
132 | $x2 = $x-$dec2; |
---|
133 | $y2 = $y-$dec2; |
---|
134 | if ($x2 < 0 OR $x2 >= $x_i) $x2 = $x; |
---|
135 | if ($y2 < 0 OR $y2 >= $y_i) $y2 = $y; |
---|
136 | $rgb2 = ImageColorAt($im, $x2, $y2); |
---|
137 | $a2 = ($rgb2 >> 24) & 0xFF; |
---|
138 | |
---|
139 | $m = round((($a-$a1)+($a2-$a))*$prof); |
---|
140 | $m = max(min($m,127),-127); |
---|
141 | $m += 127; |
---|
142 | $color = ImageColorAllocateAlpha( $im_, $m, $m, $m , 0 ); |
---|
143 | imagesetpixel ($im_, $x, $y, $color); |
---|
144 | } |
---|
145 | } |
---|
146 | _image_gd_output($im_,$image); |
---|
147 | imagedestroy($im_); |
---|
148 | imagedestroy($im); |
---|
149 | } |
---|
150 | |
---|
151 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | /** |
---|
157 | * http://www.paris-beyrouth.org/De-saturer-une-image-en-passant-en |
---|
158 | */ |
---|
159 | function image_saturer($im, $sat=1){ |
---|
160 | $image = _image_valeurs_trans($im, "saturer-$sat"); |
---|
161 | if (!$image) return(""); |
---|
162 | |
---|
163 | $x_i = $image["largeur"]; |
---|
164 | $y_i = $image["hauteur"]; |
---|
165 | |
---|
166 | $im = $image["fichier"]; |
---|
167 | $dest = $image["fichier_dest"]; |
---|
168 | $creer = $image["creer"]; |
---|
169 | |
---|
170 | if ($creer) { |
---|
171 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
172 | |
---|
173 | $im_ = imagecreatetruecolor($x_i, $y_i); |
---|
174 | @imagealphablending($im_, false); |
---|
175 | @imagesavealpha($im_,true); |
---|
176 | $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 ); |
---|
177 | imagefill ($im_, 0, 0, $color_t); |
---|
178 | |
---|
179 | for ($x = 0; $x < $x_i; $x++) { |
---|
180 | for ($y=0; $y < $y_i; $y++) { |
---|
181 | |
---|
182 | $rgb = ImageColorAt($im, $x, $y); |
---|
183 | $a = ($rgb >> 24) & 0xFF; |
---|
184 | $r = ($rgb >> 16) & 0xFF; |
---|
185 | $g = ($rgb >> 8) & 0xFF; |
---|
186 | $b = $rgb & 0xFF; |
---|
187 | |
---|
188 | if ($a < 127) { |
---|
189 | $hsv = image_rgb2hsv($r,$g,$b); |
---|
190 | $h = $hsv["h"]; |
---|
191 | $s = $hsv["s"]; |
---|
192 | $v = $hsv["v"]; |
---|
193 | |
---|
194 | $s = $s * $sat; |
---|
195 | $s = min($s,1); |
---|
196 | |
---|
197 | $rgb = image_hsv2rgb($h,$s,$v); |
---|
198 | $r = $rgb["r"]; |
---|
199 | $g = $rgb["g"]; |
---|
200 | $b = $rgb["b"]; |
---|
201 | |
---|
202 | } |
---|
203 | $color = ImageColorAllocateAlpha( $im_, $r, $g, $b , $a ); |
---|
204 | imagesetpixel ($im_, $x, $y, $color); |
---|
205 | } |
---|
206 | } |
---|
207 | _image_gd_output($im_,$image); |
---|
208 | imagedestroy($im_); |
---|
209 | imagedestroy($im); |
---|
210 | } |
---|
211 | |
---|
212 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /* |
---|
217 | * http://www.paris-beyrouth.org/Corriger-les-niveaux-des-images |
---|
218 | */ |
---|
219 | function image_niveaux_gris_auto($im, $limite=1000) { |
---|
220 | |
---|
221 | // $limite=1000: les nuances min et max representent 0,1% du total |
---|
222 | |
---|
223 | $image = _image_valeurs_trans($im, "niveaux_gris_auto-$limite"); |
---|
224 | if (!$image) return(""); |
---|
225 | |
---|
226 | $x_i = $image["largeur"]; |
---|
227 | $y_i = $image["hauteur"]; |
---|
228 | $im = $image["fichier"]; |
---|
229 | $dest = $image["fichier_dest"]; |
---|
230 | $creer = $image["creer"]; |
---|
231 | |
---|
232 | if ($creer) { |
---|
233 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
234 | |
---|
235 | // Calculer les poids des differentes nuances |
---|
236 | for ($x = 0; $x < $x_i; $x++) { |
---|
237 | for ($y=0; $y < $y_i; $y++) { |
---|
238 | |
---|
239 | $rgb = ImageColorAt($im, $x, $y); |
---|
240 | $a = ($rgb >> 24) & 0xFF; |
---|
241 | $r = ($rgb >> 16) & 0xFF; |
---|
242 | $g = ($rgb >> 8) & 0xFF; |
---|
243 | $b = $rgb & 0xFF; |
---|
244 | |
---|
245 | $a = (127-$a) / 127; |
---|
246 | $a=1; |
---|
247 | |
---|
248 | $gris = round($a*($r+$g+$b) / 3); |
---|
249 | $r = round($a*$r); |
---|
250 | $g = round($a*$g); |
---|
251 | $b = round($a*$b); |
---|
252 | |
---|
253 | if (!isset($val_gris[$gris])) { |
---|
254 | $val_gris[$gris]=0; |
---|
255 | } |
---|
256 | $val_gris[$gris] ++; |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | $total = $x_i * $y_i; |
---|
261 | |
---|
262 | for ($bas = 0; $somme_bas < $total/$limite; $bas++) { |
---|
263 | $somme_bas += $val_gris[$bas]; |
---|
264 | } |
---|
265 | |
---|
266 | for ($haut = 255; $somme_haut < $total/$limite ; $haut--) { |
---|
267 | $somme_haut += $val_gris[$haut]; |
---|
268 | } |
---|
269 | |
---|
270 | $courbe[0] = 0; |
---|
271 | $courbe[255] = 255; |
---|
272 | $courbe[$bas] = 0; |
---|
273 | $courbe[$haut] = 255; |
---|
274 | |
---|
275 | // Calculer le tableau des correspondances |
---|
276 | ksort($courbe); |
---|
277 | while (list($key, $val) = each($courbe)) { |
---|
278 | if ($key > 0) { |
---|
279 | $key1 = $key_old; |
---|
280 | $val1 = $val_old; |
---|
281 | $prop = ($val - $val1) / ($key-$key1); |
---|
282 | for ($i = $key1; $i < $key; $i++) { |
---|
283 | $valeur = round($prop * ($i - $key1) + $val1); |
---|
284 | $courbe[$i] = $valeur; |
---|
285 | } |
---|
286 | $key_old = $key; |
---|
287 | $val_old = $val; |
---|
288 | } else { |
---|
289 | $key_old = $key; |
---|
290 | $val_old = $val; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | // Appliquer les correspondances |
---|
295 | $im2 = imagecreatetruecolor($x_i, $y_i); |
---|
296 | @imagealphablending($im2, false); |
---|
297 | @imagesavealpha($im2,true); |
---|
298 | $color_t = ImageColorAllocateAlpha( $im2, 255, 255, 255 , 0 ); |
---|
299 | imagefill ($im2, 0, 0, $color_t); |
---|
300 | |
---|
301 | for ($x = 0; $x < $x_i; $x++) { |
---|
302 | for ($y=0; $y < $y_i; $y++) { |
---|
303 | $rgb = ImageColorAt($im, $x, $y); |
---|
304 | $a = ($rgb >> 24) & 0xFF; |
---|
305 | $r = ($rgb >> 16) & 0xFF; |
---|
306 | $v = ($rgb >> 8) & 0xFF; |
---|
307 | $b = $rgb & 0xFF; |
---|
308 | |
---|
309 | $r = $courbe[$r]; |
---|
310 | $v = $courbe[$v]; |
---|
311 | $b = $courbe[$b]; |
---|
312 | |
---|
313 | $color = ImageColorAllocateAlpha( $im2, $r, $v, $b , $a ); |
---|
314 | imagesetpixel ($im2, $x, $y, $color); |
---|
315 | } |
---|
316 | } |
---|
317 | _image_gd_output($im2,$image); |
---|
318 | imagedestroy($im2); |
---|
319 | imagedestroy($im); |
---|
320 | } |
---|
321 | |
---|
322 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
323 | } |
---|
324 | |
---|
325 | /* |
---|
326 | * http://www.paris-beyrouth.org/Creer-automatiquement-une |
---|
327 | */ |
---|
328 | function image_podpod($im, $coul='000000', $deb=0, $fin=70) |
---|
329 | { |
---|
330 | |
---|
331 | $image = _image_valeurs_trans($im, "podpod-$coul-$deb-$fin","png"); |
---|
332 | if (!$image) return(""); |
---|
333 | |
---|
334 | $couleurs = _couleur_hex_to_dec($coul); |
---|
335 | $dr= $couleurs["red"]; |
---|
336 | $dv= $couleurs["green"]; |
---|
337 | $db= $couleurs["blue"]; |
---|
338 | |
---|
339 | $x_i = $image["largeur"]; |
---|
340 | $y_i = $image["hauteur"]; |
---|
341 | |
---|
342 | $im = $image["fichier"]; |
---|
343 | $dest = $image["fichier_dest"]; |
---|
344 | |
---|
345 | $creer = $image["creer"]; |
---|
346 | |
---|
347 | if ($creer) { |
---|
348 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
349 | |
---|
350 | $im_ = imagecreatetruecolor($x_i, $y_i); |
---|
351 | @imagealphablending($im_, false); |
---|
352 | @imagesavealpha($im_,true); |
---|
353 | $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 ); |
---|
354 | imagefill ($im_, 0, 0, $color_t); |
---|
355 | |
---|
356 | for ($x = 0; $x < $x_i; $x++) { |
---|
357 | for ($y=0; $y < $y_i; $y++) { |
---|
358 | |
---|
359 | $rgb = ImageColorAt($im, $x, $y); |
---|
360 | $a = ($rgb >> 24) & 0xFF; |
---|
361 | $r = ($rgb >> 16) & 0xFF; |
---|
362 | $g = ($rgb >> 8) & 0xFF; |
---|
363 | $b = $rgb & 0xFF; |
---|
364 | |
---|
365 | $g = round(($r+$g+$b) / 3); |
---|
366 | |
---|
367 | if ($g >= $deb AND $g <= $fin) $color = ImageColorAllocateAlpha( $im_, $dr, $dv, $db , $a ); |
---|
368 | else $color = ImageColorAllocateAlpha( $im_, 0, 0, 0 , 127 ); |
---|
369 | |
---|
370 | imagesetpixel ($im_, $x, $y, $color); |
---|
371 | } |
---|
372 | } |
---|
373 | _image_gd_output($im2,$image); |
---|
374 | imagedestroy($im2); |
---|
375 | imagedestroy($im); |
---|
376 | } |
---|
377 | |
---|
378 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
379 | } |
---|
380 | |
---|
381 | |
---|
382 | /* |
---|
383 | * http://www.paris-beyrouth.org/Modifier-les-courbes-d-une-image |
---|
384 | */ |
---|
385 | function image_courbe($im, $couche, $courb="") { |
---|
386 | |
---|
387 | $numargs = func_num_args(); |
---|
388 | $arg_list = func_get_args(); |
---|
389 | $texte = $arg_list[0]; |
---|
390 | for ($i = 1; $i < $numargs; $i++) { |
---|
391 | if (preg_match("#=#", $arg_list[$i])) { |
---|
392 | $nom_variable = substr($arg_list[$i], 0, strpos($arg_list[$i], "=")); |
---|
393 | $val_variable = substr($arg_list[$i], strpos($arg_list[$i], "=")+1, strlen($arg_list[$i])); |
---|
394 | $courbe[$nom_variable] = $val_variable; |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | $image = _image_valeurs_trans($im, "courbe-$couche-".serialize($courbe)); |
---|
399 | if (!$image) return(""); |
---|
400 | |
---|
401 | $x_i = $image["largeur"]; |
---|
402 | $y_i = $image["hauteur"]; |
---|
403 | |
---|
404 | $im = $image["fichier"]; |
---|
405 | $dest = $image["fichier_dest"]; |
---|
406 | $creer = $image["creer"]; |
---|
407 | |
---|
408 | if ($creer) { |
---|
409 | $courbe[0] = 0; |
---|
410 | $courbe[255] = 255; |
---|
411 | |
---|
412 | ksort($courbe); |
---|
413 | while (list($key, $val) = each($courbe)) { |
---|
414 | if ($key > 0) { |
---|
415 | $key1 = $key_old; |
---|
416 | $val1 = $val_old; |
---|
417 | $prop = ($val - $val1) / ($key-$key1); |
---|
418 | for ($i = $key1; $i < $key; $i++) { |
---|
419 | $valeur = round($prop * ($i - $key1) + $val1); |
---|
420 | $courbe[$i] = $valeur; |
---|
421 | } |
---|
422 | $key_old = $key; |
---|
423 | $val_old = $val; |
---|
424 | } else { |
---|
425 | $key_old = $key; |
---|
426 | $val_old = $val; |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
431 | $im_ = imagecreatetruecolor($x_i, $y_i); |
---|
432 | @imagealphablending($im_, false); |
---|
433 | @imagesavealpha($im_,true); |
---|
434 | $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 0 ); |
---|
435 | imagefill ($im_, 0, 0, $color_t); |
---|
436 | |
---|
437 | for ($x = 0; $x < $x_i; $x++) { |
---|
438 | for ($y=0; $y < $y_i; $y++) { |
---|
439 | $rgb = ImageColorAt($im, $x, $y); |
---|
440 | $a = ($rgb >> 24) & 0xFF; |
---|
441 | $r = ($rgb >> 16) & 0xFF; |
---|
442 | $v = ($rgb >> 8) & 0xFF; |
---|
443 | $b = $rgb & 0xFF; |
---|
444 | |
---|
445 | if ($couche == "rvb" OR $couche == "r") $r = $courbe[$r]; |
---|
446 | if ($couche == "rvb" OR $couche == "v") $v = $courbe[$v]; |
---|
447 | if ($couche == "rvb" OR $couche == "b") $b = $courbe[$b]; |
---|
448 | |
---|
449 | $color = ImageColorAllocateAlpha( $im_, $r, $v, $b , $a ); |
---|
450 | imagesetpixel ($im_, $x, $y, $color); |
---|
451 | } |
---|
452 | } |
---|
453 | _image_gd_output($im_,$image); |
---|
454 | imagedestroy($im_); |
---|
455 | imagedestroy($im); |
---|
456 | } |
---|
457 | |
---|
458 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | /* |
---|
463 | * http://www.paris-beyrouth.org/Un-habillage-irregulier |
---|
464 | */ |
---|
465 | function image_float ($img, $align, $margin=10) { |
---|
466 | |
---|
467 | $image = _image_valeurs_trans($img, "float-$align", "php"); |
---|
468 | if (!$image) return(""); |
---|
469 | |
---|
470 | $w = $image["largeur"]; |
---|
471 | $h = $image["hauteur"]; |
---|
472 | $precision = round($h / 5); |
---|
473 | |
---|
474 | $im = $image["fichier"]; |
---|
475 | $dest = $image["fichier_dest"]; |
---|
476 | $creer = $image["creer"]; |
---|
477 | |
---|
478 | $ret = "<div style='position: relative; float: $align; width: 0px; height: 0px;'><img src='$im' class='format_png' alt='' style='position: absolute; $align: 0px;' /></div>"; |
---|
479 | |
---|
480 | if ($creer) { |
---|
481 | $nouveau = _image_valeurs_trans(image_reduire($im, 0, $precision),""); |
---|
482 | $im_n = $nouveau["fichier"]; |
---|
483 | $x_i = $nouveau["largeur"]; |
---|
484 | $y_i = $nouveau["hauteur"]; |
---|
485 | $rapport = ($w / $x_i); |
---|
486 | |
---|
487 | $im_n = $image["fonction_imagecreatefrom"]($im_n); |
---|
488 | |
---|
489 | // une premiere passe |
---|
490 | // pour recuperer les valeurs |
---|
491 | for ($j = 0; $j < $y_i; $j++) { |
---|
492 | $transp = true; |
---|
493 | |
---|
494 | for ($i = 0; $i < $x_i && $transp; $i++) { |
---|
495 | |
---|
496 | if ($align == "right") $rgb = ImageColorAt($im_n, $i+1, $j); |
---|
497 | else $rgb = ImageColorAt($im_n, ($x_i - $i)-1, $j); |
---|
498 | $a = ($rgb >> 24) & 0xFF; |
---|
499 | |
---|
500 | if ($a > 125) $larg[$j] ++; |
---|
501 | else $transp = false; |
---|
502 | } |
---|
503 | } |
---|
504 | |
---|
505 | $larg[-1] = $w; |
---|
506 | $larg[$y_i] = $w; |
---|
507 | // une deuxieme passe |
---|
508 | // pour appliquer les valeurs |
---|
509 | // en utilisant les valeurs precedente et suivante |
---|
510 | for ($j = 0; $j < $y_i; $j++) { |
---|
511 | $reste = ($precision - $j); |
---|
512 | $haut_rest = $h - $haut_tot; |
---|
513 | $hauteur = ($reste == 0 ? 0 : round(($haut_rest) / $reste)); |
---|
514 | $haut_tot = $haut_tot + $hauteur; |
---|
515 | $resultat = min($larg[$j-1],$larg[$j],$larg[$j+1]); |
---|
516 | |
---|
517 | $forme .= "\n<div style='float: $align; clear: $align; width: ".($margin+round(($w - ($resultat)*$rapport)))."px ; height: ".round($hauteur)."px; overflow: hidden;'></div>"; |
---|
518 | } |
---|
519 | // Ajouter un div de plus en dessous |
---|
520 | $forme .= "\n<div style='float: $align; clear: $align; width: ".($margin+round(($w - ($resultat)*$rapport)))."px ; height: ".round($hauteur)."px; overflow: hidden;'></div>"; |
---|
521 | |
---|
522 | // Sauvegarder le fichier |
---|
523 | $handle = fopen($dest, 'w'); |
---|
524 | fwrite($handle, $forme); |
---|
525 | fclose($handle); |
---|
526 | |
---|
527 | $ret .= $forme; |
---|
528 | } |
---|
529 | else { |
---|
530 | $ret .= join(file($dest),""); |
---|
531 | } |
---|
532 | |
---|
533 | return $ret; |
---|
534 | } |
---|
535 | |
---|
536 | |
---|
537 | /* |
---|
538 | * http://www.paris-beyrouth.org/Tracer-les-contours-de |
---|
539 | */ |
---|
540 | function image_contour_alpha($im, $coul='000000', $trait=1) |
---|
541 | { |
---|
542 | $image = _image_valeurs_trans($im, "contour-$coul-$trait", "png"); |
---|
543 | if (!$image) return(""); |
---|
544 | |
---|
545 | $couleurs = _couleur_hex_to_dec($coul); |
---|
546 | $dr= $couleurs["red"]; |
---|
547 | $dv= $couleurs["green"]; |
---|
548 | $db= $couleurs["blue"]; |
---|
549 | |
---|
550 | $x_i = $image["largeur"]; |
---|
551 | $y_i = $image["hauteur"]; |
---|
552 | |
---|
553 | $im = $image["fichier"]; |
---|
554 | $dest = $image["fichier_dest"]; |
---|
555 | |
---|
556 | $creer = $image["creer"]; |
---|
557 | |
---|
558 | if ($creer) { |
---|
559 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
560 | $im_ = imagecreatetruecolor($x_i, $y_i); |
---|
561 | @imagealphablending($im_, false); |
---|
562 | @imagesavealpha($im_,true); |
---|
563 | $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 ); |
---|
564 | imagefill ($im_, 0, 0, $color_t); |
---|
565 | |
---|
566 | for ($x = 0; $x < $x_i; $x++) { |
---|
567 | for ($y=0; $y < $y_i; $y++) { |
---|
568 | |
---|
569 | $rgb = ImageColorAt($im, $x, $y); |
---|
570 | $a = ($rgb >> 24) & 0xFF; |
---|
571 | |
---|
572 | $dif = false; |
---|
573 | $m = 0; |
---|
574 | $t = 0; |
---|
575 | for ($ix = -1*$trait/2; $ix <= $trait/2; $ix++) { |
---|
576 | for ($iy = -1*$trait/2; $iy <= $trait/2; $iy++) { |
---|
577 | $x2 = $x + $ix; |
---|
578 | $y2 = $y + $iy; |
---|
579 | if ($x2 >=0 AND $y2 >= 0 AND $x2 < $x_i AND $y2 < $y_i) { |
---|
580 | $t++; |
---|
581 | $rgb2 = ImageColorAt($im, $x2, $y2); |
---|
582 | $a2 = ($rgb2 >> 24) & 0xFF; |
---|
583 | $r2 = ($rgb2 >> 16) & 0xFF; |
---|
584 | $g2 = ($rgb2 >> 8) & 0xFF; |
---|
585 | $b2 = $rgb2 & 0xFF; |
---|
586 | |
---|
587 | if ($a != $a2) { |
---|
588 | $dx = min(abs($ix),abs($iy)); |
---|
589 | $dy = max(abs($ix),abs($iy)); |
---|
590 | if ($mem[$dx][$dy]) $d = $mem[$dx][$dy]; |
---|
591 | else { |
---|
592 | $mem[$dx][$dy] = sqrt(($dx)*($dx)+($dy)*($dy)); |
---|
593 | $d = $mem[$dx][$dy]; |
---|
594 | } |
---|
595 | if ($d>0) { |
---|
596 | $m = $m + (abs($a2-$a) / $d); |
---|
597 | } else { |
---|
598 | $m = $m + 127; |
---|
599 | } |
---|
600 | } |
---|
601 | } |
---|
602 | } |
---|
603 | } |
---|
604 | $m = 127 - (($m / $t) * $trait); |
---|
605 | $m = min(max($m, 0), 127); |
---|
606 | |
---|
607 | $color = ImageColorAllocateAlpha( $im_, $dr, $dv, $db , round($m) ); |
---|
608 | imagesetpixel ($im_, $x, $y, $color); |
---|
609 | } |
---|
610 | } |
---|
611 | _image_gd_output($im_,$image); |
---|
612 | imagedestroy($im_); |
---|
613 | imagedestroy($im); |
---|
614 | } |
---|
615 | |
---|
616 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
617 | } |
---|
618 | |
---|
619 | |
---|
620 | |
---|
621 | /* |
---|
622 | * http://www.paris-beyrouth.org/Welcome-to-Spip-City |
---|
623 | */ |
---|
624 | function image_sincity($im) |
---|
625 | { |
---|
626 | $image = _image_valeurs_trans($im, "sincity"); |
---|
627 | if (!$image) return(""); |
---|
628 | |
---|
629 | $x_i = $image["largeur"]; |
---|
630 | $y_i = $image["hauteur"]; |
---|
631 | |
---|
632 | $im = $image["fichier"]; |
---|
633 | $dest = $image["fichier_dest"]; |
---|
634 | $creer = $image["creer"]; |
---|
635 | |
---|
636 | if ($creer) { |
---|
637 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
638 | |
---|
639 | $im_ = imagecreatetruecolor($x_i, $y_i); |
---|
640 | @imagealphablending($im_, false); |
---|
641 | @imagesavealpha($im_,true); |
---|
642 | $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 ); |
---|
643 | imagefill ($im_, 0, 0, $color_t); |
---|
644 | |
---|
645 | $tol = 0.05 ; |
---|
646 | for ($x = 0; $x < $x_i; $x++) { |
---|
647 | for ($y=0; $y < $y_i; $y++) { |
---|
648 | |
---|
649 | $rgb = ImageColorAt($im, $x, $y); |
---|
650 | $a = ($rgb >> 24) & 0xFF; |
---|
651 | $r = ($rgb >> 16) & 0xFF; |
---|
652 | $g = ($rgb >> 8) & 0xFF; |
---|
653 | $b = $rgb & 0xFF; |
---|
654 | |
---|
655 | if ($a < 127) { |
---|
656 | $hsv = image_rgb2hsv($r,$g,$b); |
---|
657 | $h = $hsv["h"]; |
---|
658 | $s = $hsv["s"]; |
---|
659 | $v = $hsv["v"]; |
---|
660 | |
---|
661 | if ($h < $tol OR $h > 1-$tol) { |
---|
662 | if ($h < $tol) { |
---|
663 | $dist = ($tol-$h); |
---|
664 | } |
---|
665 | else if ($h > 1-$tol) { |
---|
666 | $dist = ($h - (1-$tol)); |
---|
667 | } |
---|
668 | $s = $s * ($dist/$tol); |
---|
669 | if ($s > 1) $s = 1; |
---|
670 | $h = 0; |
---|
671 | } else { |
---|
672 | $s = 0; |
---|
673 | } |
---|
674 | |
---|
675 | $v = 2*($v - 0.6) + 0.6; |
---|
676 | |
---|
677 | if ($v > 1) $v=1; |
---|
678 | if ($v < 0) $v =0; |
---|
679 | |
---|
680 | $rgb = image_hsv2rgb($h,$s,$v); |
---|
681 | $r = $rgb["r"]; |
---|
682 | $g = $rgb["g"]; |
---|
683 | $b = $rgb["b"]; |
---|
684 | } |
---|
685 | $color = ImageColorAllocateAlpha( $im_, $r, $g, $b , $a ); |
---|
686 | imagesetpixel ($im_, $x, $y, $color); } |
---|
687 | } |
---|
688 | _image_gd_output($im_,$image); |
---|
689 | imagedestroy($im_); |
---|
690 | imagedestroy($im); |
---|
691 | } |
---|
692 | |
---|
693 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
694 | } |
---|
695 | |
---|
696 | |
---|
697 | |
---|
698 | |
---|
699 | /* |
---|
700 | * http://www.paris-beyrouth.org/Un-filtre-de-dispersion-graphique |
---|
701 | */ |
---|
702 | function image_dispersion($im, $masque, $h=5, $v=5, $pos="") { |
---|
703 | |
---|
704 | $nom = preg_replace("#\.(png|jpg|gif)$#", "", $masque); |
---|
705 | $nom = str_replace("/","-",$nom); |
---|
706 | |
---|
707 | $numargs = func_num_args(); |
---|
708 | $arg_list = func_get_args(); |
---|
709 | $texte = $arg_list[0]; |
---|
710 | for ($i = 1; $i < $numargs; $i++) { |
---|
711 | if (preg_match("#=#", $arg_list[$i])) { |
---|
712 | $nom_variable = substr($arg_list[$i], 0, strpos($arg_list[$i], "=")); |
---|
713 | $val_variable = substr($arg_list[$i], strpos($arg_list[$i], "=")+1, strlen($arg_list[$i])); |
---|
714 | $variable["$nom_variable"] = $val_variable; |
---|
715 | $defini["$nom_variable"] = 1; |
---|
716 | } |
---|
717 | } |
---|
718 | |
---|
719 | $image = _image_valeurs_trans($im, "disp$nom-$h-$v$pos", "png"); |
---|
720 | if (!$image) return(""); |
---|
721 | |
---|
722 | $x_i = $image["largeur"]; |
---|
723 | $y_i = $image["hauteur"]; |
---|
724 | $im = $image["fichier"]; |
---|
725 | $dest = $image["fichier_dest"]; |
---|
726 | $creer = $image["creer"]; |
---|
727 | |
---|
728 | if (strlen($pos) > 0) { |
---|
729 | $placer = true; |
---|
730 | } |
---|
731 | else $placer = false; |
---|
732 | |
---|
733 | if ($creer) { |
---|
734 | include_spip('inc/logos'); // bicoz presence image_reduire |
---|
735 | |
---|
736 | $masque = find_in_path($masque); |
---|
737 | $mask = _image_valeurs_trans($masque,""); |
---|
738 | $im_m = $mask["fichier"]; |
---|
739 | $x_m = $mask["largeur"]; |
---|
740 | $y_m = $mask["hauteur"]; |
---|
741 | |
---|
742 | $im2 = $mask["fonction_imagecreatefrom"]($masque); |
---|
743 | |
---|
744 | if ($placer) { |
---|
745 | // On fabriquer une version "agrandie" du masque, |
---|
746 | // aux dimensions de l'image source |
---|
747 | // et on "installe" le masque dans cette image |
---|
748 | // ainsi: aucun redimensionnement |
---|
749 | |
---|
750 | $dx = 0; |
---|
751 | $dy = 0; |
---|
752 | |
---|
753 | if ($defini["right"]) { |
---|
754 | $right = $variable["right"]; |
---|
755 | $dx = ($x_i - $x_m) - $right; |
---|
756 | } |
---|
757 | if ($defini["bottom"]) { |
---|
758 | $bottom = $variable["bottom"]; |
---|
759 | $dy = ($y_i - $y_m) - $bottom; |
---|
760 | } |
---|
761 | if ($defini["top"]) { |
---|
762 | $top = $variable["top"]; |
---|
763 | $dy = $top; |
---|
764 | } |
---|
765 | if ($defini["left"]) { |
---|
766 | $left = $variable["left"]; |
---|
767 | $dx = $left; |
---|
768 | } |
---|
769 | |
---|
770 | $im3 = imagecreatetruecolor($x_i, $y_i); |
---|
771 | @imagealphablending($im3, false); |
---|
772 | @imagesavealpha($im3,true); |
---|
773 | $color_t = ImageColorAllocateAlpha( $im3, 128, 128, 128 , 0 ); |
---|
774 | imagefill ($im3, 0, 0, $color_t); |
---|
775 | |
---|
776 | imagecopy ( $im3, $im2, $dx, $dy, 0, 0, $x_m, $y_m); |
---|
777 | |
---|
778 | imagedestroy($im2); |
---|
779 | $im2 = imagecreatetruecolor($x_i, $y_i); |
---|
780 | @imagealphablending($im2, false); |
---|
781 | @imagesavealpha($im2,true); |
---|
782 | |
---|
783 | imagecopy ( $im2, $im3, 0, 0, 0, 0, $x_i, $y_i); |
---|
784 | imagedestroy($im3); |
---|
785 | $x_m = $x_i; |
---|
786 | $y_m = $y_i; |
---|
787 | } |
---|
788 | |
---|
789 | $rapport = $x_i / $x_m; |
---|
790 | if (($y_i / $y_m) < $rapport ) { |
---|
791 | $rapport = $y_i / $y_m; |
---|
792 | } |
---|
793 | |
---|
794 | $x_d = ceil($x_i / $rapport); |
---|
795 | $y_d = ceil($y_i / $rapport); |
---|
796 | |
---|
797 | if ($x_i < $x_m OR $y_i < $y_m) { |
---|
798 | $x_dest = $x_i; |
---|
799 | $y_dest = $y_i; |
---|
800 | $x_dec = 0; |
---|
801 | $y_dec = 0; |
---|
802 | } else { |
---|
803 | $x_dest = $x_m; |
---|
804 | $y_dest = $y_m; |
---|
805 | $x_dec = round(($x_d - $x_m) /2); |
---|
806 | $y_dec = round(($y_d - $y_m) /2); |
---|
807 | } |
---|
808 | |
---|
809 | $nouveau = _image_valeurs_trans(image_reduire($im, $x_d, $y_d),""); |
---|
810 | $im_n = $nouveau["fichier"]; |
---|
811 | $im = $nouveau["fonction_imagecreatefrom"]($im_n); |
---|
812 | $im_ = imagecreatetruecolor($x_dest, $y_dest); |
---|
813 | @imagealphablending($im_, false); |
---|
814 | @imagesavealpha($im_,true); |
---|
815 | $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 ); |
---|
816 | imagefill ($im_, 0, 0, $color_t); |
---|
817 | |
---|
818 | for ($x = 0; $x < $x_dest; $x++) { |
---|
819 | for ($y=0; $y < $y_dest; $y++) { |
---|
820 | |
---|
821 | $rgb2 = ImageColorAt($im2, $x+$x_dec, $y+$y_dec); |
---|
822 | $a2 = ($rgb2 >> 24) & 0xFF; |
---|
823 | $r2 = ($rgb2 >> 16) & 0xFF; |
---|
824 | $g2 = ($rgb2 >> 8) & 0xFF; |
---|
825 | $b2 = $rgb2 & 0xFF; |
---|
826 | |
---|
827 | $g2 = ($r2+$g2+$b2)/3; |
---|
828 | $val = ($g2-127)/127; |
---|
829 | $xd = $x - ($val*$h); |
---|
830 | $yd = $y + ($val*$v); |
---|
831 | |
---|
832 | $xd = max(0,$xd); |
---|
833 | $yd = max(0,$yd); |
---|
834 | if ($xd > $x_dest - $x_dec - 1) $xd = $x_dest - $x_dec - 1; |
---|
835 | if ($yd > $y_dest - $y_dec - 1) $yd = $y_dest - $y_dec - 1; |
---|
836 | |
---|
837 | $rgb = ImageColorAt($im, $xd+$x_dec, $yd+$y_dec); |
---|
838 | $a = ($rgb >> 24) & 0xFF; |
---|
839 | $r = ($rgb >> 16) & 0xFF; |
---|
840 | $g = ($rgb >> 8) & 0xFF; |
---|
841 | $b = $rgb & 0xFF; |
---|
842 | |
---|
843 | $color = ImageColorAllocateAlpha( $im_, $r, $g, $b, $a ); |
---|
844 | imagesetpixel ($im_, $x, $y, $color); } |
---|
845 | } |
---|
846 | _image_gd_output($im_,$image); |
---|
847 | imagedestroy($im2); |
---|
848 | imagedestroy($im_); |
---|
849 | imagedestroy($im); |
---|
850 | } |
---|
851 | |
---|
852 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
853 | } |
---|
854 | |
---|
855 | /* ------------------ Autres fonctions ---------------------------- */ |
---|
856 | |
---|
857 | /* |
---|
858 | * |
---|
859 | * D'apres http://www.easyrgb.com/math.php |
---|
860 | */ |
---|
861 | function image_rgb2hsl($R,$G,$B) { |
---|
862 | $var_R = ( $R / 255 ); //Where RGB values = 0 � 255 |
---|
863 | $var_G = ( $G / 255 ); |
---|
864 | $var_B = ( $B / 255 ); |
---|
865 | |
---|
866 | $var_Min = min( $var_R, $var_G, $var_B ); //Min. value of RGB |
---|
867 | $var_Max = max( $var_R, $var_G, $var_B ); //Max. value of RGB |
---|
868 | $del_Max = $var_Max - $var_Min ; //Delta RGB value |
---|
869 | |
---|
870 | $L = ( $var_Max + $var_Min ) / 2; |
---|
871 | |
---|
872 | if ( $del_Max == 0 ) //This is a gray, no chroma... |
---|
873 | { |
---|
874 | $H = 0; //HSL results = 0 � 1 |
---|
875 | $S = 0; |
---|
876 | } |
---|
877 | else //Chromatic data... |
---|
878 | { |
---|
879 | if ( $L < 0.5 ) $S = $del_Max / ( $var_Max + $var_Min ); |
---|
880 | else $S = $del_Max / ( 2 - $var_Max - $var_Min ); |
---|
881 | |
---|
882 | $del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
883 | $del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
884 | $del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max; |
---|
885 | |
---|
886 | if ( $var_R == $var_Max ) $H = $del_B - $del_G; |
---|
887 | else if ( $var_G == $var_Max ) $H = ( 1 / 3 ) + $del_R - $del_B; |
---|
888 | else if ( $var_B == $var_Max ) $H = ( 2 / 3 ) + $del_G - $del_R; |
---|
889 | |
---|
890 | if ( $H < 0 ) ; $H += 1; |
---|
891 | if ( $H > 1 ) ; $H -= 1; |
---|
892 | } |
---|
893 | |
---|
894 | $ret["h"] = $H; |
---|
895 | $ret["s"] = $S; |
---|
896 | $ret["l"] = $L; |
---|
897 | |
---|
898 | return $ret; |
---|
899 | } |
---|
900 | |
---|
901 | function image_hue2rgb($v1, $v2, $vH) { |
---|
902 | if ( $vH < 0 ) $vH += 1; |
---|
903 | if ( $vH > 1 ) $vH -= 1; |
---|
904 | if ( ( 6 * $vH ) < 1 ) return ( $v1 + ( $v2 - $v1 ) * 6 * $vH ); |
---|
905 | if ( ( 2 * $vH ) < 1 ) return ( $v2 ); |
---|
906 | if ( ( 3 * $vH ) < 2 ) return ( $v1 + ( $v2 - $v1 ) * ( ( 2 / 3 ) - $vH ) * 6 ); |
---|
907 | return ( $v1 ); |
---|
908 | } |
---|
909 | |
---|
910 | function image_hsl2rgb($H,$S,$L) { |
---|
911 | if ( $S == 0 ) //HSL values = 0 � 1 |
---|
912 | { |
---|
913 | $R = $L * 255; //RGB results = 0 � 255 |
---|
914 | $G = $L * 255; |
---|
915 | $B = $L * 255; |
---|
916 | } |
---|
917 | else |
---|
918 | { |
---|
919 | if ( $L < 0.5 ) $var_2 = $L * ( 1 + $S ); |
---|
920 | else $var_2 = ( $L + $S ) - ( $S * $L ); |
---|
921 | |
---|
922 | $var_1 = 2 * $L - $var_2; |
---|
923 | |
---|
924 | $R = 255 * image_hue2rgb( $var_1, $var_2, $H + ( 1 / 3 ) ); |
---|
925 | $G = 255 * image_hue2rgb( $var_1, $var_2, $H ); |
---|
926 | $B = 255 * image_hue2rgb( $var_1, $var_2, $H - ( 1 / 3 ) ); |
---|
927 | } |
---|
928 | |
---|
929 | $ret["r"] = floor($R); |
---|
930 | $ret["g"] = floor($G); |
---|
931 | $ret["b"] = floor($B); |
---|
932 | |
---|
933 | return $ret; |
---|
934 | } |
---|
935 | |
---|
936 | // |
---|
937 | // filtre tire de http://reflection.corephp.co.uk |
---|
938 | // adapte par Yohann Prigent |
---|
939 | // |
---|
940 | function image_reflechir($im, $hauteur=0.5, $alphastart=80, $alphaend=0, $red=127, $green=127, $blue=127){ |
---|
941 | //if(!function_exists('imagelayereffect')) |
---|
942 | // return; |
---|
943 | $image = _image_valeurs_trans($im, "relechir-$hauteur-$alphastart-$alphaend", "png"); |
---|
944 | if (!$image) return(""); |
---|
945 | $width = $image["largeur"]; |
---|
946 | $height = $image["hauteur"]; |
---|
947 | $im = $image["fichier"]; |
---|
948 | $dest = $image["fichier_dest"]; |
---|
949 | $creer = $image["creer"]; |
---|
950 | $new_height = $height * $hauteur; |
---|
951 | if($creer) { |
---|
952 | if($hauteur > $height) |
---|
953 | $hauteur = $height; |
---|
954 | $source = $image["fonction_imagecreatefrom"]($im); |
---|
955 | // We'll store the final reflection in $output. $buffer is for internal use. |
---|
956 | $resultat = imagecreatetruecolor($width, $new_height + $height); |
---|
957 | $output = imagecreatetruecolor($width, $new_height); |
---|
958 | $buffer = imagecreatetruecolor($width, $new_height); |
---|
959 | // Save any alpha data that might have existed in the source image and disable blending |
---|
960 | imagesavealpha($source, true); |
---|
961 | // alpha for output |
---|
962 | imagesavealpha($output, true); |
---|
963 | imagealphablending($output, false); |
---|
964 | // alpha for the buffer |
---|
965 | imagesavealpha($buffer, true); |
---|
966 | imagealphablending($buffer, false); |
---|
967 | // and alpha for the final result |
---|
968 | imagesavealpha($resultat, true); |
---|
969 | imagealphablending($resultat, false); |
---|
970 | // Copy the bottom-most part of the source image into the output |
---|
971 | imagecopy($output, $source, 0, 0, 0, $height - $new_height, $width, $new_height); |
---|
972 | // Rotate and flip it (strip flip method) |
---|
973 | for ($y = 0; $y < $new_height; $y++) |
---|
974 | { |
---|
975 | imagecopy($buffer, $output, 0, $y, 0, $new_height - $y - 1, $width, 1); |
---|
976 | } |
---|
977 | $output = $buffer; |
---|
978 | $alpha_length = abs($alphastart - $alphaend); |
---|
979 | imagelayereffect($output, IMG_EFFECT_OVERLAY); |
---|
980 | for ($y = 0; $y <= $new_height; $y++) |
---|
981 | { |
---|
982 | // Get % of reflection height |
---|
983 | $pct = $y / $new_height; |
---|
984 | // Get % of alpha |
---|
985 | if ($alphastart > $alphaend) |
---|
986 | { |
---|
987 | $alpha = (int) ($alphastart - ($pct * $alpha_length)); |
---|
988 | } |
---|
989 | else |
---|
990 | { |
---|
991 | $alpha = (int) ($alphastart + ($pct * $alpha_length)); |
---|
992 | } |
---|
993 | // Rejig it because of the way in which the image effect overlay works |
---|
994 | $final_alpha = 127 - $alpha; |
---|
995 | imagefilledrectangle($output, 0, $y, $width, $y, imagecolorallocatealpha($output, $red, $green, $blue, $final_alpha)); |
---|
996 | } |
---|
997 | // now, source with effect |
---|
998 | imagecopy($resultat, $source, 0, 0, 0, 0, $width, $height); |
---|
999 | imagecopy($resultat, $output, 0, $height,0, 0, $width, $height); |
---|
1000 | $image["fonction_image"]($resultat, "$dest"); |
---|
1001 | imagedestroy($buffer); |
---|
1002 | imagedestroy($resultat); |
---|
1003 | } |
---|
1004 | $class = $image["class"]; |
---|
1005 | if (strlen($class) > 1) $tags=" class='$class'"; |
---|
1006 | $tags = "$tags alt='".$image["alt"]."'"; |
---|
1007 | $style = $image["style"]; |
---|
1008 | if (strlen($style) > 1) $tags="$tags style='$style'"; |
---|
1009 | return "<img src='$dest'$tags />"; |
---|
1010 | } |
---|
1011 | |
---|
1012 | /** |
---|
1013 | * Filtre négatif |
---|
1014 | * @param unknown_type $im |
---|
1015 | */ |
---|
1016 | function image_negatif($im){ |
---|
1017 | |
---|
1018 | $image = _image_valeurs_trans($im, "negate-"); |
---|
1019 | |
---|
1020 | if (!$image) return(""); |
---|
1021 | |
---|
1022 | $x_i = $image["largeur"]; |
---|
1023 | $y_i = $image["hauteur"]; |
---|
1024 | |
---|
1025 | $im = $image["fichier"]; |
---|
1026 | $dest = $image["fichier_dest"]; |
---|
1027 | $creer = $image["creer"]; |
---|
1028 | |
---|
1029 | if ($creer){ |
---|
1030 | // on definit les images sources et destination |
---|
1031 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
1032 | $im_ = imagecreatetruecolor($x_i, $y_i); |
---|
1033 | @imagealphablending($im_, false); |
---|
1034 | @imagesavealpha($im_,true); |
---|
1035 | $color_t = imageColorAllocateAlpha( $im_, 128, 128, 128 , 0 ); |
---|
1036 | imagefill($im_, 0, 0, $color_t); |
---|
1037 | imagecopy($im_, $im, 0, 0, 0, 0, $x_i, $y_i); |
---|
1038 | |
---|
1039 | /** |
---|
1040 | * on donne l'effet negatif |
---|
1041 | * Si on a une version récente de PHP on passe par imagefilter directement |
---|
1042 | * Sinon on procède à l'analyse pixel par pixel |
---|
1043 | */ |
---|
1044 | if(function_exists('imagefilter')){ |
---|
1045 | imagefilter($im_, IMG_FILTER_NEGATE); |
---|
1046 | }else{ |
---|
1047 | for($x = 0; $x < imagesx($im_); ++$x){ |
---|
1048 | for($y = 0; $y < imagesy($im_); ++$y){ |
---|
1049 | $index = imagecolorat($im_, $x, $y); |
---|
1050 | $rgb = imagecolorsforindex($im_,$index); |
---|
1051 | $color = imagecolorallocatealpha($im_, 255 - $rgb['red'], 255 - $rgb['green'], 255 - $rgb['blue'],$rgb['alpha']); |
---|
1052 | imagesetpixel($im_, $x, $y, $color); |
---|
1053 | } |
---|
1054 | } |
---|
1055 | } |
---|
1056 | _image_gd_output($im_,$image); |
---|
1057 | imagedestroy($im_); |
---|
1058 | imagedestroy($im); |
---|
1059 | } |
---|
1060 | |
---|
1061 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
1062 | } |
---|
1063 | |
---|
1064 | /** |
---|
1065 | * Un filtre permettant de dire à peu près si une image est monochrome |
---|
1066 | * On réduit l'image de départ à un carré de 20x20 px et on analyse chaque couleur de chaque pixel |
---|
1067 | * Si on a moins de 13 couleurs différentes, on considère que l'image est plus ou moins monochrome |
---|
1068 | * |
---|
1069 | * @param unknown_type $img |
---|
1070 | * @param int $largeur |
---|
1071 | * @param int $seuil le nombre de couleur minimum d'une image non considérée comme monochrome |
---|
1072 | * @return bool true|false |
---|
1073 | */ |
---|
1074 | function image_monochrome($img,$largeur=20,$seuil=13){ |
---|
1075 | $cache = _image_valeurs_trans($img, "coul-monochrome-$largeur", "txt"); |
---|
1076 | if (!$cache) return(""); |
---|
1077 | |
---|
1078 | $x_i = $cache["largeur"]; |
---|
1079 | $y_i = $cache["hauteur"]; |
---|
1080 | |
---|
1081 | $creer = $cache["creer"]; |
---|
1082 | |
---|
1083 | $fichier = $cache["fichier"]; |
---|
1084 | $dest = $cache["fichier_dest"]; |
---|
1085 | |
---|
1086 | if ($creer) { |
---|
1087 | if (@file_exists($fichier)) { |
---|
1088 | $thumb = imagecreate($largeur, $largeur); |
---|
1089 | $source = $cache["fonction_imagecreatefrom"]($fichier); |
---|
1090 | imagepalettetotruecolor($source); |
---|
1091 | imagecopyresized($thumb, $source, 0, 0, 0, 0, $largeur, $largeur, $x_i, $y_i); |
---|
1092 | for ($x = 0; $x < $largeur; $x++) { |
---|
1093 | for ($y=0; $y < $largeur; $y++) { |
---|
1094 | $rgb[] = imagecolorat($thumb, $x, $y); |
---|
1095 | } |
---|
1096 | } |
---|
1097 | $rgb = array_unique($rgb); |
---|
1098 | } |
---|
1099 | } |
---|
1100 | if(count($rgb) < $seuil){ |
---|
1101 | return false; |
---|
1102 | } |
---|
1103 | else |
---|
1104 | return true; |
---|
1105 | } |
---|
1106 | |
---|
1107 | /* une fonction importee du plugin fotoremix par Yohann Prigent |
---|
1108 | * |
---|
1109 | * remplace aussi cette mise contribution : |
---|
1110 | * https://contrib.spip.net/Filtre-image_superpose |
---|
1111 | * |
---|
1112 | * |
---|
1113 | * @param unknown_type $img |
---|
1114 | * @param unknown_type $masque |
---|
1115 | * @param string $h alignement horizontal |
---|
1116 | * @param string $v alignement vertical |
---|
1117 | * @return |
---|
1118 | */ |
---|
1119 | function image_merge($im, $masque, $h='left', $v='top'){ |
---|
1120 | $image = _image_valeurs_trans($im, "merge-$masque-$h-$v"); |
---|
1121 | |
---|
1122 | if (!$image) return(""); |
---|
1123 | |
---|
1124 | $x_i = $image["largeur"]; |
---|
1125 | $y_i = $image["hauteur"]; |
---|
1126 | |
---|
1127 | $im = $image["fichier"]; |
---|
1128 | $dest = $image["fichier_dest"]; |
---|
1129 | $creer = $image["creer"]; |
---|
1130 | |
---|
1131 | if ($creer){ |
---|
1132 | // init |
---|
1133 | $enfoncement = 2/3; |
---|
1134 | $masque = find_in_path($masque); |
---|
1135 | |
---|
1136 | // on definit l'image de masque |
---|
1137 | $im_masque = @imagecreatefrompng($masque); |
---|
1138 | //imagealphablending($im_masque, false); |
---|
1139 | //imagesavealpha($im_masque, true); |
---|
1140 | $x_masque = 0; |
---|
1141 | $y_masque = 0; |
---|
1142 | |
---|
1143 | // on definit les images sources et destination |
---|
1144 | // l'image de destination est plus grande de la moitie du masque |
---|
1145 | $im = $image["fonction_imagecreatefrom"]($im); |
---|
1146 | $im_ = @imagecreatetruecolor($x_i + $x_masque, $y_i + $y_masque); |
---|
1147 | |
---|
1148 | // reglage des options de transparence |
---|
1149 | imagealphablending($im_, false); |
---|
1150 | imagesavealpha($im_, true); |
---|
1151 | |
---|
1152 | // on remplit l'image avec la couleur transparente |
---|
1153 | imagefilledrectangle($im_, 0, 0, $x_i + $x_masque, $y_i + $y_masque, imagecolorallocatealpha($im_, 255, 255, 255, 127)); |
---|
1154 | |
---|
1155 | // estimation du decalage en fonction de $h et $hori |
---|
1156 | if ($h == 'center'){ |
---|
1157 | $dest_x = 0; |
---|
1158 | $dest_x_masque = (imagesx($im_) - imagesx($im_masque))/2; |
---|
1159 | } |
---|
1160 | elseif ($h == 'right'){ |
---|
1161 | $dest_x = 0; |
---|
1162 | $dest_x_masque = imagesx($im_) - imagesx($im_masque); |
---|
1163 | } |
---|
1164 | else { // left par defaut |
---|
1165 | $dest_x = $x_masque; |
---|
1166 | $dest_x_masque = 0; |
---|
1167 | } |
---|
1168 | |
---|
1169 | if ($v == 'middle'){ |
---|
1170 | $dest_y = 0; |
---|
1171 | $dest_y_masque = ($dest_y_masque = imagesy($im_) - imagesy($im_masque))/2; |
---|
1172 | } |
---|
1173 | elseif ($v == 'bottom'){ |
---|
1174 | $dest_y = 0; |
---|
1175 | $dest_y_masque = imagesy($im_) - imagesy($im_masque); |
---|
1176 | } |
---|
1177 | else { // top par defaut |
---|
1178 | $dest_y = $y_masque; |
---|
1179 | $dest_y_masque = 0; |
---|
1180 | } |
---|
1181 | |
---|
1182 | // on copie l'image source |
---|
1183 | imagecopymerge($im_, $im, 0, 0, 0, 0, $x_i, $y_i, 100); |
---|
1184 | |
---|
1185 | // on copie le masque |
---|
1186 | imagealphablending($im_, true); |
---|
1187 | imagecopy($im_, $im_masque, $dest_x_masque, $dest_y_masque, 0, 0, imagesx($im_masque), imagesy($im_masque)); |
---|
1188 | |
---|
1189 | _image_gd_output($im_,$image); |
---|
1190 | imagedestroy($im_masque); |
---|
1191 | imagedestroy($im_); |
---|
1192 | imagedestroy($im); |
---|
1193 | } |
---|
1194 | |
---|
1195 | return _image_ecrire_tag($image,array('src'=>$dest)); |
---|
1196 | } |
---|
1197 | |
---|
1198 | /** |
---|
1199 | * Tramer une image avec la fonction orderedPosterizeImage d'Imagick |
---|
1200 | * Inspiré de https://github.com/lowtechmag/solar/wiki/Solar-Web-Design#dithered-images |
---|
1201 | * |
---|
1202 | * @param string $im |
---|
1203 | * Chemin de l'image ou balise html `<img src=... />` |
---|
1204 | * @param int $levels |
---|
1205 | * Le nombre de niveaux de couleurs à utiliser |
---|
1206 | * @param string $thmap |
---|
1207 | * Le nom de la carte de seuils de dither cf http://www.imagemagick.org/Usage/quantize/tmaps_list.txt |
---|
1208 | * @param boolean $color |
---|
1209 | * Générer une image en couleurs |
---|
1210 | * @return string balise image |
---|
1211 | */ |
---|
1212 | function image_tramer($img, $levels = 6, $thmap = 'o8x8', $color = false){ |
---|
1213 | $cache = _image_valeurs_trans($img, "tramer-".json_encode(array($thmap, $levels, $color)), 'png'); |
---|
1214 | if (!$cache) { |
---|
1215 | return false; |
---|
1216 | } |
---|
1217 | // facile ! |
---|
1218 | if ($cache['format_source'] === 'svg'){ |
---|
1219 | return $img; |
---|
1220 | } |
---|
1221 | |
---|
1222 | $fichier = $cache["fichier"]; |
---|
1223 | $dest = $cache["fichier_dest"]; |
---|
1224 | $creer = $cache["creer"]; |
---|
1225 | |
---|
1226 | if ($creer) { |
---|
1227 | if (method_exists('Imagick', 'orderedPosterizeImage')) { |
---|
1228 | $imagick = new Imagick(); |
---|
1229 | $imagick->readImage($fichier); |
---|
1230 | if (!$color) { |
---|
1231 | $imagick->setImageType(Imagick::IMGTYPE_GRAYSCALE); |
---|
1232 | } |
---|
1233 | $imagick->orderedPosterizeImage($thmap.','.$levels); |
---|
1234 | $imagick->setImageFormat('png'); |
---|
1235 | $imagick->writeImage($dest); |
---|
1236 | } |
---|
1237 | } |
---|
1238 | |
---|
1239 | return _image_ecrire_tag($cache, array('src' => $dest)); |
---|
1240 | } |
---|