2 /***************************************************************************\
3 * SPIP, Systeme de publication pour l'internet *
5 * Copyright (c) 2001-2014 *
6 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
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 \***************************************************************************/
12 if (!defined('_ECRIRE_INC_VERSION')) return;
15 * Toutes les fonctions image_xx de ce fichier :
16 * - prennent une image en entree
17 * - fournissent une image en sortie
18 * - sont chainables les unes derrieres les autres dans toutes les combinaisons possibles
21 // librairie de base du core
22 include_spip('inc/filtres_images_mini');
24 // 1/ Aplatir une image semi-transparente (supprimer couche alpha)
25 // en remplissant la transparence avec couleur choisir $coul.
26 // 2/ Forcer le format de sauvegarde (jpg, png, gif)
27 // pour le format jpg, $qualite correspond au niveau de compression (defaut 85)
28 // pour le format gif, $qualite correspond au nombre de couleurs dans la palette (defaut 128)
29 // pour le format png, $qualite correspond au nombre de couleur dans la palette ou si 0 a une image truecolor (defaut truecolor)
30 // attention, seul 128 est supporte en l'etat (production d'images avec palette reduite pas satisfaisante)
31 // http://doc.spip.org/@image_aplatir
32 // 3/ $transparence a "true" permet de conserver la transparence (utile pour conversion GIF)
33 // http://doc.spip.org/@image_aplatir
34 function image_aplatir($im, $format='jpg', $coul='000000', $qualite=NULL, $transparence=false)
37 if ($format=='jpg') $qualite=_IMG_GD_QUALITE
;
38 elseif ($format=='png') $qualite=0;
41 $fonction = array('image_aplatir', func_get_args());
42 $image = _image_valeurs_trans($im, "aplatir-$format-$coul-$qualite-$transparence", $format, $fonction);
44 if (!$image) return("");
46 include_spip('inc/filtres');
47 $couleurs = _couleur_hex_to_dec($coul);
48 $dr= $couleurs["red"];
49 $dv= $couleurs["green"];
50 $db= $couleurs["blue"];
52 $x_i = $image["largeur"];
53 $y_i = $image["hauteur"];
55 $im = $image["fichier"];
56 $dest = $image["fichier_dest"];
58 $creer = $image["creer"];
61 $im = @$image["fonction_imagecreatefrom"]($im);
62 imagepalettetotruecolor($im);
63 $im_ = imagecreatetruecolor($x_i, $y_i);
64 if ($image["format_source"] == "gif" AND function_exists('ImageCopyResampled')) {
65 // Si un GIF est transparent,
66 // fabriquer un PNG transparent
67 // Conserver la transparence
68 @imagealphablending
($im_, false);
69 @imagesavealpha
($im_,true);
70 if (function_exists("imageAntiAlias")) imageAntiAlias($im_,true);
71 @ImageCopyResampled
($im_, $im, 0, 0, 0, 0, $x_i, $y_i, $x_i, $y_i);
76 // allouer la couleur de fond
78 @imagealphablending
($im_, false);
79 @imagesavealpha
($im_,true);
80 $color_t = imagecolorallocatealpha( $im_, $dr, $dv, $db, 127);
82 else $color_t = ImageColorAllocate( $im_, $dr, $dv, $db);
84 imagefill ($im_, 0, 0, $color_t);
87 //$dist = abs($trait);
91 for ($x = 0; $x < $x_i; $x++
) {
92 for ($y=0; $y < $y_i; $y++
) {
94 $rgb = ImageColorAt($im, $x, $y);
95 $a = ($rgb >> 24) & 0xFF;
96 $r = ($rgb >> 16) & 0xFF;
97 $g = ($rgb >> 8) & 0xFF;
102 if ($a == 1) { // Limiter calculs
107 else if ($a == 0) { // Limiter calculs
112 $transp_x = $x; // Memoriser un point transparent
116 $r = round($a * $r +
$dr * (1-$a));
117 $g = round($a * $g +
$dv * (1-$a));
118 $b = round($a * $b +
$db * (1-$a));
121 $color = ImageColorAllocateAlpha( $im_, $r, $g, $b, $a);
122 imagesetpixel ($im_, $x, $y, $color);
125 // passer en palette si besoin
126 if ($format=='gif' OR ($format=='png' AND $qualite!==0)){
127 // creer l'image finale a palette (on recycle l'image initiale)
130 @imagetruecolortopalette
($im,true,$qualite);
133 //$im = imagecreate($x_i, $y_i);
134 // copier l'image true color vers la palette
135 imagecopy($im, $im_, 0, 0, 0, 0, $x_i, $y_i);
136 // matcher les couleurs au mieux par rapport a l'image initiale
137 // si la fonction est disponible (php>=4.3)
138 if (function_exists('imagecolormatch'))
139 @imagecolormatch
($im_, $im);
141 if ($format=='gif' && $transparence && $transp_x) {
142 $color_t = ImagecolorAt( $im, $transp_x, $transp_y);
143 if ($format == "gif" && $transparence) @imagecolortransparent
($im, $color_t);
147 // produire le resultat
148 _image_gd_output($im, $image, $qualite);
151 _image_gd_output($im_, $image, $qualite);
155 return _image_ecrire_tag($image,array('src'=>$dest));
159 // Enregistrer une image dans un format donne
160 // (conserve la transparence gif, png, ico)
161 // utilise [->@image_aplatir]
162 // http://doc.spip.org/@image_format
163 function image_format($img, $format='png') {
165 if ($format=='png8') {$format='png';$qualite=128;}
166 return image_aplatir($img, $format, 'cccccc', $qualite, true);
170 // Transforme l'image en PNG transparent
171 // alpha = 0: aucune transparence
172 // alpha = 127: completement transparent
173 // http://doc.spip.org/@image_alpha
174 function image_alpha($im, $alpha = 63)
176 $fonction = array('image_alpha', func_get_args());
177 $image = _image_valeurs_trans($im, "alpha-$alpha", "png",$fonction);
178 if (!$image) return("");
180 $x_i = $image["largeur"];
181 $y_i = $image["hauteur"];
183 $im = $image["fichier"];
184 $dest = $image["fichier_dest"];
186 $creer = $image["creer"];
189 // Creation de l'image en deux temps
190 // de facon a conserver les GIF transparents
191 $im = $image["fonction_imagecreatefrom"]($im);
192 imagepalettetotruecolor($im);
193 $im2 = imagecreatetruecolor($x_i, $y_i);
194 @imagealphablending
($im2, false);
195 @imagesavealpha
($im2,true);
196 $color_t = ImageColorAllocateAlpha( $im2, 255, 255, 255 , 127 );
197 imagefill ($im2, 0, 0, $color_t);
198 imagecopy($im2, $im, 0, 0, 0, 0, $x_i, $y_i);
200 $im_ = imagecreatetruecolor($x_i, $y_i);
201 imagealphablending ($im_, FALSE );
202 imagesavealpha ( $im_, TRUE );
206 for ($x = 0; $x < $x_i; $x++
) {
207 for ($y = 0; $y < $y_i; $y++
) {
208 $rgb = ImageColorAt($im2, $x, $y);
210 if (function_exists('imagecolorallocatealpha')) {
211 $a = ($rgb >> 24) & 0xFF;
212 $r = ($rgb >> 16) & 0xFF;
213 $g = ($rgb >> 8) & 0xFF;
217 $a_ = $alpha +
$a - round($a*$alpha/127);
218 $rgb = imagecolorallocatealpha($im_, $r, $g, $b, $a_);
220 imagesetpixel ( $im_, $x, $y, $rgb );
223 _image_gd_output($im_,$image);
230 return _image_ecrire_tag($image,array('src'=>$dest));
234 // http://doc.spip.org/@image_recadre
235 function image_recadre($im,$width,$height,$position='center', $background_color='white')
237 $fonction = array('image_recadre', func_get_args());
238 $image = _image_valeurs_trans($im, "recadre-$width-$height-$position-$background_color",false,$fonction);
240 if (!$image) return("");
242 $x_i = $image["largeur"];
243 $y_i = $image["hauteur"];
245 if ($width==0) $width=$x_i;
246 if ($height==0) $height=$y_i;
248 $offset_width = $x_i-$width;
249 $offset_height = $y_i-$height;
250 $position=strtolower($position);
251 if (strpos($position,'left')!==FALSE){
252 if (preg_match(';left=(\d{1}\d+);', $position, $left)){
253 $offset_width=$left[1];
259 elseif (strpos($position,'right')!==FALSE)
260 $offset_width=$offset_width;
262 $offset_width=intval(ceil($offset_width/2));
264 if (strpos($position,'top')!==FALSE){
265 if (preg_match(';top=(\d{1}\d+);', $position, $top)){
266 $offset_height=$top[1];
272 elseif (strpos($position,'bottom')!==FALSE)
273 $offset_height=$offset_height;
275 $offset_height=intval(ceil($offset_height/2));
277 $im = $image["fichier"];
278 $dest = $image["fichier_dest"];
280 $creer = $image["creer"];
283 $im = $image["fonction_imagecreatefrom"]($im);
284 imagepalettetotruecolor($im);
285 $im_ = imagecreatetruecolor($width, $height);
286 @imagealphablending
($im_, false);
287 @imagesavealpha
($im_,true);
289 if ($background_color=='transparent')
290 $color_t = imagecolorallocatealpha( $im_, 255, 255, 255 , 127 );
292 $bg = _couleur_hex_to_dec($background_color);
293 $color_t = imagecolorallocate( $im_, $bg['red'], $bg['green'], $bg['blue']);
295 imagefill ($im_, 0, 0, $color_t);
296 imagecopy($im_, $im, max(0,-$offset_width), max(0,-$offset_height), max(0,$offset_width), max(0,$offset_height), min($width,$x_i), min($height,$y_i));
298 _image_gd_output($im_,$image);
303 return _image_ecrire_tag($image,array('src'=>$dest,'width'=>$width,'height'=>$height));
308 * Recadrer une image dans le rectangle le plus petit possible sans perte
309 * de pixels non transparent
314 function image_recadre_mini($im)
316 $fonction = array('image_recadre_mini', func_get_args());
317 $image = _image_valeurs_trans($im, "recadre_mini",false,$fonction);
319 if (!$image) return("");
321 $width = $image["largeur"];
322 $height = $image["hauteur"];
324 $im = $image["fichier"];
325 $dest = $image["fichier_dest"];
327 $creer = $image["creer"];
329 $im = $image["fonction_imagecreatefrom"]($im);
330 imagepalettetotruecolor($im);
332 // trouver le rectangle mini qui contient des infos de couleur
333 // recherche optimisee qui ne balaye que par zone
338 while ($yy<=$height/2 AND $max_y<=$min_y){
340 for ($xx = 0; $xx < $width; $xx++
) {
341 $color_index = imagecolorat($im, $xx, $yy);
342 $color_tran = imagecolorsforindex($im, $color_index);
343 if ($color_tran['alpha']!==127){
344 $min_y = min($yy,$min_y);
345 $max_y = max($height-1-$yy,$max_y);
349 if($height-1-$yy>$max_y)
350 for ($xx = 0; $xx < $width; $xx++
) {
351 $color_index = imagecolorat($im, $xx, $height-1-$yy);
352 $color_tran = imagecolorsforindex($im, $color_index);
353 if ($color_tran['alpha']!==127){
354 $min_y = min($yy,$min_y);
355 $max_y = max($height-1-$yy,$max_y);
361 $min_y = min($max_y,$min_y); // tout a 0 aucun pixel trouve
364 while ($xx<=$width/2 AND $max_x<=$min_x){
366 for ($yy = $min_y; $yy < $max_y; $yy++
) {
367 $color_index = imagecolorat($im, $xx, $yy);
368 $color_tran = imagecolorsforindex($im, $color_index);
369 if ($color_tran['alpha']!==127){
370 $min_x = min($xx,$min_x);
371 $max_x = max($xx,$max_x);
372 break; // inutile de continuer sur cette colonne
375 if ($width-1-$xx>$max_x)
376 for ($yy = $min_y; $yy < $max_y; $yy++
) {
377 $color_index = imagecolorat($im, $width-1-$xx, $yy);
378 $color_tran = imagecolorsforindex($im, $color_index);
379 if ($color_tran['alpha']!==127){
380 $min_x = min($width-1-$xx,$min_x);
381 $max_x = max($width-1-$xx,$max_x);
382 break; // inutile de continuer sur cette colonne
387 $min_x = min($max_x,$min_x); // tout a 0 aucun pixel trouve
389 $width = $max_x-$min_x+
1;
390 $height = $max_y-$min_y+
1;
392 $im_ = imagecreatetruecolor($width, $height);
393 @imagealphablending
($im_, false);
394 @imagesavealpha
($im_,true);
396 $color_t = imagecolorallocatealpha( $im_, 255, 255, 255 , 127 );
397 imagefill ($im_, 0, 0, $color_t);
398 imagecopy($im_, $im, 0, 0, $min_x, $min_y, $width, $height);
400 _image_gd_output($im_,$image);
405 list ($height,$width) = taille_image($image['fichier_dest']);
408 return _image_ecrire_tag($image,array('src'=>$dest,'width'=>$width,'height'=>$height));
412 // http://doc.spip.org/@image_flip_vertical
413 function image_flip_vertical($im)
415 $fonction = array('image_flip_vertical', func_get_args());
416 $image = _image_valeurs_trans($im, "flip_v", false,$fonction);
417 if (!$image) return("");
419 $x_i = $image["largeur"];
420 $y_i = $image["hauteur"];
422 $im = $image["fichier"];
423 $dest = $image["fichier_dest"];
425 $creer = $image["creer"];
428 $im = $image["fonction_imagecreatefrom"]($im);
429 imagepalettetotruecolor($im);
430 $im_ = imagecreatetruecolor($x_i, $y_i);
431 @imagealphablending
($im_, false);
432 @imagesavealpha
($im_,true);
434 $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 );
435 imagefill ($im_, 0, 0, $color_t);
437 for ($x = 0; $x < $x_i; $x++
) {
438 for ($y = 0; $y < $y_i; $y++
) {
439 imagecopy($im_, $im, $x_i - $x - 1, $y, $x, $y, 1, 1);
443 _image_gd_output($im_,$image);
448 return _image_ecrire_tag($image,array('src'=>$dest));
451 // http://doc.spip.org/@image_flip_horizontal
452 function image_flip_horizontal($im)
454 $fonction = array('image_flip_horizontal', func_get_args());
455 $image = _image_valeurs_trans($im, "flip_h",false,$fonction);
456 if (!$image) return("");
458 $x_i = $image["largeur"];
459 $y_i = $image["hauteur"];
461 $im = $image["fichier"];
462 $dest = $image["fichier_dest"];
464 $creer = $image["creer"];
467 $im = $image["fonction_imagecreatefrom"]($im);
468 imagepalettetotruecolor($im);
469 $im_ = imagecreatetruecolor($x_i, $y_i);
470 @imagealphablending
($im_, false);
471 @imagesavealpha
($im_,true);
473 $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 );
474 imagefill ($im_, 0, 0, $color_t);
476 for ($x = 0; $x < $x_i; $x++
) {
477 for ($y = 0; $y < $y_i; $y++
) {
478 imagecopy($im_, $im, $x, $y_i - $y - 1, $x, $y, 1, 1);
481 _image_gd_output($im_,$image);
486 return _image_ecrire_tag($image,array('src'=>$dest));
489 // http://doc.spip.org/@image_masque
490 function image_masque($im, $masque, $pos="") {
491 // Passer, en plus de l'image d'origine,
492 // une image de "masque": un fichier PNG24 transparent.
493 // Le decoupage se fera selon la transparence du "masque",
494 // et les couleurs seront eclaircies/foncees selon de couleur du masque.
495 // Pour ne pas modifier la couleur, le masque doit etre en gris 50%.
497 // Si l'image source est plus grande que le masque, alors cette image est reduite a la taille du masque.
498 // Sinon, c'est la taille de l'image source qui est utilisee.
500 // $pos est une variable libre, qui permet de passer left=..., right=..., bottom=..., top=...
501 // dans ce cas, le pasque est place a ces positions sur l'image d'origine,
502 // et evidemment cette image d'origine n'est pas redimensionnee
504 // Positionnement horizontal: text-align=left, right, center
505 // Positionnement vertical : vertical-align: top, bottom, middle
506 // (les positionnements left, right, top, left sont relativement inutiles, mais coherence avec CSS)
508 // Choix du mode de fusion: mode=masque, normal, eclaircir, obscurcir, produit, difference
509 // masque: mode par defaut
510 // normal: place la nouvelle image par dessus l'ancienne
511 // eclaircir: place uniquement les points plus clairs
512 // obscurcir: place uniquement les points plus fonc'es
513 // produit: multiplie par le masque (points noirs rendent l'image noire, points blancs ne changent rien)
514 // difference: remplit avec l'ecart entre les couleurs d'origine et du masque
519 $numargs = func_num_args();
520 $arg_list = func_get_args();
521 $texte = $arg_list[0];
522 for ($i = 1; $i < $numargs; $i++
) {
523 if ( ($p = strpos($arg_list[$i],"=")) !==false) {
524 $nom_variable = substr($arg_list[$i], 0, $p);
525 $val_variable = substr($arg_list[$i], $p+
1);
526 $variable["$nom_variable"] = $val_variable;
527 $defini["$nom_variable"] = 1;
530 if ($defini["mode"]) $mode = $variable["mode"];
532 $masque = find_in_path($masque);
533 $pos = md5(serialize($variable).@filemtime
($masque));
535 $fonction = array('image_masque', func_get_args());
536 $image = _image_valeurs_trans($im, "masque-$masque-$pos", "png",$fonction);
537 if (!$image) return("");
539 $x_i = $image["largeur"];
540 $y_i = $image["hauteur"];
542 $im = $image["fichier"];
543 $dest = $image["fichier_dest"];
545 $creer = $image["creer"];
548 if ($defini["right"] OR $defini["left"] OR $defini["bottom"] OR $defini["top"] OR $defini["text-align"] OR $defini["vertical-align"]) {
551 else $placer = false;
555 $mask = _image_valeurs_trans($masque,"");
556 if (!is_array($mask)) return("");
557 $im_m = $mask["fichier"];
558 $x_m = $mask["largeur"];
559 $y_m = $mask["hauteur"];
561 $im2 = $mask["fonction_imagecreatefrom"]($masque);
562 if ($mask["format_source"] == "gif" AND function_exists('ImageCopyResampled')) {
563 $im2_ = imagecreatetruecolor($x_m, $y_m);
564 // Si un GIF est transparent,
565 // fabriquer un PNG transparent
566 // Conserver la transparence
567 if (function_exists("imageAntiAlias")) imageAntiAlias($im2_,true);
568 @imagealphablending
($im2_, false);
569 @imagesavealpha
($im2_,true);
570 @ImageCopyResampled
($im2_, $im2, 0, 0, 0, 0, $x_m, $y_m, $x_m, $y_m);
576 // On fabriquer une version "agrandie" du masque,
577 // aux dimensions de l'image source
578 // et on "installe" le masque dans cette image
579 // ainsi: aucun redimensionnement
584 if ($defini["right"]) {
585 $right = $variable["right"];
586 $dx = ($x_i - $x_m) - $right;
588 if ($defini["bottom"]) {
589 $bottom = $variable["bottom"];
590 $dy = ($y_i - $y_m) - $bottom;
592 if ($defini["top"]) {
593 $top = $variable["top"];
596 if ($defini["left"]) {
597 $left = $variable["left"];
600 if ($defini["text-align"]) {
601 $align = $variable["text-align"];
602 if ($align == "right") {
605 } else if ($align == "left") {
608 } else if ($align = "center") {
609 $dx = round( ($x_i - $x_m) / 2 ) ;
612 if ($defini["vertical-align"]) {
613 $valign = $variable["vertical-align"];
614 if ($valign == "bottom") {
617 } else if ($valign == "top") {
620 } else if ($valign = "middle") {
621 $dy = round( ($y_i - $y_m) / 2 ) ;
626 $im3 = imagecreatetruecolor($x_i, $y_i);
627 @imagealphablending
($im3, false);
628 @imagesavealpha
($im3,true);
629 if ($mode == "masque") $color_t = ImageColorAllocateAlpha( $im3, 128, 128, 128 , 0 );
630 else $color_t = ImageColorAllocateAlpha( $im3, 128, 128, 128 , 127 );
631 imagefill ($im3, 0, 0, $color_t);
635 imagecopy ( $im3, $im2, $dx, $dy, 0, 0, $x_m, $y_m);
638 $im2 = imagecreatetruecolor($x_i, $y_i);
639 @imagealphablending
($im2, false);
640 @imagesavealpha
($im2,true);
644 imagecopy ( $im2, $im3, 0, 0, 0, 0, $x_i, $y_i);
651 $rapport = $x_i / $x_m;
652 if (($y_i / $y_m) < $rapport ) {
653 $rapport = $y_i / $y_m;
656 $x_d = ceil($x_i / $rapport);
657 $y_d = ceil($y_i / $rapport);
660 if ($x_i < $x_m OR $y_i < $y_m) {
668 $x_dec = round(($x_d - $x_m) /2);
669 $y_dec = round(($y_d - $y_m) /2);
673 $nouveau = _image_valeurs_trans(image_reduire($im, $x_d, $y_d),"");
674 if (!is_array($nouveau)) return("");
675 $im_n = $nouveau["fichier"];
678 $im = $nouveau["fonction_imagecreatefrom"]($im_n);
679 imagepalettetotruecolor($im);
680 if ($nouveau["format_source"] == "gif" AND function_exists('ImageCopyResampled')) {
681 $im_ = imagecreatetruecolor($x_dest, $y_dest);
682 // Si un GIF est transparent,
683 // fabriquer un PNG transparent
684 // Conserver la transparence
685 if (function_exists("imageAntiAlias")) imageAntiAlias($im_,true);
686 @imagealphablending
($im_, false);
687 @imagesavealpha
($im_,true);
688 @ImageCopyResampled
($im_, $im, 0, 0, 0, 0, $x_dest, $y_dest, $x_dest, $y_dest);
692 $im_ = imagecreatetruecolor($x_dest, $y_dest);
693 @imagealphablending
($im_, false);
694 @imagesavealpha
($im_,true);
695 $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 );
696 imagefill ($im_, 0, 0, $color_t);
699 for ($x = 0; $x < $x_dest; $x++
) {
700 for ($y=0; $y < $y_dest; $y++
) {
701 $rgb = ImageColorAt($im2, $x, $y);
702 $a = ($rgb >> 24) & 0xFF;
703 $r = ($rgb >> 16) & 0xFF;
704 $g = ($rgb >> 8) & 0xFF;
708 $rgb2 = ImageColorAt($im, $x+
$x_dec, $y+
$y_dec);
709 $a2 = ($rgb2 >> 24) & 0xFF;
710 $r2 = ($rgb2 >> 16) & 0xFF;
711 $g2 = ($rgb2 >> 8) & 0xFF;
716 if ($mode == "normal") {
717 $v = (127 - $a) / 127;
723 $v2 = (127 - $a2) / 127;
728 } else if ($v2 ==0) {
732 } else if ($v == 0) {
737 $r_ = $r +
(($r2 - $r) * $v2 * (1 - $v));
738 $g_ = $g +
(($g2 - $g) * $v2 * (1 - $v));
739 $b_ = $b +
(($b2 - $b) * $v2 * (1 - $v));
743 } elseif ($mode == "produit" OR $mode == "difference") {
745 if ($mode == "produit") {
749 } else if ($mode == "difference") {
755 $r = max(0, min($r, 255));
756 $g = max(0, min($g, 255));
757 $b = max(0, min($b, 255));
759 $v = (127 - $a) / 127;
765 $v2 = (127 - $a2) / 127;
771 $r_ = $r +
(($r2 - $r) * $v2 * (1 - $v));
772 $g_ = $g +
(($g2 - $g) * $v2 * (1 - $v));
773 $b_ = $b +
(($b2 - $b) * $v2 * (1 - $v));
779 } elseif ($mode == "eclaircir" OR $mode == "obscurcir") {
780 $v = (127 - $a) / 127;
786 $v2 = (127 - $a2) / 127;
792 $r_ = $r +
(($r2 - $r) * $v2 * (1 - $v));
793 $g_ = $g +
(($g2 - $g) * $v2 * (1 - $v));
794 $b_ = $b +
(($b2 - $b) * $v2 * (1 - $v));
797 if ($mode == "eclaircir") {
798 $r_ = max ($r_, $r2);
799 $g_ = max ($g_, $g2);
800 $b_ = max ($b_, $b2);
802 $r_ = min ($r_, $r2);
803 $g_ = min ($g_, $g2);
804 $b_ = min ($b_, $b2);
809 $r_ = $r2 +
1 * ($r - 127);
810 $r_ = max(0, min($r_, 255));
811 $g_ = $g2 +
1 * ($g - 127);
812 $g_ = max(0, min($g_, 255));
813 $b_ = $b2 +
1 * ($b - 127);
814 $b_ = max(0, min($b_, 255));
816 $a_ = $a +
$a2 - round($a*$a2/127);
819 $color = ImageColorAllocateAlpha( $im_, $r_, $g_, $b_ , $a_ );
820 imagesetpixel ($im_, $x, $y, $color);
824 _image_gd_output($im_,$image);
830 $x_dest = largeur($dest);
831 $y_dest = hauteur($dest);
832 return _image_ecrire_tag($image,array('src'=>$dest,'width'=>$x_dest,'height'=>$y_dest));
835 // Passage de l'image en noir et blanc
836 // un noir & blanc "photo" n'est pas "neutre": les composantes de couleur sont
837 // ponderees pour obtenir le niveau de gris;
838 // on peut ici regler cette ponderation en "pour mille"
839 // http://doc.spip.org/@image_nb
840 function image_nb($im, $val_r = 299, $val_g = 587, $val_b = 114)
842 $fonction = array('image_nb', func_get_args());
843 $image = _image_valeurs_trans($im, "nb-$val_r-$val_g-$val_b",false,$fonction);
844 if (!$image) return("");
846 $x_i = $image["largeur"];
847 $y_i = $image["hauteur"];
849 $im = $image["fichier"];
850 $dest = $image["fichier_dest"];
852 $creer = $image["creer"];
855 // resultat plus beau, mais tres lourd
856 // Et: indispensable pour preserver transparence!
859 // Creation de l'image en deux temps
860 // de facon a conserver les GIF transparents
861 $im = $image["fonction_imagecreatefrom"]($im);
862 imagepalettetotruecolor($im);
863 $im_ = imagecreatetruecolor($x_i, $y_i);
864 @imagealphablending
($im_, false);
865 @imagesavealpha
($im_,true);
866 $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 );
867 imagefill ($im_, 0, 0, $color_t);
868 imagecopy($im_, $im, 0, 0, 0, 0, $x_i, $y_i);
870 for ($x = 0; $x < $x_i; $x++
) {
871 for ($y=0; $y < $y_i; $y++
) {
872 $rgb = ImageColorAt($im_, $x, $y);
873 $a = ($rgb >> 24) & 0xFF;
874 $r = ($rgb >> 16) & 0xFF;
875 $g = ($rgb >> 8) & 0xFF;
878 $c = round(($val_r * $r / 1000) +
($val_g * $g / 1000) +
($val_b * $b / 1000));
880 if ($c > 254) $c = 254;
883 $color = ImageColorAllocateAlpha( $im_, $c, $c, $c , $a );
884 imagesetpixel ($im_, $x, $y, $color);
887 _image_gd_output($im_,$image);
892 return _image_ecrire_tag($image,array('src'=>$dest));
895 // http://doc.spip.org/@image_flou
896 function image_flou($im,$niveau=3)
898 // Il s'agit d'une modification du script blur qu'on trouve un peu partout:
899 // + la transparence est geree correctement
900 // + les dimensions de l'image sont augmentees pour flouter les bords
906 array ( 1, 4, 6, 4, 1),
907 array ( 1, 5, 10, 10, 5, 1),
908 array ( 1, 6, 15, 20, 15, 6, 1),
909 array ( 1, 7, 21, 35, 35, 21, 7, 1),
910 array ( 1, 8, 28, 56, 70, 56, 28, 8, 1),
911 array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1),
912 array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1),
913 array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1)
916 $fonction = array('image_flou', func_get_args());
917 $image = _image_valeurs_trans($im, "flou-$niveau", false,$fonction);
918 if (!$image) return("");
920 $x_i = $image["largeur"];
921 $y_i = $image["hauteur"];
922 $sum = pow (2, $niveau);
924 $im = $image["fichier"];
925 $dest = $image["fichier_dest"];
927 $creer = $image["creer"];
930 // resultat plus beau, mais tres lourd
931 // Et: indispensable pour preserver transparence!
934 // Creation de l'image en deux temps
935 // de facon a conserver les GIF transparents
936 $im = $image["fonction_imagecreatefrom"]($im);
937 imagepalettetotruecolor($im);
938 $temp1 = imagecreatetruecolor($x_i+
$niveau, $y_i);
939 $temp2 = imagecreatetruecolor($x_i+
$niveau, $y_i+
$niveau);
941 @imagealphablending
($temp1, false);
942 @imagesavealpha
($temp1,true);
943 @imagealphablending
($temp2, false);
944 @imagesavealpha
($temp2,true);
947 for ($i = 0; $i < $x_i+
$niveau; $i++
) {
948 for ($j=0; $j < $y_i; $j++
) {
955 for ( $k=0 ; $k <= $niveau ; ++
$k ) {
956 $color = imagecolorat($im, $i_ = ($i-$niveau)+
$k , $j);
958 $a = ($color >> 24) & 0xFF;
959 $r = ($color >> 16) & 0xFF;
960 $g = ($color >> 8) & 0xFF;
961 $b = ($color) & 0xFF;
963 if ($i_ < 0 OR $i_ >= $x_i) $a = 127;
965 $coef = $coeffs[$niveau][$k];
967 $ac = ((127-$a) / 127);
971 $sumr +
= $r * $coef * $ac;
972 $sumg +
= $g * $coef * $ac;
973 $sumb +
= $b * $coef * $ac;
977 if ($sum > 0) $color = ImageColorAllocateAlpha ($temp1, $sumr/$sum, $sumg/$sum, $sumb/$sum, $suma/$sum_);
978 else $color = ImageColorAllocateAlpha ($temp1, 255, 255, 255, 127);
979 imagesetpixel($temp1,$i,$j,$color);
983 for ($i = 0; $i < $x_i+
$niveau; $i++
) {
984 for ($j=0; $j < $y_i+
$niveau; $j++
) {
991 for ( $k=0 ; $k <= $niveau ; ++
$k ) {
992 $color = imagecolorat($temp1, $i, $j_ = $j-$niveau+
$k);
993 $a = ($color >> 24) & 0xFF;
994 $r = ($color >> 16) & 0xFF;
995 $g = ($color >> 8) & 0xFF;
996 $b = ($color) & 0xFF;
997 if ($j_ < 0 OR $j_ >= $y_i) $a = 127;
999 $suma +
= $a*$coeffs[$niveau][$k];
1000 $ac = ((127-$a) / 127);
1002 $sumr +
= $r * $coeffs[$niveau][$k] * $ac;
1003 $sumg +
= $g * $coeffs[$niveau][$k] * $ac;
1004 $sumb +
= $b * $coeffs[$niveau][$k] * $ac;
1005 $sum +
= $coeffs[$niveau][$k] * $ac;
1006 $sum_ +
= $coeffs[$niveau][$k];
1009 if ($sum > 0) $color = ImageColorAllocateAlpha ($temp2, $sumr/$sum, $sumg/$sum, $sumb/$sum, $suma/$sum_);
1010 else $color = ImageColorAllocateAlpha ($temp2, 255, 255, 255, 127);
1011 imagesetpixel($temp2,$i,$j,$color);
1015 _image_gd_output($temp2,$image);
1016 imagedestroy($temp1);
1017 imagedestroy($temp2);
1020 return _image_ecrire_tag($image,array('src'=>$dest,'width'=>($x_i+
$niveau),'height'=>($y_i+
$niveau)));
1023 // http://doc.spip.org/@image_RotateBicubic
1024 function image_RotateBicubic($src_img, $angle, $bicubic=0) {
1025 include_spip('filtres/images_lib');
1027 if (round($angle/90)*90 == $angle) {
1029 if (round($angle/180)*180 == $angle) $rot = 180;
1035 // convert degrees to radians
1036 $angle = $angle +
180;
1037 $angle = deg2rad($angle);
1041 $src_x = imagesx($src_img);
1042 $src_y = imagesy($src_img);
1045 $center_x = floor(($src_x-1)/2);
1046 $center_y = floor(($src_y-1)/2);
1048 $cosangle = cos($angle);
1049 $sinangle = sin($angle);
1051 // calculer dimensions en simplifiant angles droits, ce qui evite "floutage"
1052 // des rotations a angle droit
1054 $corners=array(array(0,0), array($src_x,0), array($src_x,$src_y), array(0,$src_y));
1056 foreach($corners as $key=>$value) {
1057 $value[0]-=$center_x; //Translate coords to center for rotation
1058 $value[1]-=$center_y;
1060 $temp[0]=$value[0]*$cosangle+
$value[1]*$sinangle;
1061 $temp[1]=$value[1]*$cosangle-$value[0]*$sinangle;
1062 $corners[$key]=$temp;
1065 $min_x=1000000000000000;
1066 $max_x=-1000000000000000;
1067 $min_y=1000000000000000;
1068 $max_y=-1000000000000000;
1070 foreach($corners as $key => $value) {
1071 if($value[0]<$min_x)
1073 if($value[0]>$max_x)
1076 if($value[1]<$min_y)
1078 if($value[1]>$max_y)
1082 $rotate_width=ceil($max_x-$min_x);
1083 $rotate_height=ceil($max_y-$min_y);
1087 $rotate_height = $src_y;
1088 $rotate_width = $src_x;
1090 $rotate_height = $src_x;
1091 $rotate_width = $src_y;
1097 $rotate=imagecreatetruecolor($rotate_width,$rotate_height);
1098 imagealphablending($rotate, false);
1099 imagesavealpha($rotate, true);
1101 $cosangle = cos($angle);
1102 $sinangle = sin($angle);
1104 // arrondir pour rotations angle droit (car cos et sin dans {-1,0,1})
1106 $cosangle = round($cosangle);
1107 $sinangle = round($sinangle);
1110 $newcenter_x = ($rotate_width-1)/2;
1111 $newcenter_y = ($rotate_height-1)/2;
1114 for ($y = 0; $y < $rotate_height; $y++
) {
1115 for ($x = 0; $x < $rotate_width; $x++
) {
1117 $old_x = ((($newcenter_x-$x) * $cosangle +
($newcenter_y-$y) * $sinangle))
1119 $old_y = ((($newcenter_y-$y) * $cosangle - ($newcenter_x-$x) * $sinangle))
1122 $old_x = ceil($old_x);
1123 $old_y = ceil($old_y);
1125 if ( $old_x >= 0 && $old_x < $src_x
1126 && $old_y >= 0 && $old_y < $src_y ) {
1127 if ($bicubic == true) {
1135 // on prend chaque point, mais on pondere en fonction de la distance
1136 $rgb = ImageColorAt($src_img, $x0, $y0);
1137 $a1 = ($rgb >> 24) & 0xFF;
1138 $r1 = ($rgb >> 16) & 0xFF;
1139 $g1 = ($rgb >> 8) & 0xFF;
1141 $d1 = _image_distance_pixel($xo, $yo, $x0, $y0);
1143 $rgb = ImageColorAt($src_img, $x1, $y0);
1144 $a2 = ($rgb >> 24) & 0xFF;
1145 $r2 = ($rgb >> 16) & 0xFF;
1146 $g2 = ($rgb >> 8) & 0xFF;
1148 $d2 = _image_distance_pixel($xo, $yo, $x1, $y0);
1150 $rgb = ImageColorAt($src_img,$x0, $y1);
1151 $a3 = ($rgb >> 24) & 0xFF;
1152 $r3 = ($rgb >> 16) & 0xFF;
1153 $g3 = ($rgb >> 8) & 0xFF;
1155 $d3 = _image_distance_pixel($xo, $yo, $x0, $y1);
1157 $rgb = ImageColorAt($src_img,$x1, $y1);
1158 $a4 = ($rgb >> 24) & 0xFF;
1159 $r4 = ($rgb >> 16) & 0xFF;
1160 $g4 = ($rgb >> 8) & 0xFF;
1162 $d4 = _image_distance_pixel($xo, $yo, $x1, $y1);
1164 $ac1 = ((127-$a1) / 127);
1165 $ac2 = ((127-$a2) / 127);
1166 $ac3 = ((127-$a3) / 127);
1167 $ac4 = ((127-$a4) / 127);
1169 // limiter impact des couleurs transparentes,
1170 // mais attention tout transp: division par 0
1171 if ($ac1*$d1 +
$ac2*$d2 +
$ac3+
$d3 +
$ac4+
$d4 > 0) {
1172 if ($ac1 > 0) $d1 = $d1 * $ac1;
1173 if ($ac2 > 0) $d2 = $d2 * $ac2;
1174 if ($ac3 > 0) $d3 = $d3 * $ac3;
1175 if ($ac4 > 0) $d4 = $d4 * $ac4;
1178 $tot = $d1 +
$d2 +
$d3 +
$d4;
1180 $r = round((($d1*$r1)+
($d2*$r2)+
($d3*$r3)+
($d4*$r4))/$tot);
1181 $g = round((($d1*$g1+
($d2*$g2)+
$d3*$g3+
$d4*$g4))/$tot);
1182 $b = round((($d1*$b1+
($d2*$b2)+
$d3*$b3+
$d4*$b4))/$tot);
1183 $a = round((($d1*$a1+
($d2*$a2)+
$d3*$a3+
$d4*$a4))/$tot);
1184 $color = imagecolorallocatealpha($src_img, $r,$g,$b,$a);
1187 $color = imagecolorat($src_img, round($old_x), round($old_y));
1191 // this line sets the background colour
1192 $color = imagecolorallocatealpha($src_img, 255, 255, 255, 127);
1194 @imagesetpixel
($rotate, $x, $y, $color);
1200 // permet de faire tourner une image d'un angle quelconque
1201 // la fonction "crop" n'est pas implementee...
1202 // http://doc.spip.org/@image_rotation
1203 function image_rotation($im, $angle, $crop=false)
1205 $fonction = array('image_rotation', func_get_args());
1206 $image = _image_valeurs_trans($im, "rot-$angle-$crop", "png", $fonction);
1207 if (!$image) return("");
1209 $im = $image["fichier"];
1210 $dest = $image["fichier_dest"];
1212 $creer = $image["creer"];
1215 $effectuer_gd = true;
1217 if(is_callable(array('Imagick','rotateImage'))){
1218 $imagick = new Imagick();
1219 $imagick->readImage($im);
1220 $imagick->rotateImage(new ImagickPixel('none'), $angle);
1221 $imagick->writeImage($dest);
1222 $effectuer_gd = false;
1224 else if ($GLOBALS['meta']['image_process'] == "convert") {
1225 if (_CONVERT_COMMAND
!='') {
1226 @define
('_CONVERT_COMMAND', 'convert');
1227 @define
('_ROTATE_COMMAND', _CONVERT_COMMAND
.' -background none -rotate %t %src %dest');
1229 @define
('_ROTATE_COMMAND', '');
1230 if (_ROTATE_COMMAND
!=='') {
1231 $commande = str_replace(
1232 array('%t', '%src', '%dest'),
1235 escapeshellcmd($im),
1236 escapeshellcmd($dest)
1239 spip_log($commande);
1241 if (file_exists($dest)) // precaution
1242 $effectuer_gd = false;
1245 // cette variante genere-t-elle un fond transparent
1246 // dans les coins vide issus de la rotation ?
1247 elseif (function_exists('imagick_rotate')) {
1248 $handle = imagick_readimage ($im);
1249 if ($handle && imagick_isopaqueimage( $handle )) {
1250 imagick_setfillcolor($handle, 'transparent');
1251 imagick_rotate( $handle, $angle);
1252 imagick_writeimage( $handle, $dest);
1253 $effectuer_gd = false;
1256 if ($effectuer_gd) {
1257 // Creation de l'image en deux temps
1258 // de facon a conserver les GIF transparents
1259 $im = $image["fonction_imagecreatefrom"]($im);
1260 imagepalettetotruecolor($im);
1261 $im = image_RotateBicubic($im, $angle, true);
1262 _image_gd_output($im,$image);
1266 list ($src_y,$src_x) = taille_image($dest);
1267 return _image_ecrire_tag($image,array('src'=>$dest,'width'=>$src_x,'height'=>$src_y));
1270 // Permet d'appliquer un filtre php_imagick a une image
1271 // par exemple: [(#LOGO_ARTICLE|image_imagick{imagick_wave,20,60})]
1272 // liste des fonctions: http://www.linux-nantes.org/~fmonnier/doc/imagick/
1273 // http://doc.spip.org/@image_imagick
1274 function image_imagick () {
1275 $tous = func_get_args();
1279 $tous_var = join($tous, "-");
1281 $fonction = array('image_imagick', func_get_args());
1282 $image = _image_valeurs_trans($img, "$tous_var", "png",$fonction);
1283 if (!$image) return("");
1285 $im = $image["fichier"];
1286 $dest = $image["fichier_dest"];
1288 $creer = $image["creer"];
1291 if (function_exists($fonc)) {
1293 $handle = imagick_readimage ($im);
1295 for ($i=2; $i < count($tous); $i++
) $arr[] = $tous[$i];
1296 call_user_func_array($fonc, $arr);
1297 // Creer image dans fichier temporaire, puis renommer vers "bon" fichier
1298 // de facon a eviter time_out pendant creation de l'image definitive
1299 $tmp = preg_replace(",[.]png$,i", "-tmp.png", $dest);
1300 imagick_writeimage( $handle, $tmp);
1301 rename($tmp, $dest);
1302 ecrire_fichier($dest.".src",serialize($image));
1305 list ($src_y,$src_x) = taille_image($dest);
1306 return _image_ecrire_tag($image,array('src'=>$dest,'width'=>$src_x,'height'=>$src_y));
1310 // Permet de rendre une image
1311 // plus claire (gamma > 0)
1312 // ou plus foncee (gamma < 0)
1313 // http://doc.spip.org/@image_gamma
1314 function image_gamma($im, $gamma = 0){
1315 include_spip('filtres/images_lib');
1316 $fonction = array('image_gamma', func_get_args());
1317 $image = _image_valeurs_trans($im, "gamma-$gamma",false,$fonction);
1318 if (!$image) return("");
1320 $x_i = $image["largeur"];
1321 $y_i = $image["hauteur"];
1323 $im = $image["fichier"];
1324 $dest = $image["fichier_dest"];
1326 $creer = $image["creer"];
1329 // Creation de l'image en deux temps
1330 // de facon a conserver les GIF transparents
1331 $im = $image["fonction_imagecreatefrom"]($im);
1332 imagepalettetotruecolor($im);
1333 $im_ = imagecreatetruecolor($x_i, $y_i);
1334 @imagealphablending
($im_, false);
1335 @imagesavealpha
($im_,true);
1336 $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 );
1337 imagefill ($im_, 0, 0, $color_t);
1338 imagecopy($im_, $im, 0, 0, 0, 0, $x_i, $y_i);
1340 for ($x = 0; $x < $x_i; $x++
) {
1341 for ($y=0; $y < $y_i; $y++
) {
1342 $rgb = ImageColorAt($im_, $x, $y);
1343 $a = ($rgb >> 24) & 0xFF;
1344 $r = ($rgb >> 16) & 0xFF;
1345 $g = ($rgb >> 8) & 0xFF;
1348 $r = _image_decale_composante($r, $gamma);
1349 $g = _image_decale_composante($g, $gamma);
1350 $b = _image_decale_composante($b, $gamma);
1352 $color = ImageColorAllocateAlpha( $im_, $r, $g, $b , $a );
1353 imagesetpixel ($im_, $x, $y, $color);
1356 _image_gd_output($im_,$image);
1358 return _image_ecrire_tag($image,array('src'=>$dest));
1361 // Passe l'image en "sepia"
1362 // On peut fixer les valeurs RGB
1363 // de la couleur "complementaire" pour forcer une dominante
1364 //function image_sepia($im, $dr = 137, $dv = 111, $db = 94)
1365 // http://doc.spip.org/@image_sepia
1366 function image_sepia($im, $rgb = "896f5e"){
1367 include_spip('filtres/images_lib');
1369 if (!function_exists("imagecreatetruecolor")) return $im;
1371 $couleurs = _couleur_hex_to_dec($rgb);
1372 $dr= $couleurs["red"];
1373 $dv= $couleurs["green"];
1374 $db= $couleurs["blue"];
1376 $fonction = array('image_sepia', func_get_args());
1377 $image = _image_valeurs_trans($im, "sepia-$dr-$dv-$db",false,$fonction);
1378 if (!$image) return("");
1380 $x_i = $image["largeur"];
1381 $y_i = $image["hauteur"];
1383 $im = $image["fichier"];
1384 $dest = $image["fichier_dest"];
1386 $creer = $image["creer"];
1389 // Creation de l'image en deux temps
1390 // de facon a conserver les GIF transparents
1391 $im = $image["fonction_imagecreatefrom"]($im);
1392 imagepalettetotruecolor($im);
1393 $im_ = imagecreatetruecolor($x_i, $y_i);
1394 @imagealphablending
($im_, false);
1395 @imagesavealpha
($im_,true);
1396 $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 );
1397 imagefill ($im_, 0, 0, $color_t);
1398 imagecopy($im_, $im, 0, 0, 0, 0, $x_i, $y_i);
1400 for ($x = 0; $x < $x_i; $x++
) {
1401 for ($y=0; $y < $y_i; $y++
) {
1402 $rgb = ImageColorAt($im_, $x, $y);
1403 $a = ($rgb >> 24) & 0xFF;
1404 $r = ($rgb >> 16) & 0xFF;
1405 $g = ($rgb >> 8) & 0xFF;
1408 $r = round(.299 * $r +
.587 * $g +
.114 * $b);
1413 $r = _image_decale_composante_127($r, $dr);
1414 $g = _image_decale_composante_127($g, $dv);
1415 $b = _image_decale_composante_127($b, $db);
1417 $color = ImageColorAllocateAlpha( $im_, $r, $g, $b , $a );
1418 imagesetpixel ($im_, $x, $y, $color);
1421 _image_gd_output($im_,$image);
1426 return _image_ecrire_tag($image,array('src'=>$dest));
1430 // Renforcer la nettete d'une image
1431 // http://doc.spip.org/@image_renforcement
1432 function image_renforcement($im, $k=0.5)
1434 $fonction = array('image_flou', func_get_args());
1435 $image = _image_valeurs_trans($im, "renforcement-$k",false,$fonction);
1436 if (!$image) return("");
1438 $x_i = $image["largeur"];
1439 $y_i = $image["hauteur"];
1440 $im = $image["fichier"];
1441 $dest = $image["fichier_dest"];
1442 $creer = $image["creer"];
1445 $im = $image["fonction_imagecreatefrom"]($im);
1446 imagepalettetotruecolor($im);
1447 $im_ = imagecreatetruecolor($x_i, $y_i);
1448 @imagealphablending
($im_, false);
1449 @imagesavealpha
($im_,true);
1450 $color_t = ImageColorAllocateAlpha( $im_, 255, 255, 255 , 127 );
1451 imagefill ($im_, 0, 0, $color_t);
1453 for ($x = 0; $x < $x_i; $x++
) {
1454 for ($y=0; $y < $y_i; $y++
) {
1456 $rgb[1][0]=imagecolorat($im,$x,$y-1);
1457 $rgb[0][1]=imagecolorat($im,$x-1,$y);
1458 $rgb[1][1]=imagecolorat($im,$x,$y);
1459 $rgb[2][1]=imagecolorat($im,$x+
1,$y);
1460 $rgb[1][2]=imagecolorat($im,$x,$y+
1);
1463 if ($x-1 < 0) $rgb[0][1] = $rgb[1][1];
1464 if ($y-1 < 0) $rgb[1][0] = $rgb[1][1];
1465 if ($x+
1 == $x_i) $rgb[2][1] = $rgb[1][1];
1466 if ($y+
1 == $y_i) $rgb[1][2] = $rgb[1][1];
1468 $a = ($rgb[1][1] >> 24) & 0xFF;
1469 $r = -$k *(($rgb[1][0] >> 16) & 0xFF) +
1470 -$k *(($rgb[0][1] >> 16) & 0xFF) +
1471 (1+
4*$k) *(($rgb[1][1] >> 16) & 0xFF) +
1472 -$k *(($rgb[2][1] >> 16) & 0xFF) +
1473 -$k *(($rgb[1][2] >> 16) & 0xFF) ;
1475 $g = -$k *(($rgb[1][0] >> 8) & 0xFF) +
1476 -$k *(($rgb[0][1] >> 8) & 0xFF) +
1477 (1+
4*$k) *(($rgb[1][1] >> 8) & 0xFF) +
1478 -$k *(($rgb[2][1] >> 8) & 0xFF) +
1479 -$k *(($rgb[1][2] >> 8) & 0xFF) ;
1481 $b = -$k *($rgb[1][0] & 0xFF) +
1482 -$k *($rgb[0][1] & 0xFF) +
1483 (1+
4*$k) *($rgb[1][1] & 0xFF) +
1484 -$k *($rgb[2][1] & 0xFF) +
1485 -$k *($rgb[1][2] & 0xFF) ;
1487 $r=min(255,max(0,$r));
1488 $g=min(255,max(0,$g));
1489 $b=min(255,max(0,$b));
1492 $color = ImageColorAllocateAlpha( $im_, $r, $g, $b , $a );
1493 imagesetpixel ($im_, $x, $y, $color);
1496 _image_gd_output($im_,$image);
1499 return _image_ecrire_tag($image,array('src'=>$dest));
1506 // alpha = 0: aucune transparence
1507 // alpha = 127: completement transparent
1509 * Transforme la couleur de fond de l'image en transparence
1510 * Le filtre ne g�re pas la notion de contiguite aux bords, et affectera tous les pixels de l'image dans la couleur visee
1511 * $background_color : couleur cible
1512 * $tolerance : distance L1 dans l'espace RGB des couleur autour de la couleur $background_color pour lequel la transparence sera appliquee
1513 * $alpha : transparence a appliquer pour les pixels de la couleur cibles avec la tolerance ci-dessus
1514 * $coeff_lissage : coeff applique a la tolerance pour determiner la decroissance de la transparence fonction de la distance L1 entre la couleur du pixel et la couleur cible
1517 * @param string $background_color
1518 * @param int $tolerance
1520 * @param float $coeff_lissage
1523 function image_fond_transparent($im, $background_color, $tolerance=12, $alpha = 127, $coeff_lissage=7)
1525 $fonction = array('image_fond_transparent', func_get_args());
1526 $image = _image_valeurs_trans($im, "fond_transparent-$background_color-$tolerance-$coeff_lissage-$alpha", "png", $fonction);
1527 if (!$image) return("");
1529 $x_i = $image["largeur"];
1530 $y_i = $image["hauteur"];
1532 $im = $image["fichier"];
1533 $dest = $image["fichier_dest"];
1535 $creer = $image["creer"];
1537 if (true OR $creer) {
1538 $bg = _couleur_hex_to_dec($background_color);
1540 $bg_g = $bg['green'];
1541 $bg_b = $bg['blue'];
1543 // Creation de l'image en deux temps
1544 // de facon a conserver les GIF transparents
1545 $im = $image["fonction_imagecreatefrom"]($im);
1546 imagepalettetotruecolor($im);
1547 $im2 = imagecreatetruecolor($x_i, $y_i);
1548 @imagealphablending
($im2, false);
1549 @imagesavealpha
($im2,true);
1550 $color_t = ImageColorAllocateAlpha( $im2, 255, 255, 255 , 127 );
1551 imagefill ($im2, 0, 0, $color_t);
1552 imagecopy($im2, $im, 0, 0, 0, 0, $x_i, $y_i);
1554 $im_ = imagecreatetruecolor($x_i, $y_i);
1555 imagealphablending ($im_, FALSE );
1556 imagesavealpha ( $im_, TRUE );
1557 $color_f = ImageColorAllocateAlpha( $im_, 255, 255, 255 , $alpha );
1559 for ($x = 0; $x < $x_i; $x++
) {
1560 for ($y = 0; $y < $y_i; $y++
) {
1561 $rgb = ImageColorAt($im2, $x, $y);
1562 $r = ($rgb >> 16) & 0xFF;
1563 $g = ($rgb >> 8) & 0xFF;
1565 if ((($d=abs($r-$bg_r)+
abs($g-$bg_g)+
abs($b-$bg_b))<=$tolerance)){
1566 imagesetpixel ( $im_, $x, $y, $color_f );
1568 elseif ($tolerance AND $d<=($coeff_lissage+
1)*$tolerance){
1569 $transp = round($alpha*(1-($d-$tolerance)/($coeff_lissage*$tolerance)));
1570 $color_p = ImageColorAllocateAlpha( $im_, $r, $g, $b , $transp);
1571 imagesetpixel ( $im_, $x, $y, $color_p );
1574 imagesetpixel ( $im_, $x, $y, $rgb );
1577 _image_gd_output($im_,$image);
1583 return _image_ecrire_tag($image,array('src'=>$dest));