Script redimenssion d'image petit souci

Fermé
miss_kaya Messages postés 35 Date d'inscription dimanche 17 décembre 2006 Statut Membre Dernière intervention 24 septembre 2010 - Modifié par miss_kaya le 14/05/2010 à 22:41
Alain_42 Messages postés 5361 Date d'inscription dimanche 3 février 2008 Statut Membre Dernière intervention 13 février 2017 - 15 mai 2010 à 11:49
Bonjour,

Merci d'avance pour votre aide cela fais quelques jours que je recherche une solution pour UP des images sur mon serveur cela j'ai réussi, mais je voudrai bien qu'elle se redimensionne automatiquement.

Mon souci avec le script suivant ai qu'il redimenssione juste un coté de l'image et pas la hauteur + la largeur comme je le voudrai, biensur cela va déformer un pe l'image mais cela n'ai pas un souci pour ce que je ve faire ;)

Question suivante comment faire pour que le script préne en compte les dimenssion que je lui donne ?

voici le script il fais 2 fichier en index.php et un config.php

voici l'index.php

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title> .: Maaking.Com :. Upload and Resize Images</title>
<?php
########################################
#      Upload and Resize Images        #
#          with Aspect Ratio           #
#         By: Mohammed Ahmed           #
#           Gaza, Palestine            #
#         Email: m@maaking.com         #
#            Skype: maaking            #
#           Date: 16-May-2006          #
########################################

//load the config file
include("config.php");

//if the for has submittedd
if (isset($_POST['upForm'])){

       $file_type = $_FILES['imgfile']['type'];
       $file_name = $_FILES['imgfile']['name'];
       $file_size = $_FILES['imgfile']['size'];
       $file_tmp = $_FILES['imgfile']['tmp_name'];

       //check if you have selected a file.
       if(!is_uploaded_file($file_tmp)){
          echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit(); //exit the script and don't do anything else.
       }
       //check file extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);
       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit();
       }
       //get the file extension.
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

       //create a random file name
       $rand_name = md5(time());
       $rand_name= rand(0,999999999);
       //get the new width variable.
       $ThumbWidth = $img_thumb_width;

       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list width and height and keep height ratio.
           list($width, $height) = getimagesize($file_tmp);
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //save image
           ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
           //print message
           echo "<br>Image Thumb: <a href=\"$path_thumbs/$rand_name.$file_ext\">$path_thumbs/$rand_name.$file_ext</a>";
        }

        //upload the big image
        move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

        echo "<br>Image Big: <a href=\"$path_big/$rand_name.$file_ext\">$path_big/$rand_name.$file_ext</a>";

        echo "<br><br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

}else{ //if the form hasn't been submitted.

      //print the form
      echo "<script>
      function view_img(img_name){
         document[img_name].src = upForm.imgfile.value;
            document[img_name].width = 150;
      }
      </script>\n\n
      <br><h3>:: Browse an Image to Upload:</h3>\n
      <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\">\n
      <input type=\"file\" name=\"imgfile\" onchange=\"javascript:view_img('img_vv');\"> <img src='' name='img_vv' width='0'><br>\n
      Image width will resize to <b>$img_thumb_width</b> with height ratio.
      <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n
      </form>
      <a href=\"view_gallery.php\">View Images</a>";

}


//print copyright ;)
echo"<p align=\"right\"><br>Script by: <a href=\"http://www.maaking.com\">maaking.com</a></p>
</body>
</html>";

?>






Et voici le fichier config.php

<?php
########################################
#      Upload and Resize Images        #
#          with Aspect Ratio           #
#         By: Mohammed Ahmed           #
#           Gaza, Palestine            #
#         Email: m@maaking.com         #
#            Skype: maaking            #
#           Date: 16-May-2006          #
########################################
//path where to store images
$path_thumbs = "images/thumbs";
$path_big = "images/big";

//the new width of the resized image.
$img_thumb_width = 150; // in pixcel

$extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
//allowed Extensions
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");


//check if folders are Writable or not
//please CHOMD them 777
if (!is_writeable($path_thumbs)){
   die ("Error: The directory <b>($path_thumbs)</b> is NOT writable");
}
if (!is_writeable($path_big)){
    die ("Error: The directory <b>($path_big)</b> is NOT writable");
}

?>




J'ai testé pas mal de chose mais rien n'y fais j'ai changé ca dan sle fichier config.php

//the new width of the resized image.
$img_thumb_width = 177; // in pixcel
$img_thumb_height = 133; // in pixcel

Mais il le prend pas en compte méme apré l'avoir rajouté dans l'index.php si l'image fais 1000 / 3000 il va me faire la hauteur mais pas la largeur :(


Merci encore pour votre aide.
A voir également:

3 réponses

Alain_42 Messages postés 5361 Date d'inscription dimanche 3 février 2008 Statut Membre Dernière intervention 13 février 2017 894
14 mai 2010 à 14:37
essayes en apportant ces modifs marquées //la

//get the new width variable. 
       $ThumbWidth = $img_thumb_width; 
			$ThumbHeight = $img_thumb_height;  ///la
       //keep image type 
       if($file_size){ 
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ 
               $new_img = imagecreatefromjpeg($file_tmp); 
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){ 
               $new_img = imagecreatefrompng($file_tmp); 
           }elseif($file_type == "image/gif"){ 
               $new_img = imagecreatefromgif($file_tmp); 
           } 
           //list width and height and keep height ratio. 
           list($width, $height) = getimagesize($file_tmp); 
           $imgratio=$width/$height; 
           if ($imgratio>1){ 
              $newwidth = $ThumbWidth; 
              $newheight = $ThumbHeight/$imgratio; //la
           } 
0
miss_kaya Messages postés 35 Date d'inscription dimanche 17 décembre 2006 Statut Membre Dernière intervention 24 septembre 2010 1
14 mai 2010 à 22:48
Enfait il me dit ca comme érreure, quand j'envois une photo si je fais la modif que tu ma donné , je sais pas si ca pe t'aider ?

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /index.php on line 71

Warning: imagecopyresized(): supplied argument is not a valid Image resource in /index.php on line 75

Warning: imagejpeg(): supplied argument is not a valid Image resource in /index.php on line 77

Warning: imagedestroy(): supplied argument is not a valid Image resource in /index.php on line 78



Je ne comprend pas tres bien la

help me please ;) merci encore
0
Alain_42 Messages postés 5361 Date d'inscription dimanche 3 février 2008 Statut Membre Dernière intervention 13 février 2017 894
15 mai 2010 à 11:49
ne tiens pas compte des deux modifs précédentes que je t'ai fait faire par contre:

remplaces:

//function for resize image.
if (function_exists(imagecreatetruecolor)){

par

//function for resize image.
           if (function_exists('imagecreatetruecolor')){


qqs guillements ça change tout !
0