PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Text_CAPTCHA, (image can not be displayed because it has err
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PEAR Forum Forum Index -> General PEAR questions
View previous topic :: View next topic  
Author Message
horseatingweeds



Joined: 15 Apr 2008
Posts: 10

PostPosted: Thu Apr 17, 2008 5:48 am    Post subject: Text_CAPTCHA, (image can not be displayed because it has err Reply with quote

I'm using Text_CAPTCHA in the code bellow. I'm getting and error "The image http://localhost/utility/captcha cannot be displayed, because it contains errors." I'm viewing just the 'image' by going strait to it (/utilities/captcha)

From what I can tell, Pear is installed. GD is too. I have all php errors enabled but I still get only the above error.

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();
   }
}
?>


Does anyone have any idea why this would be happening? Also, if I view the page source when the error is displayed, I don't see any error html, only a bunch of odd code, like it you look at a pdf file with the wrong extension.
Back to top
View user's profile Send private message
Mittineague



Joined: 26 May 2007
Posts: 43

PostPosted: Thu Apr 17, 2008 10:14 pm    Post subject: image Reply with quote

I believe the "bunch of odd code" you are seeing is the byte code for the image. I don't know if this is much help as my working Text_CAPTCHA code is a bit different, but I have
Code:
.....
$captcha->init(160,60,NULL,$text_image_options);
  $image_data = $captcha->getCAPTCHAAsPNG();
  file_put_contents('captcha.png',$image_data);
$_SESSION['session_key'] = $captcha->getPhrase();

Note* the file_put_contents() line.
Back to top
View user's profile Send private message
horseatingweeds



Joined: 15 Apr 2008
Posts: 10

PostPosted: Fri Apr 18, 2008 4:29 am    Post subject: Reply with quote

I'm using the Web based Pear package manager. Could I have things not installed properly or have some setting off? How can I test if these settings are correct or that Pear is working right?
Back to top
View user's profile Send private message
Mittineague



Joined: 26 May 2007
Posts: 43

PostPosted: Fri Apr 18, 2008 6:19 am    Post subject: png image Reply with quote

I think if it was a PEAR install problem the error messages would be more like "class not found". It seems that the header('Content-type: image/png'); would be enough to signal the browser what to expect. Yet it sounds like the bytes are getting sent as a string. Are you calling the generated image with a php extension. ie.
<img src="whatever.php" ?? Although I don't see where it should make a difference, maybe it has something to with it ???
Not so easy to test, but you could add a rewrite to an htaccess file to find out. ie
Code:
RewriteRule ^(.*)/captchatest\.png$ $1/whatever.php [L]


How is the output showing in view-source, is there any mark-up before it? ie.
blah blah</div>as4t_dJ2zLp7fD....
In other words, where is it breaking at?
Back to top
View user's profile Send private message
horseatingweeds



Joined: 15 Apr 2008
Posts: 10

PostPosted: Fri Apr 18, 2008 9:24 am    Post subject: Reply with quote

Hi Mittineague,

Yeah, I even tried trashing Pear and reinstalling it. Still all the same.

I'm calling the above script through a template like this:

Code:
<img src='/utility/captcha' alt='CAPTCHA image' />


I tried running it in IE. I didn't get the error, all I got was that little white box with the red X.

Here is the source from FireFox:

Code:
PNG

