Précédent   Forum des professionnels en informatique > PHP > Langage > Fichiers
Fichiers Forum d'entraide sur les fichiers avec PHP. Avant de poster -> FAQ fichiers et Sources fichiers
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 30/01/2007, 19h25   #1
Membre habitué
 
Inscription : mai 2002
Messages : 475
Détails du profil
Informations forums :
Inscription : mai 2002
Messages : 475
Points : 105
Points : 105
Par défaut [Upload] Redimensionnement d'image uploader

Bonjour

je suis sur ce script depuis un bon moment
afin de le faire fonctionner comme je le veux, c'est dire :
uploader une image et la redimmensionner
avec ce script l'image uploadé garde sa taille originale sur le serveur (ce que je ne veux pas)
A l'affichage pas de probleme, elle s'afffiche bien aux dimensions voulue
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
if($_GET['pic']){
	$img = new img('upload/'.$_GET['pic']);
	//$destination = 'upload/'.$_GET['pic'];
	//$img->img($destination);
	$img->resize();
	$img->show();
}
 
class img {
 
	var $image = '';
	var $temp = '';
 
	function img($sourceFile){
		if(file_exists($sourceFile)){
			$this->image = ImageCreateFromJPEG($sourceFile);
		} else {
			$this->errorHandler();
		}
		return;
	}
 
	function resize($width = 220, $height = 135, $aspectradio = true){
		$o_wd = imagesx($this->image);
		$o_ht = imagesy($this->image);
		if(isset($aspectradio)&&$aspectradio) {
			$w = round($o_wd * $height / $o_ht);
			$h = round($o_ht * $width / $o_wd);
			if(($height-$h)<($width-$w)){
				$width =& $w;
			} else {
				$height =& $h;
			}
		}
		$this->temp = imageCreateTrueColor($width,$height);
		imageCopyResampled($this->temp, $this->image,
		0, 0, 0, 0, $width, $height, $o_wd, $o_ht);
		//imagejpeg($this->temp, $destination);
		$this->sync();
		return;
	}
 
	function sync(){
		$this->image =& $this->temp;
		unset($this->temp);
		$this->temp = '';
		return;
	}
 
	function show(){
		$this->_sendHeader();
		ImageJPEG($this->image);
		return;
	}
 
	function _sendHeader(){
		header('Content-Type: image/jpeg');
	}
 
	function errorHandler(){
		echo "error";
		exit();
	}
 
	function store($file){
		ImageJPEG($this->image,$file);
		return;
	}
 
	function watermark($pngImage, $left = 0, $top = 0){
		ImageAlphaBlending($this->image, true);
		$layer = ImageCreateFromPNG($pngImage); 
		$logoW = ImageSX($layer); 
		$logoH = ImageSY($layer); 
		ImageCopy($this->image, $layer, $left, $top, 0, 0, $logoW, $logoH); 
	}
}
// en commentaire : tests non concluants

Merci de votre aide
nicerico est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/02/2007, 11h05   #2
Membre expérimenté
 
Avatar de mathieugamin
 
Inscription : octobre 2006
Messages : 572
Détails du profil
Informations personnelles :
Âge : 32

Informations forums :
Inscription : octobre 2006
Messages : 572
Points : 562
Points : 562
Salut
Je crois que pour ça tu peux t'orienter vers la librairie GD...

Regarde dans les cours PHP :

N'oublie pas si c'est la solution que tu cherchais
__________________
GAMIN !!!!
_______________________________________________
PHP 5.2 | Apache 2 | MySQL 5 | WinXP Pro | Mac OSX
mathieugamin est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/02/2007, 10h02   #3
Membre habitué
 
Inscription : mai 2002
Messages : 475
Détails du profil
Informations forums :
Inscription : mai 2002
Messages : 475
Points : 105
Points : 105
j'ai bien regardé la librairie GD
et je pense que toutes les étapes de redimentionnement on bien été effectuées dans la class...
mais la taille reste inchangée...
c'est qqchose m'echappe..
nicerico est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/02/2007, 11h06   #4
En attente de confirmation mail
 
Inscription : juin 2002
Messages : 6 164
Détails du profil
Informations forums :
Inscription : juin 2002
Messages : 6 164
Points : 6 404
Points : 6 404
Votre méthode store semble déjà gérer l'enregistrement de l'image redimensionnée sur le disque or vous ne l'utilisez pas. Ensuite, la variable $destination (méthode resize) n'existe pas dans la portée de votre classe et/ou méthode alors que celle-ci existe dans le contexte général/normal. Si vous tenez à garder vos modifications il vous faut donc rajouter un paramètre à la méthode ou bien déclarer un attribut supplémentaire à la classe qui sera initialisé par le constructeur.


