8796 sujets

Développement web côté serveur, CMS

Bonjour a tous,

Voila je me met progressivement au POO en php, j'ai lue pas mal de doc théorique et j'essaie de passer à la pratique.
J'ai hélas trouvé aucun tutorial sur le POO en ce qui concerne la pratique meme (si vous en avez je suis preneur : ) ).
Donc je me lance dans la création d'une class pour un captcha simple afin de vérifier si j'ai bien saisi ce que j'ai lue.

Voici mon code
class Captcha {

public $ImgWidth;
public $ImgHeigth;
private $_image;
private $_backgr_col;
private $_border_col;

function __construct($Width, $Heigth) {
	$this->ImgWidth = $Width;
	$this->ImgHeigth = $Heigth;
}

private function _creaImg() {
	$this->_image = imagecreatetruecolor($this->ImgWidth, $this->ImgHeigth);
	
}

private function _couleurImg() {
	$this->_backgr_col = imagecolorallocate($this->_image, 238,239,239);
	$this->_border_col = imagecolorallocate($this->_image, 208,208,208);
}

public function remplissage() {
	imagefilledrectangle($this->_image, 0, 0, $this->ImgWidth, $this->ImgHeigth, $this->_backgr_col);
	imagerectangle($this->_image, 1, 1, ($this->ImgWidth-1), ($this->ImgHeigth-1), $this->_border_col);
	return($this->_image);
}

public function chgHeader() {
	if(remplissage()) {
	header("Content-type: image/png");
	imagepng($this->_image);
	imagedestroy ($this->_image);
	}
}

public function affiche() {
	echo '<img src="ImgCaptcha3.php" width="' . $this->ImgWidth . '" height="' . $this->ImgHeigth . '" />';	
}

}//Fin de la class


et voici la page qui appel ma class

	include_once 'ImgCaptcha3.php';
	$img = new Captcha(180, 60);
	$img->affiche();


Pour l'instant je ne cherche qu'à afficher une image.
Ma fonction "affiche()" me renvoie bien l'"echo" demandé mais c'est une image vide que j'obtiens

Je ne sais pas trop ou placer mes exceptions pour contrôler si mes fonctions fonctionnent comme je le voudrai.

Il doit me manquer des fondamentaux que j'ai due zapper Smiley confused

Voila si vous pouviez m'orienter sur la bonne voie ce serais cool Smiley smile
Modifié par Traxyl2en1 (04 Jun 2009 - 13:02)
Salut,

ben le problème c'est qu'à aucun moment tu n'appelles les méthodes qui servent à générer l'image :
* _creaImg()
* _couleurImg
* chgHeader

Tu pourrais peut-être t'inspirer de ce post pour voir un exemple...
Salut Heyoan,

désole pour la réponse si tardive,

j'ai regardé ton poste et effectivement il n'y a aucun lien entre mes fonctions

j'en ai déduis ce code

class Captcha {

public $ImgWidth;
public $ImgHeigth;
private $_image;
private $_backgr_col;
private $_border_col;

public function __construct() {
	$this->ImgWidth = 0;
	$this->ImgHeigth = 0;

}

public function creaImg($Width, $Heigth) {
	$this->ImgWidth = $Width;
	$this->ImgHeigth = $Heigth;
	$this->_image = imagecreatetruecolor($this->ImgWidth, $this->ImgHeigth);
	$this->_couleurImg();
	
}

private function _couleurImg() {
	$this->_backgr_col = imagecolorallocate($this->_image, 238,239,239);
	$this->_border_col = imagecolorallocate($this->_image, 208,208,208);
	$this->remplissage();
}

public function remplissage() {
	imagefilledrectangle($this->_image, 0, 0, $this->ImgWidth, $this->ImgHeigth, $this->_backgr_col);
	imagerectangle($this->_image, 1, 1, ($this->ImgWidth-1), ($this->ImgHeigth-1), $this->_border_col);
	$this->chgHeader();
}

public function chgHeader() {
	if(remplissage()) {
	header("Content-type: image/png");
	imagepng($this->_image);
	imagedestroy ($this->_image);
	}
}

public function affiche() {
	echo '<img src="ImgCaptcha3.php" width="' . $this->ImgWidth . '" height="' . $this->ImgHeigth . '" />';	
}

}//Fin de la class


et

include_once 'ImgCaptcha3.php';
	$img = new Captcha();
	$img->creaImg(180, 60);
	$img->affiche();


Mais ca ne donne pas grand chose et php ne me donne aucun message d'erreur ou indice pour trouver d'où viens le problème

C'est le gros défaut, je trouve, de phpoo, difficile de debugger un code quand on ne sait pas d'où viens le probleme... Smiley bawling
Modifié par Traxyl2en1 (08 Jun 2009 - 10:33)
Salut,

la ligne if(remplissage()) { devrait pourtant générer une erreur !

Mais le problème ne vient pas de l'utilisation d'une classe : en l'état je ne vois pas comment ça pourrait marcher. Tu devrais essayer de le faire d'abord avec une simple fonction et seulement ensuite le faire avec une classe.
après plusieurs heures passés à ne pas savoir ou j'aurais pu faire une erreur, Smiley decu
je vais suivre ton conseil et repartir d'un peu plus bas.

Je vous tiens au courant

merci
Modifié par Traxyl2en1 (08 Jun 2009 - 15:57)