View Single Post
  #1 (permalink)  
Old 04-19-2008, 03:06 AM
horseatingweeds horseatingweeds is offline
Junior Member
 
Join Date: Apr 2008
Posts: 21
Default captcha image getting corupted

For some reason when I add a captcha (I'm using a Pear script) to this control it becomes corrupt and displays this error: "The image “http://localhost/phpweb20/utility/captcha” cannot be displayed, because it contains errors." If I look at the source of the page there is the binary image code. One suspicion I have is that the cause in the image code might be a blank line at the top... I don't know.

Here is the control code the captcha is in:

Code:
<?php
class UtilityController extends CustomControllerAction
{
	public function captchaAction()
	{
		$session = new Zend_Session_Namespace('captcha');
		// Chack for existing phrase in session
		$phrase = null;
		if (isset($session->phrase) && strlen($session->phrase) > 0)
			$phrase = $session->phrase;
		
		// Call Pear - generate CAPTCHA
		$captcha = Text_CAPTCHA::factory('Image');
		$opts = array('font_size' => 20,
		'font_path' => Zend_Registry::get('config')->paths->data,
		'font_file' => 'VeraBd.ttf');					
		// Call the init() method specifying the width, height, and CAPTCHA phrase		
		$captcha->init(120,60,$phrase,$opts);
		
		// write the phrase to session
		$session->phrase = $captcha->getPhrase();
		
		// disable auto-rendering since we're outputting an image
		$this->_helper->viewRenderer->setNoRender();
		header('Content-type: image/png');
		echo $captcha->getCAPTCHAAsPng();
	}
}
?>
Here is the CustomControllerAction
Code:
<?php
class CustomControllerAction extends Zend_Controller_Action
{
	public $db;
	
	// init() is automatically called by Zend_Controller_Front
	public function init()
	{
		// fetches database handle from the application resistry and stores it in db property, and makes it available to all controllers $this->db
		$this->db = Zend_Registry::get('db');
	}
}
?>
And here is the index
Code:
<?php
// registerAutoload() automatically loads Zend Framework classes ( except for Zend_Loader )
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

//load the application configuration
$config = new Zend_Config_Ini('templates/settings.ini', 'development');
Zend_Registry::set('config', $config);

// creat the application logger
$logger = new Zend_log(new Zend_Log_Writer_Stream($config->logging->file));
Zend_Registry::set('logger', $logger);

// Connect to the database
$params = array('host'		=> $config->database->hostname,
				'username'	=> $config->database->username,
				'password'	=> $config->database->password,
				'dbname'	=> $config->database->database);
				
$db = Zend_Db::factory($config->database->type, $params);
// writes $db object to the Zend_Registry so we can use it throughout the application
Zend_Registry::set('db', $db);

// setup application authentication
$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session());

// handle the user request ( controller/action ) 
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory($config->paths->base . '/includes/Controllers');
$controller->registerPlugin(new CustomControllerAclManager($auth));

// set the view renderer
// Makes Zend_Controller use Templater class instead of its default Zend_View class.
// Zend_Controller will now automatically look for a template based on the controller and action name
$vr = new Zend_Controller_Action_Helper_ViewRenderer();
$vr->setView(new Templater());
$vr->setViewSuffix('tpl');
Zend_Controller_Action_HelperBroker::addHelper($vr);

//$controller->throwExceptions(true);

// make the request
$controller->dispatch();



?>
I've run a simple test, calling the Pear script, that works fine - displaying the captcha image. If I place that simple test in the captcha function of the utility class, it fails with the above error.

Any good ideas??
Reply With Quote