Julp.
julp est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/02/2007, 11h22   #5
Membre à l'essai
 
Inscription : février 2007
Messages : 21
Détails du profil
Informations forums :
Inscription : février 2007
Messages : 21
Points : 23
Points : 23
Pensez à vérifier les valeurs de retour des fonctions

Essaye d'afficher $this->temp a la fin de img::resize (directement dans la fonction de classe, pas en appel derrière) juste après le copyresample, avant le sync et quitte direct après, voir si là ça marche (si oui, l'erreur vient de sync).
En gros le code a la fin de resize deviendrait:
Code :
1
2
3
4
5
6
7
8
9
10
if (imageCopyResampled($this->temp, $this->image,
0, 0, 0, 0, $width, $height, $o_wd, $o_ht)) {
ImageJPEG($this->temp);
} else {
die(posix_strerror(posix_get_last_error()));
}
exit;
//imagejpeg($this->temp, $destination);
//$this->sync();
return;
lepidosteus est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/02/2007, 07h18   #6
Membre habitué
 
Inscription : mai 2002
Messages : 475
Détails du profil
Informations forums :
Inscription : mai 2002
Messages : 475
Points : 105
Points : 105
Citation:
Si vous tenez à garder vos modifications il vous faut donc rajouter un paramètre à la méthode ou bien déclarer un attribut supplémentaire à la classe qui sera initialisé par le constructeur.
Dans la théorie, je vois ce que vous voulez dire
mais dans la pratique je n'y arrive pas

je ne suis pas tres POO ...

néanmoins j'ai essayé ça mais l'image garde toujours sa taille originale
Code :
1
2
3
4
5
6
		$this->temp = imagecreatetruecolor($width,$height);
		imagecopyresampled($this->temp, $this->image,
		0, 0, 0, 0, $width, $height, $o_wd, $o_ht);
		imagejpeg($this->image,$this->temp);
		$this->sync();
		return;
******************************************************
le test suivant n'a rien donné
Code :
1
2
3
4
5
6
7
if (imageCopyResampled($this->temp, $this->image,
0, 0, 0, 0, $width, $height, $o_wd, $o_ht)) {
ImageJPEG($this->temp);
} else {
die(posix_strerror(posix_get_last_error()));
}
exit;
nicerico est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/02/2007, 11h19   #7
En attente de confirmation mail
 
Inscription : juin 2002
Messages : 6 164
Détails du profil
Informations forums :
Inscription : juin 2002
Messages : 6 164
Points : 6 404
Points : 6 404
Je vais aller un peu plus loin alors dans mon explication :
Solution 1 : faire appel à la méthode store :
Code :
1
2
3
4
5
6
7
if($_GET['pic']){
	$img = new img('upload/'.$_GET['pic']);
	$destination = 'upload/'.$_GET['pic'];
	$img->resize();
	$img->show();
	$img->store($destination);
}
Solution 2 : modifier la méthode pour intégrer la "destination"
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
	function resize($destination = '', $width = 220, $height = 135, $aspectradio = true) {
		$o_wd = imagesx($this->image);
		$o_ht = imagesy($this->image);
		if(isset($aspectradio)&&$aspectradio) { # le test isset n'est pas utile ici puisque cette variable sera systématiquement définie
			$w = round($o_wd * $height / $o_ht);
			$h = round($o_ht * $width / $o_wd);
			if(($height-$h)<($width-$w)){
				$width =& $w;
			} else {
				$height =& $h;
			}
		}
		$this->temp = imageCreateTrueColor($width,$height);
		imageCopyResampled($this->temp, $this->image,
		0, 0, 0, 0, $width, $height, $o_wd, $o_ht);
		if (!empty($destination)) {
			ImageJPEG($this->image, $destination);
		}
		$this->sync();
		return; # strictement inutile
	}
Et changer l'appel en :
Code :
1
2
3
4
5
6
if($_GET['pic']) {
	$img = new img('upload/'.$_GET['pic']);
	$destination = 'upload/'.$_GET['pic'];
	$img->resize($destination);
	$img->show();
}
julp est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/02/2007, 20h58   #8
Membre habitué
 
Inscription : mai 2002
Messages : 475
Détails du profil
Informations forums :
Inscription : mai 2002
Messages : 475
Points : 105
Points : 105
merci pour tout
ça fonctionne maintenant comme je veux
nicerico 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 01h26.


 
 
 
 
Partenaires

Hébergement Web