Categories > TinyButStrong general >

CAPTCHA and TBS

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Trent
Date: 2007-02-08
Time: 02:38

CAPTCHA and TBS

I'm trying to implement a simple CAPTCHA system for my logins and I'm having a few issues.  When not using TBS, my CAPTCHA function works, but when I try and implement it with TBS, I get the following error:

The image “http://localhost/demo/login.php” cannot be displayed, because it contains errors.

Here is my php function:
function captcha($image)
{
header ("Content-type: image/png");
$handle = ImageCreate (130, 50) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 255, 255, 255);
$txt_color = ImageColorAllocate ($handle, 0, 0, 0);
ImageTTFText ($handle, 20, 15, 30, 40, $txt_color, "/includes/type-ra.ttf", "$image");
ImagePng ($handle);
return ImagePng;
}
And here is how I determine the numbers for my image.
$image = rand(10000,99999);
$_SESSION['validate'] = $image;
$captcha = captcha($image);
And in my HTML template:
[var.captcha]
I've tried placing my [var.captcha] inside an image tag and that doesn't work either.  Any ideas as to what I'm doing wrong?
By: sheepy
Date: 2007-02-08
Time: 05:42

Re: CAPTCHA and TBS

First, the function won't work since it's trying to return a function not a variable.

Assuming that is fixed, I take it that it is returning a GD image resource, which is a 'handle' and does not contains any content of the image.  Trying to display, return, or otherwise manipulate it will result in it being treated as null.

I'd recommend having another php (e.g. captcha.php) that returns the image (content) and nothing else.  Then you can use that php file as image (<img src='captcha.php'/>),

If you insist, it is possible to embed an image by the <img src='data:image/png;base64,(content in BASE64)'/> syntax, which is supported by everything other then IE and Lynx.
By: Trent
Date: 2007-02-08
Time: 17:52

Re: CAPTCHA and TBS

Thanks sheepy.  That works.  Now just to get it to pass the validation data through the session so I can check the input against the image, but I'll get that worked out soon enough.