Hi,

I have a captcha example and works fine. However, only image is dispayed on the screen. HTML form is not visible. I believe this related to the "header" bit in the class file. I added the code. Please help me solving this problem.

Thanks

index.php

<?php
require_once "CaptchaSecurityImages.php";

$width = '120';
$height = '40';
$characters = '6';

$captcha = new CaptchaSecurityImages();
$img = $captcha -> Captcha($width,$height,$characters);
?>

<form action="save.php" method="post">
Name <input type="text" name="textName" /><br />
<img src="<?php $img; ?>" /><br />
Security Code: <input type="text" name="textCaptchaCode" id="textCaptchaCode" /><br />
<input type="submit" name="submitButton" value="Submit" />
</form>

CaptchaSecurityImages.php

<?php
session_start();

class CaptchaSecurityImages {

	var $font = 'monofont.ttf';

	function generateCode($characters) {
		/* list all possible characters, similar looking characters and vowels have been removed */
		$possible = '23456789bcdfghjkmnpqrstvwxyz';
		$code = '';
		$i = 0;
		while ($i < $characters) { 
			$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
			$i++;
		}
		return $code;
	}

	function Captcha($width='120',$height='40',$characters='6') {
		$code = $this->generateCode($characters);
		/* font size will be 75% of the image height */
		$font_size = $height * 0.75;
		
		$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
		/* set the colours */
		$background_color = imagecolorallocate($image, 255, 255, 255);
		$text_color = imagecolorallocate($image, 20, 40, 100);
-		$noise_color = imagecolorallocate($image, 100, 120, 180);
		/* generate random dots in background */
		for( $i=0; $i<($width*$height)/3; $i++ ) {
			imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
		}
		/* generate random lines in background */
		for( $i=0; $i<($width*$height)/150; $i++ ) {
			imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
		}
		/* create textbox and add text */
		$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
		$x = ($width - $textbox[4])/2;
		$y = ($height - $textbox[5])/2;
		imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
		/* output captcha image to browser */
		header('Content-Type: image/jpeg');
		imagejpeg($image);
		imagedestroy($image);
		$_SESSION['security_code'] = $code;
	}
}
?>

Note: found this example on the web

Recommended Answers

All 2 Replies

Yep its a realy simple problem and a simple solution. Even though its part of a PHP script you need to refference it as an image I.E.
Create a file 'op_image.php' and add the code

<?php
require_once "CaptchaSecurityImages.php";

$width = '120';
$height = '40';
$characters = '6';

$captcha = new CaptchaSecurityImages();
$img = $captcha -> Captcha($width,$height,$characters);
?>

(Removing that code from the form)
then in the form set the image link to

<img src="op_image.php" />

Exactly samarudge. SOLVED. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.