Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.4.0

Request #5055 Enhance _createCAPTCHA method to use user-defined colours
Submitted: 2005-08-10 10:00 UTC Modified: 2005-08-11 14:25 UTC
From: steve at stevenmapes dot com Assigned:
Status: Analyzed Package: Text_CAPTCHA
PHP Version: 4.3.10 OS: Various
Roadmaps: (Not assigned)    
Subscription  
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes. If this is not your bug, you can add a comment by following this link. If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: steve at stevenmapes dot com
New email:
PHP Version: Package Version: OS:

 

 [2005-08-10 10:00 UTC] steve at stevenmapes dot com
Description: ------------ The file is Text/CAPTCHA/Driver/Image.php This is a request to enchance the method _createCAPTCHA() so that $options['color'] can be defined by the user by adding them to $this->_options array. Also to allow the user to define the colour that is used for the obfuscation. Another useful enchancement would be to set the background colour of the image that is created rather than defaulting to black. This would allow people to design an image more in keeping with the styles of their websites.

Comments

 [2005-08-11 10:44 UTC] wenz
I agree, but I somehow fail in making Image_Text cooperate with other colors. I'll try to work something out with their maintainers or finally drop the dependency on the package. I'll report back here when I have news.
 [2005-08-11 10:52 UTC] steve at stevenmapes dot com
I have implemented a version of your class which handles setting colours for the font and obfuscation okay, well on Fedora core 2 with apache and windows xp with apache and the pages viewed through IE6 and Firefox 1.04-1.06 I've not tried setting the background colour, but I believe that what would need to happen is to create a new layer, with a rectangle that is filled in the colour. I will look to see if I can get it working using the current setup of your project
 [2005-08-11 14:25 UTC] steve at stevenmapes dot com
