function image_niveaux_gris_auto($im, $limite=1000) { // $limite=1000: les nuances min et max representent 0,1% du total $image = valeurs_image_trans($im, "niveaux_gris_auto-$limite"); if (!$image) return(""); $x_i = $image["largeur"]; $y_i = $image["hauteur"]; $im = $image["fichier"]; $dest = $image["fichier_dest"]; $creer = $image["creer"]; if ($creer) { $im = $image["fonction_imagecreatefrom"]($im); // Calculer les poids des differentes nuances for ($x = 0; $x < $x_i; $x++) { for ($y=0; $y < $y_i; $y++) { $rgb = ImageColorAt($im, $x, $y); $a = ($rgb >> 24) & 0xFF; $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $a = (127-$a) / 127; $a=1; $gris = round($a*($r+$g+$b) / 3); $r = round($a*$r); $g = round($a*$g); $b = round($a*$b); $val_gris[$gris] ++; } } $total = $x_i * $y_i; for ($bas = 0; $somme_bas < $total/$limite; $bas++) { $somme_bas += $val_gris[$bas]; } for ($haut = 255; $somme_haut < $total/$limite ; $haut--) { $somme_haut += $val_gris[$haut]; } $courbe[0] = 0; $courbe[255] = 255; $courbe[$bas] = 0; $courbe[$haut] = 255; // Calculer le tableau des correspondances ksort($courbe); while (list($key, $val) = each($courbe)) { if ($key > 0) { $key1 = $key_old; $val1 = $val_old; $prop = ($val - $val1) / ($key-$key1); for ($i = $key1; $i < $key; $i++) { $valeur = round($prop * ($i - $key1) + $val1); $courbe[$i] = $valeur; } $key_old = $key; $val_old = $val; } else { $key_old = $key; $val_old = $val; } } // Appliquer les correspondances $im2 = imagecreatetruecolor($x_i, $y_i); @imagealphablending($im2, false); @imagesavealpha($im2,true); $color_t = ImageColorAllocateAlpha( $im2, 255, 255, 255 , 0 ); imagefill ($im2, 0, 0, $color_t); for ($x = 0; $x < $x_i; $x++) { for ($y=0; $y < $y_i; $y++) { $rgb = ImageColorAt($im, $x, $y); $a = ($rgb >> 24) & 0xFF; $r = ($rgb >> 16) & 0xFF; $v = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $r = $courbe[$r]; $v = $courbe[$v]; $b = $courbe[$b]; $color = ImageColorAllocateAlpha( $im2, $r, $v, $b , $a ); imagesetpixel ($im2, $x, $y, $color); } } $image["fonction_image"]($im2, "$dest"); imagedestroy($im2); imagedestroy($im); } $class = $image["class"]; if (strlen($class) > 1) $tags=" class='$class'"; $tags = "$tags alt='".$image["alt"]."'"; $style = $image["style"]; if (strlen($style) > 1) $tags="$tags style='$style'"; return ""; }