Précédent   Forum des professionnels en informatique > PHP > Bibliothèques et frameworks > Images > GD
GD Forum d'entraide pour l'extension GD permettant de manipuler des images en PHP. Avant de poster -> tutoriels GD
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 23/11/2011, 10h36   #1
Candidat au titre de Membre du Club
 
Inscription : août 2008
Messages : 92
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 92
Points : 14
Points : 14
Par défaut 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 :
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 !';  
	 }
 
}
link.80 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/11/2011, 10h57   #2
Modérateur
 
Inscription : septembre 2010
Messages : 7 169
Détails du profil
Informations forums :
Inscription : septembre 2010
Messages : 7 169
Points : 8 543
Points : 8 543
ç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
__________________
http://blog.stealth35.com/
stealth35 est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/11/2011, 12h09   #3
Candidat au titre de Membre du Club
 
Inscription : août 2008
Messages : 92
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 92
Points : 14
Points : 14
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 :
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);
}
link.80 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 25/11/2011, 14h26   #4
Rédacteur/Modérateur
 
Avatar de Thes32
 
Homme
Développeur Web
Inscription : décembre 2006
Messages : 2 335
Détails du profil
Informations personnelles :
Sexe : Homme

Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : décembre 2006
Messages : 2 335
Points : 3 774
Points : 3 774
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/
__________________
Développeur | Zend Certified Engineer

Étapes Pour mieux se servir du forum:
1. Commencez par lire les cours et tutoriels ;
2. Faites une recherche;
3. Faites un post si rien trouvé dans les deux étapes précédentes en respectant les règles;

Nix>_Rien n'est plus pratique que la théorie
Thes32 est déconnecté   Envoyer un message privé Réponse avec citation 20
Vieux 26/11/2011, 11h38   #5
Candidat au titre de Membre du Club
 
Inscription : août 2008
Messages : 92
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 92
Points : 14
Points : 14
merci !!!
link.80 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 14h34.


 
 
 
 
Partenaires

Hébergement Web