Okay, i've looked into it, and believe that it will indeed require changes to both your class and the dependency one. Here is my modifed Text/CAPTCHA/Driver/Image.php file. I've put all of my modifications between block comments marked /* Start of Steven Mapes Modificaiton */ and /* End of Steven Mapes Modification */, I've also increased some of the spaces before and after to try to make it eaiser to find. Basically the init() method now uses two extra options, Color (text colour in hex) and ob_color (obfuscation color). Most of the changes are in _createCAPTCHA() which uses these two new options to define the colours but also has two extra lines to define another colour, in this case blue, and then fill the image with that colour from reference 1,1 (should really be 0,0). The problem is that because the text has already been rendered, it fills around the letters so you have gaps with black in. Ideally the Image/Text class that the other people are doing should be modifed to accept another parameter which will fill the image before writing the text. Anyway, hopefully its a start for you. <?php /** * Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs * * Class to create a graphical Turing test * * TODOs: * + refine the obfuscation algorithm :-) * + learn how to use Image_Text better (or remove dependency) * * * @license PHP License, version 3.0 * @author Christian Wenz <wenz@php.net> */ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA { /** * Image object * * @access private * @var resource */ var $_im; /** * Image_Text object * * @access private * @var resource */ var $_imt; /** * Width of CAPTCHA * * @access private * @var int */ var $_width; /** * Height of CAPTCHA * * @access private * @var int */ var $_height; /** * Further options (for Image_Text) * * @access private * @var array */ var $_options; /** * init function * * Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image * * @param int $width Width of image * @param int $height Height of image * @param string $phrase The "secret word" of the CAPTCHA * @param array $options further options (for Image_Text) * @access public * @return mixed true upon success, PEAR error otherwise */ function init($width = 200, $height = 80, $phrase = null, $options = null) { if (is_int($width) && is_int($height)) { $this->_width = $width; $this->_height = $height; if (empty($phrase)) { $this->_createPhrase(); } else { $this->_phrase = $phrase; } if (empty($options)) { $this->_options = array( 'font_size' => 24, 'font_path' => './', 'font_file' => 'COUR.TTF' ); } else { $this->_options = $options; if (!isset($this->_options['font_size'])) { $this->_options['font_size'] = 24; } if (!isset($this->_options['font_path'])) { $this->_options['font_path'] = './'; } if (!isset($this->_options['font_file'])) { $this->_options['font_file'] = 'COUR.TTF'; } /* Start of Steven Mapes Modification */ if (!isset($this->_options['color'])) { $this->_options['color'] = array( '#FFFFFF', '#000000' ); } if (!isset($this->_options['ob_color'])) { $this->_options['ob_color'] = array( 0, 0, 0 ); } /* End of Steven Mapes Modification */ } $retval = $this->_createCAPTCHA(); if (PEAR::isError($retval)) { return PEAR::raiseError($retval->getMessage()); } else { return true; } } else { return PEAR::raiseError('Use numeric CAPTCHA dimensions!'); } } /** * Create random CAPTCHA phrase, Image edition (with size check) * * This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller * * @access private */ function _createPhrase() { $len = intval(min(8, $this->_width / 25)); $this->_phrase = Text_Password::create($len); } /** * Create CAPTCHA image * * This method creates a CAPTCHA image * * @access private */ function _createCAPTCHA() { $options['canvas'] = array( 'width' => $this->_width, 'height' => $this->_height ); $options['width'] = $this->_width - 20; $options['height'] = $this->_height - 20; $options['cx'] = ceil(($this->_width) / 2 + 10); $options['cy'] = ceil(($this->_height) / 2 + 10); $options['angle'] = rand(0, 30) - 15; $options['font_size'] = $this->_options['font_size']; $options['font_path'] = $this->_options['font_path']; $options['font_file'] = $this->_options['font_file']; /* Start of Steven Mapes Modification */ $options['color'] = $this->_options['color']; /* End of Steven Mapes Modification */ $options['max_lines'] = 1; $options['mode'] = 'auto'; $this->_imt = new Image_Text( $this->_phrase, $options ); if (PEAR::isError($this->_imt->init())) { return PEAR::raiseError('Error initializing Image_Text (font missing?!)'); } $this->_imt->measurize(); $this->_imt->render(); $this->_im =& $this->_imt->getImg(); /* Start of Steven Mapes Modification */ // Should really use the extended $this->options to define this $red = imagecolorallocate($this->_im, 255, 0, 0); imagefill ( $this->_im, 1, 1, $red); $ob_Color = array(); // Color of obfuscation if( $this->_options['ob_color'] ) { $ob_Color = $this->_options['ob_color']; } else { // Defaults $ob_Color = array( '0xFF', '0xFF', '0xFF'); } $white = imagecolorallocate($this->_im, $ob_Color[0], $ob_Color[1], $ob_Color[2]); /* End of Steven Mapes Modification */ //some obfuscation for ($i=0; $i<3; $i++) { $x1 = rand(0, $this->_width - 1); $y1 = rand(0, round($this->_height / 10, 0)); $x2 = rand(0, round($this->_width / 10, 0)); $y2 = rand(0, $this->_height - 1); imageline($this->_im, $x1, $y1, $x2, $y2, $white); $x1 = rand(0, $this->_width - 1); $y1 = $this->_height - rand(1, round($this->_height / 10, 0)); $x2 = $this->_width - rand(1, round($this->_width / 10, 0)); $y2 = rand(0, $this->_height - 1); imageline($this->_im, $x1, $y1, $x2, $y2, $white); $cx = rand(0, $this->_width - 50) + 25; $cy = rand(0, $this->_height - 50) + 25; $w = rand(1, 24); imagearc($this->_im, $cx, $cy, $w, $w, 0, 360, $white); } } /** * Return CAPTCHA as image resource * * This method returns the CAPTCHA as GD2 image resource * * @access public * @return im image resource */ function getCAPTCHA() { return $this->_im; } /** * Return CAPTCHA as PNG * * This method returns the CAPTCHA as PNG * * @access public */ function getCAPTCHAAsPNG() { if (is_resource($this->_im)) { ob_start(); imagepng($this->_im); $data = ob_get_contents(); ob_end_clean(); return $data; } else { return PEAR::raiseError('Error creating CAPTCHA image (font missing?!)'); } } /** * Return CAPTCHA as JPEG * * This method returns the CAPTCHA as JPEG * * @access public */ function getCAPTCHAAsJPEG() { if (is_resource($this->_im)) { ob_start(); imagejpeg($this->_im); $data = ob_get_contents(); ob_end_clean(); return $data; } else { return PEAR::raiseError('Error creating CAPTCHA image (font missing?!)'); } } }
 [2006-08-29 09:29 UTC] riker_09 at yahoo dot de (Volker)
Had the same problem (only black as bg color). Solved it like discussed here (modified Image_Text). Maybe another suggestion for the obfusciation: Have the text distorted. Volker