���
IHDR���x���<���"��IDATx[MLMvBAJOQlx0X=h<x
{<P#?&N1D1(1h�E?[iC -ewn|7.mvgLw?}wvNw^z\v�2??_!#4/ahNp(؆#YI?Z?/A_N>α?3Ns/\?G]HGYUܡ#7NU4!Lt!gDq$.4}By/J&^dCk_fEC"FtR n)Ek\?   9t-oytF,

Qe4Z


,+e=x<Q:   YY5WKtF} W?FzJ rSH8^љ2%ąL 4t<?WdLbF ':
/[email protected]`rrryy9暚&S)?IY#x򊞘<<==}D?@,FNDap y]uu5Ç`H$0h4?Ee4P? 
ѩXELj_e?NfK$>O9Ԙe[nY,X?DWO?:0Ą`08p 6dp;*A\$C?7pwtt|6<<<44p8X�8ϣƲd$EVAb{.
EQUUUvdAy^EI��4M3 c2P??1}~ATX,eEQz(btX=
�>t>;; +^luuuO<!NСejjjd,ÿ$ZF4db h4z}�@WWt~۷oO<玲ż^ojoo/--U2#;B`ʮ]& o K7ǎl`I?' ,4
977766?�Hdlltʌt:az$Ix1�Ȉ! ?M No>K
 6m enkk4??-a?;w²n?F_~Յp8jjkkt:~Jܹs?zlm0`RY^^~i7+++x`-B"@U*$IB��h(*
1 #�㸦&XF
Z?^7=ћ??ױet6 &i}}V߼y#?? ):r~(Bš!XF1oJOOŋ.\`0FgёH$Qi>Zݻw ]v/..o,{Xr߾}?˒$myEEe9듥+?\R)
<իWh&
TTTD@  k&]KTqeebyyAѨ=O^/TT<j>ZuH˗EQ\ZZeٌѣG`cJSSS]ݎW>$-   ?;v�� dR̲lwwwEE$I<t_wvv P5>>ozC%o޼Y^^( `2V*.,,߿=?u:?dwz? \ t˗8---IH   |a?nqq1
%   Z[[KӴ$I?'NxtK͟?F 5H$[email protected]`~~{qxUw�>~O!ԻtDoMF(K9Ϟ=+kWCCq_~]ZZj6u:p$ u8Nl6߿!ĿdT86kll65N˗/333p8,?rn?;wъ    K8Ŀ ?4?)~qp
0r�G?w�̀ZVu~~rn߾ѰS?[G3GzׯիW>}
�x<4MZB?~pqav={FGGqYn?^ȹ?2ߪt*HyW?!HƧJjb"'i2Pa   4]|jlLc4?8?x7ɣL ّ8"ǰ"פ4٭2:`?쩉l?RMj??Mx D~?Wb?BM֧   D%%Z| ?-]@Krq@   oY*Z;a0)( Z?,dB!z
Br?
EљX8_逡����IENDB`



I'm about out of ideas.... Maybe I should try swearing at it again.

I tried the htaccess idea too:

Code:
RewriteRule ^(.*)/captchatest\.png$ $1/captcha [L]


I must be doing it wrong though because there was no change anywhere including in the url.
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Fri Apr 18, 2008 11:23 am    Post subject: Reply with quote

Do you maybe have spaces or something like that before your echo() call? This would circumvent the sending of the right header rows that indicate that an image is sent to the browser - resulting in the fact that the binary image data is shown in the browser.
Back to top
View user's profile Send private message
horseatingweeds



Joined: 15 Apr 2008
Posts: 10

PostPosted: Fri Apr 18, 2008 8:59 pm    Post subject: Reply with quote

I tried taking out spaces. It doesn't look like that's the problem. Good idea though. Why would it be a string?

I'm using Pear not in the php installation way. Loaded it into the website's root with the go-pear script, and am using the Webbased Pear Package manager. Is there some additional thing to do after you load packages or something?

I've tried running a Captcha on another server, my host:

Code:
<?php

error_reporting(E_ALL);
require_once ($_SERVER['DOCUMENT_ROOT'] . '/pear/PEAR/Text/CAPTCHA.php');
require_once ($_SERVER['DOCUMENT_ROOT'] . '/pear/PEAR/Text/Password.php');
require_once ($_SERVER['DOCUMENT_ROOT'] . '/pear/PEAR/Image/Text.php');
echo $_SERVER['DOCUMENT_ROOT'];

$captcha = Text_CAPTCHA::factory('Image');
$opts = array('font_size' => 20,
         'font_path' => $_SERVER['DOCUMENT_ROOT'] . '/',
         'font_file' => 'VeraBd.ttf');
         
$captcha->init(120, 60, 'This', $opts);
header('Content-type: image/png');
echo $captcha->getCAPTCHAAsPng();

?>


It doesn't produce anything there. I loaded Pear the same way on that server. I places the error_reporting(E_ALL); but I still don't get any errors. [/code]
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Fri Apr 18, 2008 9:53 pm    Post subject: Reply with quote

Although there seems to be an image being generated, I'd recommend that you change your require_once calls. You currently have this:
Code:

require_once ($_SERVER['DOCUMENT_ROOT'] . '/pear/PEAR/Text/CAPTCHA.php');

while you should have just
Code:

require_once('Text/CAPTCHA.php');


This requires that you have added the PEAR directory to your include_path, resulting in that you make sure that all PEAR packages are able to find their files.

If you don't see any errors -- would it be possible that display_errors is set to off? To enable this setting, just add the following line next to the error_reporting() call:
Code:

ini_set('display_errors', 1);
Back to top
View user's profile Send private message
horseatingweeds



Joined: 15 Apr 2008
Posts: 10

PostPosted: Fri Apr 18, 2008 10:25 pm    Post subject: Reply with quote

Hi Mark,

I was including the file that way just as a test to see if I could produce a captcha on another server that I haven't added Pear to it's includes. On my test server I include them the way you suggest. The problem is the captcha isn't rendering, it just give an error, "The image http://localhost/phpweb20/utility/captcha cannot be displayed, because it contains errors." The source code has the image code in it....

Thanks for the ini_set idea. I'm not sure what's going on with the other server now. The error says the path is wrong - but it's not... I've checked it about fifteen times and cut-pasted from the ftp client.

This Pear business is about the most complicated and monstrous convenience I've ever dealt with. This is actually my third time trying to us Pear. The first two times I gave up, I think because then it was even more difficult to get on a shared host. It really doesn't seem like included files should be so difficult.
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Fri Apr 18, 2008 10:36 pm    Post subject: Reply with quote

horseatingweeds wrote:
The problem is the captcha isn't rendering, it just give an error, "The image http://localhost/phpweb20/utility/captcha cannot be displayed, because it contains errors." The source code has the image code in it....


Is there anything before the %png (= image start) or anything after the binary image data?

horseatingweeds wrote:
Thanks for the ini_set idea. I'm not sure what's going on with the other server now. The error says the path is wrong - but it's not... I've checked it about fifteen times and cut-pasted from the ftp client.


Please quote the error message here (maybe with your server's detail anonymized).

horseatingweeds wrote:
This Pear business is about the most complicated and monstrous convenience I've ever dealt with. This is actually my third time trying to us Pear. The first two times I gave up, I think because then it was even more difficult to get on a shared host. It really doesn't seem like included files should be so difficult.


It isn't really difficult, and in your case the reason seems to be something else. If you use the usual require_once calls (e.g. 'Text/Captcha.php') and adjust your include_path (that's of course also possbile on shared hosts), you won't have problems with the includes.
Back to top
View user's profile Send private message
horseatingweeds



Joined: 15 Apr 2008
Posts: 10

PostPosted: Fri Apr 18, 2008 11:12 pm    Post subject: Reply with quote

The binary image code I have above. There is a blank line at the top though. I've been trying to find what might be causing that.

I've solved the include problem. I added the include path and require_once'ed it properly.

Holly-ZHit. It works on my hosted server! What a pain. Is there some windows bug, maybe in the PEAR_Frontend_Web beta?

*sigh* now I can't give up.

OK, there's something wrong with this code. I've got the simple test captcha working. It's the class above called Utility. Something is off in there - I guess.
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Fri Apr 18, 2008 11:56 pm    Post subject: Reply with quote

horseatingweeds wrote:
Holly-ZHit. It works on my hosted server! What a pain. Is there some windows bug, maybe in the PEAR_Frontend_Web beta?

*sigh* now I can't give up.

OK, there's something wrong with this code. I've got the simple test captcha working. It's the class above called Utility. Something is off in there - I guess.


Please describe what works on which server and what doesn't work. And what is this "Utility" class?

The PEAR_Frontend_Web package shouldn't break anything because it just uses the usual methods of the PEAR installer for downloading and installing the package files. It is much more likely that there is something wrong in your code, and that you have some differences between your local and your remote server.
Back to top
View user's profile Send private message
horseatingweeds



Joined: 15 Apr 2008
Posts: 10

PostPosted: Sat Apr 19, 2008 1:22 am    Post subject: Reply with quote

OK

This code works on both my local and remote server:

Test
Code:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once 'Text/CAPTCHA.php';

$captcha = Text_CAPTCHA::factory('Image');
$opts = array('font_size' => 20,
         'font_path' => $_SERVER['DOCUMENT_ROOT'] . '/phpweb20/data/',
         'font_file' => 'VeraBd.ttf');
         
$captcha->init(120, 60, 'This', $opts);
header('Content-type: image/png');
echo $captcha->getCAPTCHAAsPng();

?>


This code doesn't work on my local server. The error is

The image http://localhost/phpweb20/utility/captcha cannot be displayed, because it contains errors.

when I run it by its self by going to root/utility/captcha. If I call it through an image tag it just displays the alt. I haven't tested it on the remote server.

CAPTCHA controller
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();
   }
}
?>


I've done some experiments. If I take the 'test' code and put it in the CAPTCHA action in the Utility class, it doesn't work.

Here is the CustomeController:
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');
   }
}
?>


I'm building this using the Zend framework. So, the Zend_Controller_Action is part of the Zend Framework.

Here is my bootstrap file:

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();
?>
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1235

PostPosted: Sat Apr 19, 2008 12:55 pm    Post subject: Reply with quote

Your bootstrap file starts with an empty line here in the forum -- could that be the problem?

If it isn't, I'd guess that the Zend Framework sends e.g. some headers that make your image broken. If haven't used the ZF, and can't tell you more about that, but maybe a Zend forum can do.
Back to top
View user's profile Send private message
horseatingweeds



Joined: 15 Apr 2008
Posts: 10

PostPosted: Sat Apr 19, 2008 6:17 pm    Post subject: Reply with quote

Good work Mark! That was the problem. What a sneaky pain! It makes sense thinking about it now though....

Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PEAR Forum Forum Index -> General PEAR questions All times are GMT + 2 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement