Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Bonjour tous le monde.
Est ce qu'on peu modifier l'arriere plan de l'image captcha generé par Zend_Captcha_Image, parce que c'est tres dificile de lire l'image generé par default.
Et merci pour toute aide
Hors ligne
Hello,
Normalement en passant à l'option "startImage" (ou via setStartImage())le chemin de l'image de fond, cela doit fonctionner.
@+
Hors ligne
Merci infiniment pour votre reponce.
J'ai ajouter l'option setStartImage("Path") mé ça me donne l'image par default et quand je recupere le chemin de l'image avec l'option getStartImage() il me donne le chemin correcte vers l'image arriere plan que j'ai crée.
j'ai testé avec des image .png et .jpg mais ça me donne rien du tout.
Dernière modification par m4riachi1986 (23-09-2010 11:25:28)
Hors ligne
Peux-tu fournir le code que tu utilises et qui ne fonctionne pas ?
Hors ligne
class captcha
{
public function generateCaptcha()
{
$captcha = new Zend_Captcha_Image();
$captcha->setStartImage($_SERVER['DOCUMENT_ROOT'] . "/font/arriere.png")
->setTimeout("300")
->setExpiration("4")
->setWordLen("5")
->setHeight("50")
->setWidth("100")
->setFont($_SERVER['DOCUMENT_ROOT'] . "/font/arial.ttf")
->setImgDir($_SERVER['DOCUMENT_ROOT'] . "/captcha");
$captcha->generate();
var_dump($captcha->getStartImage());
return $captcha->getId();
}
public function validateCaptcha($captcha)
{
Zend_Session::start();
$captchaId = $captcha["id"];
var_dump($captchaId);
$captchaInput = $captcha["input"];
$captchaSession = new Zend_Session_Namespace("Zend_Form_Captcha_" . $captchaId);
$captchaIterator = $captchaSession->getIterator();
$captchaWord = $captchaIterator["word"];
if($captchaWord)
{
if($captchaInput != $captchaWord)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
}
Hors ligne
Bonjour,
Je rencontre le même souçi. En fait, il faut retirer les grains et les lines avec ceci par exemple :
$captcha->setDotNoiseLevel(0);
$captcha->setLineNoiseLevel(0);
Et après affecter le startImage.. cependant, j'ai mis par exemple une image png de 400 x 300 en rouge
Le fait de mettre startImage ne change pas vraiment grand chose puisque le background sera mis en blanc de toute manière
(Ligne 505 $bg_color = imagecolorallocate($img2, 255, 255, 255); de Zend\Captcha\Image.php
Aucune possibilité de transparence visiblement.. il faudrait utiliser imagecolorallocatealpha() à la place.
Autre problème, le render est mis automatiquement avec getWidth et getHeight() qui sont définit dans la classe.
En mettant une image perso, les fonts seront alors super petits, car on passe de 400 x 300 a 200 x 50.. la seule solution constitue à étendre la classe et à redefinir la fonction render() (ce qui permet de dégager le vieux <br /> à la fin par ailleurs)
Enfin, une derniere question, j'aimerai générer un captcha de 4 chiffres aléatoire (et je ne veux pas utiliser de lettre) avec Zend_Word_Capcha je peux juste dire si je veux des chiffres ou que des lettres, mais je ne peux pas envoyer des expressions à la place.. (genre rand( + expression régulière avec des caractères spéciaux +))
Avez vous une solution pour ces deux problèmes ? (Background, taille de l'image et génération du mot)
Ou dois je le marquer dans la roadmap de Zend ?
Merci
Hors ligne
Hello,
Pose un issue sur le tracker. J'ai repris la gestion du composant Captcha donc je vais regarder pour ZF 1.11.
@+
Hors ligne
C'est fait.. le bug a été assigné mais aucun retour de la part des équipes de zend
Hors ligne
Hello,
Je ne l'avais pas vu passer car les bugs Captcha sont toujours assigné par défaut à Stas. Donc je viens de me le réassigner. Je regarderais dans le week-end.
Le délai de réponse (et/ou de correction) se compte en jours voire en semaines pas en heures mais cela devrait être OK pour ZF 1.11
@+
Hors ligne
Bonjour,
Je suis utilisateur de ZF version 1.11 mais il semblerait que le problème soit toujours d'actualité.
Impossible d'avoir un fond transparent même en forçant le startImage sur un png transparent.
Est ce normal ?
Merci
Hors ligne
Si ça peut aider quelqu'un : une classe qui hérite de Zend_Captcha_Image que j'ai corrigé :
<?php class Tools_Captcha_Image extends Zend_Captcha_Image { /** * Generate image captcha * Correction pour fond transparent * * Override this function if you want different image generator * Wave transform from http://www.captcha.ru/captchas/multiwave/ * * @param string $id Captcha ID * @param string $word Captcha word */ protected function _generateImage($id, $word) { if (!extension_loaded("gd")) { require_once 'Zend/Captcha/Exception.php'; throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension"); } if (!function_exists("imagepng")) { require_once 'Zend/Captcha/Exception.php'; throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support"); } if (!function_exists("imageftbbox")) { require_once 'Zend/Captcha/Exception.php'; throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support"); } $font = $this->getFont(); if (empty($font)) { require_once 'Zend/Captcha/Exception.php'; throw new Zend_Captcha_Exception("Image CAPTCHA requires font"); } $w = $this->getWidth(); $h = $this->getHeight(); $fsize = $this->getFontSize(); $img_file = $this->getImgDir() . $id . $this->getSuffix(); if(empty($this->_startImage)) { $img = imagecreatetruecolor($w, $h); imagesavealpha($img, true); } else { $img = imagecreatefrompng($this->_startImage); if(!$img) { require_once 'Zend/Captcha/Exception.php'; throw new Zend_Captcha_Exception("Can not load start image"); } $w = imagesx($img); $h = imagesy($img); } $text_color = imagecolorallocate($img, 0, 0, 0); $bg_color = imagecolorallocatealpha($img, 255, 255, 255, 127); //imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color); imagefill($img, 0, 0, $bg_color); $textbox = imageftbbox($fsize, 0, $font, $word); $x = ($w - ($textbox[2] - $textbox[0])) / 2; $y = ($h - ($textbox[7] - $textbox[1])) / 2; imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word); // generate noise for ($i=0; $i<$this->_dotNoiseLevel; $i++) { imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color); } for($i=0; $i<$this->_lineNoiseLevel; $i++) { imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color); } // transformed image $img2 = imagecreatetruecolor($w, $h); imagesavealpha($img2, true); $bg_color = imagecolorallocatealpha($img2, 255, 255, 255, 127); //imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color); imagefill($img2, 0, 0, $bg_color); // apply wave transforms $freq1 = $this->_randomFreq(); $freq2 = $this->_randomFreq(); $freq3 = $this->_randomFreq(); $freq4 = $this->_randomFreq(); $ph1 = $this->_randomPhase(); $ph2 = $this->_randomPhase(); $ph3 = $this->_randomPhase(); $ph4 = $this->_randomPhase(); $szx = $this->_randomSize(); $szy = $this->_randomSize(); for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx; $sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy; if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) { continue; } else { $color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF; $color_x = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF; $color_y = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF; $color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF; } if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) { // ignore background continue; } elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) { // transfer inside of the image as-is $newcolor = 0; } else { // do antialiasing for border items $frac_x = $sx-floor($sx); $frac_y = $sy-floor($sy); $frac_x1 = 1-$frac_x; $frac_y1 = 1-$frac_y; $newcolor = $color * $frac_x1 * $frac_y1 + $color_x * $frac_x * $frac_y1 + $color_y * $frac_x1 * $frac_y + $color_xy * $frac_x * $frac_y; } imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor)); } } // generate noise for ($i=0; $i<$this->_dotNoiseLevel; $i++) { imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color); } for ($i=0; $i<$this->_lineNoiseLevel; $i++) { imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color); } imagepng($img2, $img_file); imagedestroy($img); imagedestroy($img2); } }
Hors ligne