IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[GD] Redimensionnement d'images vignette en PHP


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Août 2008
    Messages
    112
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Août 2008
    Messages : 112
    Points : 48
    Points
    48
    Par défaut [GD] Redimensionnement d'images vignette en PHP
    Bonjour,

    Je me suis crée un petit script de redimensionnement et création de vignettes en m'inspirant de divers tutos et codes trouvés sur internet.

    Le script fonctionne à deux exceptions :

    1. Le script redimensionne comme il faut sur la largeur maximum mais ne tient pas en compte de la hauteur max renseignée.

    2. Lorsque j'upload une image carrée (hauteur et largeur identique), l'image est déformée.

    J'ai vraiment de la peine a trouver pourquoi. ça serait vraiment sympa si quelqu’un pouvait me donner un coup de main et aussi me dire si le code vous semble correctement écrit et optimisé.

    D'avance un grand merci pour votre aide.
    Salutations à tous


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    if(is_uploaded_file($_FILES["upFile"]['tmp_name'])){
     
    	  $width = 120; // Largeur maximum vignette
    	  $height = 120; // Hauteur maximum vignette
     
    	  $width2 = 800; // Largeur maximum image
    	  $height2 = 200; // Hauteur maximum image
     
    	  $file=$_FILES["upFile"]['tmp_name'];
     
    	  $folder_big = 'upload/'; //Dossier d'enregistrement
    	  $taille_maxi = 10000000; //Taille max  		
    	  $taille = $_FILES['upFile']['size'];
    	  $extension = strrchr($_FILES['upFile']['name'], '.'); 
    	  $extensions = array('.jpg','.JPG','.png','.PNG','.gif','.GIF');
     
    	  if(!in_array($extension, $extensions)){
    	   alert("Veuillez choisir un fichier au format .jpg, .png ou .gif"); 
    	   exit;
    	  }
    	  elseif($taille>$taille_maxi){
    	   alert("Fichier trop volumineux"); 
    	   exit;
    	  }
    	  else{
    				list($width_orig, $height_orig, $image_type) = getimagesize($file);
    				list($width_orig2, $height_orig2, $image_type2) = getimagesize($file);
     
    				//On garde l'échelle de l'image VIGNETTE
    				if (($width_orig > $height_orig) && ($width_orig > $width))
    					$height = (int) (($width / $width_orig) * $height_orig);
    				elseif($height_orig > $height)
    					$height = $height;
    				else
    					$height = $height_orig;
     
    				if (($height_orig > $width_orig) && ($height_orig > $height))
    					$width = (int) (($height / $height_orig) * $width_orig);
    				elseif($width_orig > $width)
    					$width = $width;
    				else
    					$width = $width_orig;
     
    				//On garde l'échelle de l'image BIG
    				if (($width_orig2 > $height_orig2) && ($width_orig2 > $width2))
    					$height2 = (int) (($width2 / $width_orig2) * $height_orig2);
    				elseif($height_orig2 > $height2)
    					$height2 = $height2;
    				else
    					$height2 = $height_orig2;
     
    				if (($height_orig2 > $width_orig2) && ($height_orig2 > $height2))
    					$width2 = (int) (($height2 / $height_orig2) * $width_orig2);
    				elseif($width_orig2 > $width2)
    					$width2 = $width2;
    				else
    					$width2 = $width_orig2;
     
    				//Création des miniatures avec leur fond respectif
    				$image_p = imagecreatetruecolor($width, $height);
    				$image_b = imagecreatetruecolor($width2, $height2);
     
    				$ext = strrchr($_FILES["upFile"]['name'], '.'); 
     
    				if(($ext == '.jpeg') || ($ext == '.jpg') || ($ext == '.JPEG') || ($ext == '.JPG')){
    					$file_ok = imagecreatefromjpeg($file);
    					$file_ok2 = imagecreatefromjpeg($file);
    				}
    				elseif ($ext == '.png') {
    					$file_ok = imagecreatefrompng($file);
    					$file_ok2 = imagecreatefrompng($file);
    					//Fond transparent (pour les png avec transparence)
    					imagesavealpha($image_p, true);
    					$trans_color = imagecolorallocatealpha($image_p, 0, 0, 0, 127);
    					imagefill($image_p, 0, 0, $trans_color);
    				} 
    				elseif ($ext == '.gif') {
    					$file_ok = imagecreatefromgif($file);
    					$file_ok2 = imagecreatefromgif($file);
    					//Fond transparent (pour les gifs avec transparence)
    					$red = rand(0,255);
    					$green = rand(0,255);
    					$blue = rand(0,255);
    					$transparent = imagecolorallocate($image_p, $red, $green, $blue);
    					imagefill($image_p, 0, 0, $transparent);
    					imagecolortransparent($image_p, $transparent);
    					imagetruecolortopalette($image_p, false, 256);
     
    					$transparent2 = imagecolorallocate($image_b, $red, $green, $blue);
    					imagefill($image_b, 0, 0, $transparent2);
    					imagecolortransparent($image_b, $transparent2);
    					imagetruecolortopalette($image_b, false, 256);	
    				}
     
    				imagecopyresampled($image_p, $file_ok, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
    				imagecopyresampled($image_b, $file_ok2, 0, 0, 0, 0, $width2, $height2, $width_orig2, $height_orig2);
     
    				$file = date("Ymd-His").$extension;
    				$file2 = 'big-'.$file;
     
    				//Ecriture de l'image
    				if(!is_writeable(dirname($folder_big)))
    					echo 'Impossible d\'enregistrer l\'image dans le dossier';
     
    				else {
    					if ($ext == '.png'){
    						imagepng($image_p, $folder_big.$file, 9);
    						imagepng($image_b, $folder_big.$file2, 9);
    					}
    					elseif ($ext == '.gif') {
    						imagegif($image_p, $folder_big.$file, 100);
    						imagegif($image_b, $folder_big.$file2, 100);
    					}
    					else {
    						imagejpeg($image_p, $folder_big.$file, 80);
    						imagejpeg($image_b, $folder_big.$file2, 80);
    					}
     
    				}		
    				imagedestroy($image_p);
    				imagedestroy($file_ok);
     
    				imagedestroy($image_b);
    				imagedestroy($file_ok2);
     
    				echo 'Upload effectué avec succès !';  
    	 }
     
    }

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 727
    Points
    10 727
    Par défaut
    ça me semble bien compliquer,je te conseil de séparé ton code en plusieurs partie, déjà créer un fonction qui redimentionne les image, ça t'evitera de remtere deux fois la meme chose

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Août 2008
    Messages
    112
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Août 2008
    Messages : 112
    Points : 48
    Points
    48
    Par défaut
    hello stealth35,

    merci pour la réponse, tu as tout à fait raison, c'est vrai que c'était pas très logique de tout dupliquer pour créer la vignette. J'ai des connaissances assez limitées en programmation objet mais j'ai quand même essayé de faire une fonction et d'optimiser un peu le code, dis moi ce que tu en penses :

    (du coup autre question, dans ma fonction je modifies le nom de mon image : $file = $name.date("Ymd-His").$extension; comment faire sortir de ma fonction à la fois $affichage et la nouvelle variable $file qui contient le nouveau nom de l'image ?)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
     
    if(is_uploaded_file($_FILES["upFile"]['tmp_name'])){
     
    	 function redim($width,$height,$name,$file,$extension,$taille,$folder_big,$taille_maxi){
    		 	  $affichage='';
     
    			  $extensions = array('.jpg','.JPG','.png','.PNG','.gif','.GIF');
     
    			  if(!in_array($extension, $extensions)){
    			   alert("Veuillez choisir un fichier au format .jpg, .png ou .gif"); 
    			   exit;
    			  }
    			  elseif($taille>$taille_maxi){
    			   alert("Fichier trop volumineux"); 
    			   exit;
    			  }
    			  else{
     
    				list($width_orig, $height_orig, $image_type) = getimagesize($file);
     
    				//On garde l'échelle de l'image VIGNETTE
    				if (($width_orig > $height_orig) && ($width_orig > $width))
    					$height = (int) (($width / $width_orig) * $height_orig);
    				elseif($height_orig > $height)
    					$height = $height;
    				else
    					$height = $height_orig;
     
    				if (($height_orig > $width_orig) && ($height_orig > $height))
    					$width = (int) (($height / $height_orig) * $width_orig);
    				elseif($width_orig > $width)
    					$width = $width;
    				else
    					$width = $width_orig;
     
    				//Création des miniatures avec leur fond respectif
    				$image_p = imagecreatetruecolor($width, $height);
     
    				$ext = strrchr($_FILES["upFile"]['name'], '.'); 
     
    				if(($ext == '.jpeg') || ($ext == '.jpg') || ($ext == '.JPEG') || ($ext == '.JPG')){
    					$file_ok = imagecreatefromjpeg($file);
    				}
    				elseif ($ext == '.png') {
    					$file_ok = imagecreatefrompng($file);
    					//Fond transparent (pour les png avec transparence)
    					imagesavealpha($image_p, true);
    					$trans_color = imagecolorallocatealpha($image_p, 0, 0, 0, 127);
    					imagefill($image_p, 0, 0, $trans_color);
    				} 
    				elseif ($ext == '.gif') {
    					$file_ok = imagecreatefromgif($file);
    					//Fond transparent (pour les gifs avec transparence)
    					$red = rand(0,255);
    					$green = rand(0,255);
    					$blue = rand(0,255);
    					$transparent = imagecolorallocate($image_p, $red, $green, $blue);
    					imagefill($image_p, 0, 0, $transparent);
    					imagecolortransparent($image_p, $transparent);
    					imagetruecolortopalette($image_p, false, 256);
     
    				}
     
    				imagecopyresampled($image_p, $file_ok, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
     
    				$file = $name.date("Ymd-His").$extension;
     
    				//Ecriture de l'image
    				if(!is_writeable(dirname($folder_big)))
    					$affichage.='Impossible d\'enregistrer l\'image dans le dossier';
     
    				else {
    					if ($ext == '.png'){
    						imagepng($image_p, $folder_big.$file, 9);
    					}
    					elseif ($ext == '.gif') {
    						imagegif($image_p, $folder_big.$file, 100);
    					}
    					else {
    						imagejpeg($image_p, $folder_big.$file, 80);
    					}
     
    				}		
    				imagedestroy($image_p);
    				imagedestroy($file_ok);
     
    				$affichage.='Upload effectué avec succès !';
    			}
    	  return($affichage);
    	}  
        echo redim(120,120,'',$_FILES["upFile"]['tmp_name'],strrchr($_FILES['upFile']['name'], '.'),$_FILES['upFile']['size'],'upload/article/',10000000);
        echo redim(500,200,'big_',$_FILES["upFile"]['tmp_name'],strrchr($_FILES['upFile']['name'], '.'),$_FILES['upFile']['size'],'upload/article/',10000000);
    }

  4. #4
    Expert confirmé
    Avatar de Thes32
    Homme Profil pro
    Développeur PHP, .Net, T-SQL
    Inscrit en
    Décembre 2006
    Messages
    2 379
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Développeur PHP, .Net, T-SQL

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 379
    Points : 4 853
    Points
    4 853
    Par défaut
    Salut,

    certains membres on travaillés sur le sujet :

    - tu peux réutilisez cette classe de ThomasR http://www.developpez.net/forums/d14...e/#post4566977
    - ABCIWEB a également proposé http://www.developpez.net/forums/d10...mensionnement/
    - Tu peux aussi regarder le système de jreaux62 http://www.developpez.net/forums/d73...uvelles-photo/

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Août 2008
    Messages
    112
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Août 2008
    Messages : 112
    Points : 48
    Points
    48
    Par défaut
    merci !!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Adapter le 2 scripts php ,pour un redimensionnement d'image
    Par artandsports dans le forum Langage
    Réponses: 2
    Dernier message: 02/10/2009, 18h03
  2. Redimensionnement d'image en PHP
    Par Casio dans le forum Langage
    Réponses: 23
    Dernier message: 09/09/2008, 14h57
  3. [PHP-JS] pb avec un redimensionnement d'image
    Par jexl dans le forum Langage
    Réponses: 2
    Dernier message: 06/10/2006, 09h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo