NYCPHP Meetup

NYPHP.org

[nycphp-talk] PEAR CAPTCHA Problems

Ben Sgro (ProjectSkyLine) ben at projectskyline.com
Wed Aug 29 18:44:03 EDT 2007


Hello, 

I've just written a class around the PEAR captcha code.

Except, I can't get it to work.
The code is included:

class Captcha
    {
        private $path;
        public  $error;
        function __construct($PATH)
        {
            $this->phrase = '';
            $this->image  = '';
            $this->width  = '';
            $this->height = '';
            $this->path   = $PATH;
            $this->error  = '';
        }
    
    
        function GenerateCaptcha($width, $height, $options=false)
        {
            if ( !$options ) $this->GenerateCaptchaOptions($options);
            $captcha = TEXT_CAPTCHA::factory('Image');
            $result  = $captcha->init($width, $height, null, $options);
            if ( PEAR::isError($captcha) )
            {
                return ($this->error = CAPTCHA_CREATE_ERROR);
            }

            $this->phrase = $captcha->getPhrase( );
            $this->image  = $captcha->getCAPTCHAAsJPEG( );
            if ( PEAR::isError($this->image) )
            {
                return ($this->error = CAPTCHA_IMAGE_ERROR);                
            }
            $this->width  = $width;
            $this->height = $height;

            $this->WriteCaptcha( );
            
            return $this;
        }
        
        
        function WriteCaptcha( )
        {
            $file = $this->path . md5($this->phrase) . '.jpg';
            file_put_contents($file, $this->image);     
        }
        
        
        function GetCaptchaFilename( )
        {
            return md5($this->phrase) . '.jpg';
        }
        
        
        function GenerateCaptchaOptions(&$options)
        {
            $options = array(   'font_size' => 32,
                                'font_path' => './',
                                'font_file' => 'arial.ttf'
                            );
        }
    }

As far as I can tell, its unable to write/create the file/image.

[29-Aug-2007 18:34:09] There was an error creating the captcha image

Is what I am getting in my PHP logs. Now the captcha directory is chmod 777, just to see if that
fixed it. It didn't. I also did a chown apache:blah captcha and that didn't work either.

The code I'm using to call the CAPTCHA is:

        $captchaObject = new Captcha(CAPTCHA_PATH);
        $captchaObject->GenerateCaptcha(400, 180);
        if ( $captchaObject->error != '' )
        {
            error_log($captchaObject->error);
        }
        $smartyObject->assign('captcha', $htmlObject->CreateImage(
            $captchaObject->GetCaptchaFilename( ), 300, 180, 'CAPTCHA'));

Now, if I print_r($captchaObject) I get:

    [error] => There was an error creating the captcha image
    [phrase] => chiaprae
    [image] => PEAR_Error Object
        (
            [error_message_prefix] => 
            [mode] => 1
            [level] => 1024
            [code] => 
            [message] => Error initializing Image_Text (font missing?!)I just copied this from the PEAR Apress book. Does anyone know where to get the font file,if that is indeed the error?- Ben
Ben Sgro, President
ProjectSkyLine - Defining New Horizons
+1 718.487.9368 (N.Y. Office)

Our company: www.projectskyline.com
Our products: www.project-contact.com

This e-mail is confidential information intended only for the use of the individual to whom it is addressed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070829/a16366fe/attachment.html>


More information about the talk mailing list