Page 1 of 1

captcha code: need to get image if passed a 'pass' string

Posted: Fri Jul 27, 2007 10:19 am
by raghavan20
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.

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


?>
the output is at
http://thisisraghavan.com/test/Captcha/

Re: captcha code: need to get image if passed a 'pass' strin

Posted: Fri Jul 27, 2007 10:35 am
by superdezign
raghavan20 wrote:but i do not really want to create files for images and use those files.
The <img /> tag only takes a file as a src attribute. You will either need to create a PHP file specifically for generating the images and putting it into the src attribute, or make your class save the file prior to attempting to output it to the screen, then putting that filename into the src.

Posted: Fri Jul 27, 2007 10:37 am
by miro_igov
Superdiz, are you 100% sure?

Code: Select all

<img src="<?php echo file_get_contents('test.jpg') ;" />

Posted: Fri Jul 27, 2007 10:52 am
by superdezign
miro_igov wrote:Superdiz, are you 100% sure?

Code: Select all

<img src="<?php echo file_get_contents('test.jpg') ;" />
Yes, I do believe I am. How would that work? The src attribute doesn't take binary data, it takes filenames. Also, that wouldn't have the necessary header information, either.

Did you try it before you posted it?

Posted: Fri Jul 27, 2007 10:58 am
by miro_igov
Not tested, but seen it somewhere.

Posted: Sat Jul 28, 2007 4:57 pm
by feyd
miro_igov wrote:Not tested, but seen it somewhere.
Unless test.jpg contains a URL, using file_get_contents() is 100% wrong.