captcha code: need to get image if passed a 'pass' string
Posted: Fri Jul 27, 2007 10:19 am
i am thinking of using captcha code for a few forms. I thought of making as a module so that when we give it a pass string(the string that we display in the image), it generates an image. We then use this image to display in <img /> later. i have a script/class that does generate an image and but it is directly getting outputted to the browser and i cannot see other HTML output. i somehow want to capture the contents of the image and want to use in <img />. but i do not really want to create files for images and use those files.
this class allows to generate the captcha code image and i have put 'pass' in a public variable so that i can access from outside which text is used. I also allowed to provide custom text from outside to the class so that we can generate image of a custom text.
I am trying to use the image in this file
the output is at
http://thisisraghavan.com/test/Captcha/
this class allows to generate the captcha code image and i have put 'pass' in a public variable so that i can access from outside which text is used. I also allowed to provide custom text from outside to the class so that we can generate image of a custom text.
Code: Select all
Captcha.php
<?php
header("Content-Type: image/jpeg");
class Captcha{
public $pass = NULL;
public function __construct( $generateRandom = TRUE, $pass = NULL ){
//if generate random number is false, then use the pass sent
if( $generateRandom === FALSE ){
if( strlen( $pass ) == 0 ) $this->generateRandom();
else $this->pass = $pass;
}else{
$this->generateRandom();
}
}
private function generateRandom( ){
$md5 = md5(rand(0,9999));
$this->pass = substr($md5, 10, 5);
}
public function createImage( )
{
//create box
$image = ImageCreatetruecolor(150, 20);
//create colors
$clr_white = ImageColorAllocate($image, 255, 255, 255);
$clr_black = ImageColorAllocate($image, 0, 0, 0);
//fill box
imagefill($image, 0, 0, $clr_black);
//set font properties
imagefontheight(20);
imagefontwidth(20);
//create string using font
imagestring($image, 5, 30, 3, $this->pass, $clr_white);
//draw lines in box
imageline($image, 5, 1, 50, 20, $clr_white);
imageline($image, 60, 1, 96, 20, $clr_white);
//return image
imagejpeg($image);
imagedestroy($image);
}
}
?>I am trying to use the image in this file
Code: Select all
<?php
error_reporting( E_ALL );
include_once( 'Captcha.php' );
$CaptchaObj = new Captcha( FALSE, 'sf9799999sf' );
$pass = $CaptchaObj->pass;
echo "<html><body>pass: $pass; <img src = '{$CaptchaObj->createImage( )}' ></body></html>";
##echo phpinfo();
?>http://thisisraghavan.com/test/Captcha/