0/5 (17 avis)
Vue 75 292 fois - Téléchargée 1 982 fois
<?php $action = $_POST['action']; //fonction pour le calcul du temps d'affichage function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } $debut = getmicrotime(); //generateur de chaine de caractere aleatoire, servant a l'attibution des nom des images function alea_string($caract_max = "10"){ $chaine=""; $caract = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); for ($i=0; $i < $caract_max; $i++) $chaine .= $caract[rand(0, count($caract)-1)]; return $chaine; } // fonction de comparaison function comparaison_img($img1, $img2){ $im1 = ImageCreateFromPng($img1); list($width_img1,$height_img1) = getimagesize($img1); $im2 = ImageCreateFromPng($img2); list($width_img2,$height_img2) = getimagesize($img2); $width = ($width_img1 > $width_img2)?$width_img1:$width_img2; $height = ($height_img1 > $height_img2)?$height_img1:$height_img2; $image_resultat_1 = imagecreatetruecolor($width,$height); $image_resultat_2 = imagecreatetruecolor($width,$height); $diff_totales = 0; for ($x=0;$x<$width;$x++) { for ($y=0;$y<$height;$y++) { $rgb_img1 = ImageColorAt($im1, $x, $y); $r_img1 = ($rgb_img1 >> "16") & 0xFF; $g_img1 = ($rgb_img1 >> "8") & 0xFF; $b_img1 = $rgb_img1 & 0xFF; $rgb_img2 = ImageColorAt($im2, $x, $y); $r_img2 = ($rgb_img2 >> "16") & 0xFF; $g_img2 = ($rgb_img2 >> "8") & 0xFF; $b_img2 = $rgb_img2 & 0xFF; $r = 255 - abs($r_img1 - $r_img2) ; $g = 255 - abs($g_img1 - $g_img2) ; $b = 255 - abs($b_img1 - $b_img2) ; //resultat 1 : image par gradiant $color_resultat_1 = imagecolorallocate($image_resultat_1,$r,$g,$b); imagesetpixel($image_resultat_1, $x, $y, $color_resultat_1); //resultat 2 : image N/B des différences if($r_img1 != $r_img2 or $g_img1 != $g_img2 or $b_img1 != $b_img2) { $color_resultat_2 = imagecolorallocate($image_resultat_2, "0", "0", "0"); $diff_totales++; }else $color_resultat_2 = imagecolorallocate($image_resultat_2, "255", "255", "255"); imagesetpixel($image_resultat_2, $x, $y, $color_resultat_2); } } // génération d'un nom pour les images de resultat $nom_img_resultat = alea_string(); //repertoir de stockage des images de resultat: $rep_img_result = "images/"; imagepng($image_resultat_1, $rep_img_result . $nom_img_resultat.'_1.png'); imagepng($image_resultat_2, $rep_img_result . $nom_img_resultat.'_2.png'); $return = array( "resultat_1" => $rep_img_result . $nom_img_resultat.'_1.png', "resultat_2" => $rep_img_result . $nom_img_resultat.'_2.png', "diff" => $diff_totales ); return $return; } //systeme d'upload switch ($action){ case "upload": // Repertoire cible $target = "upload/"; // Taille max en octets du fichier $max_size = "1000000"; // Largeur max de l'image en pixels $width_max = "200"; // Hauteur max de l'image en pixels $height_max = "200"; $extensions_ok = array("jpg","gif","png","jpeg"); $error_1 = 0; $error_2 = 0; $nom_fichier_enregister = alea_string(); for($nb_img = 1; $nb_img <= 2; $nb_img++){ ${'nom_file_' . $nb_img} = $_FILES['image_'.$nb_img]['name']; ${'taille_' . $nb_img} = $_FILES['image_'.$nb_img]['size']; ${'tmp_' . $nb_img} = $_FILES['image_'.$nb_img]['tmp_name']; ${'chemin_' . $nb_img} = $target.$nom_fichier_enregister.'_'.$nb_img; $extension = substr(${'nom_file_' . $nb_img},-3); if(${'nom_file_' . $nb_img}) { if(in_array(strtolower($extension),$extensions_ok)) { $infos_img = getimagesize(${'tmp_' . $nb_img} ); if(($infos_img[0] <= $width_max) && ($infos_img[1] <= $height_max) && (${'taille_' . $nb_img} <= $max_size)) { if(move_uploaded_file(${'tmp_' . $nb_img},${'chemin_' . $nb_img})) { echo '<p>Image '.$nb_img.' uploadée avec succès !</p>'; echo '<ul><li>Fichier : '.${'nom_file_' . $nb_img}.'</li>'; echo '<li>Taille : '.${'taille_' . $nb_img}.' Octets</li>'; echo '<li>Largeur : '.$infos_img['0'].' px</li>'; echo '<li>Hauteur : '.$infos_img['1'].' px</li></ul>'; } else { echo '<p>Problème lors de l\'upload de l\'image '.$nb_img.' !</p>'; ${'error'.$nb_img} = 1; } } else { echo '<p>Erreur dans les dimensions ou taille de l\'image '.$nb_img.'!</p>'; ${'error'.$nb_img} = 1; } }else { echo '<p>Votre image '.$nb_img.' ne comporte pas une extension valide !</p>'; ${'error'.$nb_img} = 1; } } else { echo '<p>Le champ du formulaire de l\image '.$nb_img.' est vide !</p>'; ${'error'.$nb_img} = 1; } } if ($error_1 == 0 && $error_2 == 0) echo "<br /><form action=\"". $PHP_SELF ."\" method=\"post\" name=\"comparer\"><input name=\"action\" type=\"hidden\" value=\"compare\"><input name=\"img1\" type=\"hidden\" value=\"".$nom_file_1."\"><input name=\"img2\" type=\"hidden\" value=\"".$nom_file_2."\"><input name=\"\" type=\"submit\" value=\"Comparer les deux images\"></form><br />"; else echo "<br /><form action=\"". $PHP_SELF ."\" method=\"post\" name=\"error\"><input name=\"\" type=\"submit\" value=\"Une erreur est survenu durant le traitement d'une des images. Veuillez recommencer\"></form><br />"; break; //appel de la fonction de comparaison case "compare": $comparaison_img1_img2 = comparaison_img($_POST['img1'], $_POST['img2']); echo "<p> Image d'origine 1 : <br /> <img src=\"".$_POST['img1']."\" /><br /> </p> <hr> <p> Image d'origine 2 : <br /> <img src=\"".$_POST['img2']."\" /><br /><br /> </p> <p> <h3>Résultat d'analyse</h3> <fieldset><legend>Resultat 1: Image par gradients de difference</legend> <img src=\"".$comparaison_img1_img2['resultat_1']."\" /> </fieldset> <fieldset><legend>Resultat 2: Image Noir et Blanc des différences</legend> <img src=\"".$comparaison_img1_img2['resultat_2']."\"/> </fieldset> <br /> Différences comptabilisée: ".$comparaison_img1_img2['diff']." </p>"; break; //defaut, formulaire d'upload des images default: echo "<form enctype=\"multipart/form-data\" action=\"". $PHP_SELF ."\" method=\"POST\"> <input type=\"hidden\" name=\"action\" value=\"upload\"> <p>Envoyer l'image 1 :</p> <input name=\"image_1\" type=\"file\"> <p>Envoyer l'image 2 :</p> <input name=\"image_2\" type=\"file\"><br /> <input type=\"submit\" value=\"Uploader\"> </form>"; } //affichage du temps de génération de la page echo "<div align=\"center\">Page générée en ".round( getmicrotime() - $debut, 3) ." secondes</div>"; ?>
6 oct. 2012 à 10:44
14 avril 2011 à 11:36
3 févr. 2010 à 15:43
j ai le message suivant
Fatal error: Call to undefined function ImageCreateFromPng() in -------index.php on line 24
Les images se télécharge correctement mais analyse point ??
Quel le problème?
Merci pour la réponse
13 nov. 2006 à 18:12
pour infos, tu peux utilisé getimagesize() pour testé le type d'image (jpg, png....) pour etre certains du format de l'image.
list($width, $height, $type, $image_bal) = getimagesize($img);
Si non, quelle application peut t'on donné avec ce type de programme?
31 oct. 2006 à 18:29
> Comme un coque en patte ^^ Merci ;-)
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.