Page 1 sur 1

PHP upload photo

Posté : jeu. 14 juil. 2022 11:55
par gismin
Bonjour,
J'ai un code pour uploader une photo à partir d'un formulaire puis l'afficher.
Çà marche pour des photos mais pas toutes, certaines se retrouvent à 90° à l'affichage,
et pour celle-ci quand je fais un " echo $img_size[0] et echo $img_size[1] " la largeur ($img_size[0]) est supérieure à la hauteur ($img_size[0]) .
J'ai essayé de les tourner avec "imagerotate" mais c pas concluant.
Pouvez vous m'aider ?
Code : Tout sélectionner
if (isset($_FILES['photo1']))  {  
 	 $ph1_tmp = $_FILES['photo1']['tmp_name']; 
	     $ph1 = $_FILES['photo1']['name'];  
  move_uploaded_file($ph1_tmp,'../imgs-temp/'.$ph1); 	 
  $im1="../imgs-temp/$ph1";     
                   // Début des véfications de sérité.
             $exts=array('jpg','gif','png','peg');
              $ext=strtolower(substr($im1,-3));   //strtolower :  en minuscule
                if(!in_array($ext, $exts))  
                {  $erreur1 = 'Vous devez uploader un fichier de type png, gif, jpg, jpeg ';   }
	    
	      if  ($ext=="jpg" ) 
             {$img1 = imagecreatefromjpeg($im1);}
          else if ($ext=="png") 
	         {$img1 = imagecreatefrompng($im1);}
          else if ($ext=="gif") 
	         {$img1 = imagecreatefromgif($im1);}
		  else if ($ext==0) {$img1=0;}
          else  
         { echo 'image non valide ! ';     $img1= false;     }
            //traitement image
          if($img1!=false)
         {$img_size=getimagesize($im1); 
          $img_width=300;						
         if($img_size[0]<=$img_width)						 
         {  $img_fin1=$img1;  }
         else
           {   
	       $new_height =($img_size[1]/$img_size[0])*$img_width;		   
	       $img_fin1=imagecreatetruecolor($img_width,$new_height) or die ("Erreur");	
           imagecopyresampled($img_fin1,$img1,0,0,0,0,$img_width,$new_height,$img_size[0],$img_size[1]);     		
		   } 

     $img_fin1= md5(uniqid(rand(), true)); 
         // enregistrement dans fichier imgs 
          imagejpeg($img1, '../imgsC/'.$img_fin1.'.'. $ext, 80); 
		  $img_fin1="$img_fin1.$ext";
  
 echo('<img src="../imgsC/'.$img_fin1 .'"  width="160" border="2">');   
 imagedestroy($img1);
 unlink('../imgs-temp/'.$ph1);
 
 }