Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

captcha gone wrong (gd)

Status
Not open for further replies.

snowboardr

Programmer
Feb 22, 2002
1,401
PH
Trying to put together a captcha been looking for this bug for a minute now... im just not seeing it... my text is overlapping in the image... each letter is on top of each other... where is the problem??

Code:
<?php

$pass_length = 4;		//captcha word length
$width = 200;			//captch image width
$height = 60;			//height

$font_path = dirname(_FILE_);




session_start();


$passwd = "";
$i=0 ;
while ($i < $pass_length) {
	$passwd .= chr(rand(97, 122));
	$i++;
}

$_SESSION["tt_pass"] = $passwd;

// check all fonts in the folder this script is in, then add them to array
$font = array();

if ($handle = opendir($font_path)) {
	while (false !== ($file = readdir($handle))) {
	
		if (substr(strtolower($file),-4,4) == '.ttf') {
			$fonts[] = $font_path . '/' . $file;
			
		
		}
	}		
}



if (count($fonts) < 1) {
	die("No fonts found!");
}


//jpeg header // expire this page
header("Content-Type: image/jpeg");
header("Expires: Mon 01, Jul 1998 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");


//create the image
$img = imagecreatetruecolor($width, $height);
$bg = imagecolorallocate($img, rand(210, 255), rand(210, 255), rand(210,255));
imagefilledrectangle($img, 0, 0, $width, $height, $bg);


// rough background // makes some polys and glue them around the image all over in a messy fasion
$right = rand(10, 30);
$left = 0;

while ($left < $width) {
	$poly_points = array(
		$left, 0,
		$right, 0,
		rand($right-25, $right+25), $height,
		rand($left-15, $left+15), $height);
	$c = imagecolorallocate($img, rand(210, 255), rand(210,255), rand(210,255));
	imagefilledpolygon($img, $poly_points, 4, $c);
	
	$random_ammount = rand(10, 30);
	$left += $random_ammount;
	$right += $random_ammount;

}


//vert lines

$c_min = rand(120, 185);
$c_max = rand(195, 280);


$left = 0;
while ($left < $width) {
	$right = $left + rand(3, 7);
	$offset = rand(-3, 3);
	
	$line_points = array(
		$left, 0,
		$right, 0,
		$right + $offset, $height,
		$left + $offset, $height);
		
		$pc = imagecolorallocate($img, rand($c_min, $c_max),
		rand($c_min, $c_max),
		rand($c_min, $c_max));
		imagefilledpolygon($img, $line_points, 4, $pc);
		
		$left += rand(20, 60);

}

// horiz line

$top = 0;
while ($top < $height) {
		$bottom = $top + rand(1,4);
		$offset = rand(-6,6);
		
		$line_points = array(
		0, $top,
		0, $bottom,
		$width, $bottom + $offset,
		$width, $top + $offset);
		
		$pc = imagecolorallocate($img, rand($c_min, $c_max),
		rand($c_min, $c_max),
		rand($c_min, $c_max));
		imagefilledpolygon($img, $line_points, 4, $pc);
		$top += rand(8, 15);		
		
}


//letter spaces
$spacing = $width / (strlen($passwd)+2);
$x = $spacing;


//spread letters // rotate // output it
for ($i = 0; $i < strlen($passwd); $i++) {
	$letter = $passwd[$i];
	$size = rand($height/3, $height/2);
	$rotation = rand(-30, 30);
	$y = rand($height * .90, $height - $size - 4);
	
	$font = $fonts[array_rand($fonts)];

	$r = rand(100, 255); $g = rand(100, 255); $b = rand(100, 255);
	
	$color = imagecolorallocate($img, $r, $g, $b);
	$shadow = imagecolorallocate($img, $r/3, $g/3, $b/3);
	
	imagettftext($img, $size, $roation, $x, $y, $shadow, $font, $letter);
	imagettftext($img, $size, $roation, $x-1, $y-3, $color, $font, $letter);
	
	$x +- rand($spacing, $spacing * 1.5);


}

//output to browser
imagejpeg($img);
imagedestroy($img);



?>

Jason

[red]Army[/red] : [white]Combat Engineer[/white] : [blue]21B[/blue]

 
could i suggest an alternative to captcha? better in most respects...

if you're wedded to image captcha i'll take a proper look at your post
 
Hi jpadie, thanks for the reply... I would like to use an image capture this time around... i just can't find the issue with my code that would make the letters not space... maybe i have a value wrong that im a not seeing... What is your other suggestion?

Thanks jason

Jason

[red]Army[/red] : [white]Combat Engineer[/white] : [blue]21B[/blue]

 
Code:
 $x +- rand($spacing, $spacing * 1.5);

what does this line do for you. i've not seen the +- operator before.
 
i suspect you mean +=. which would create the spacing you are looking for.
 
For an image-based captcha that's worked effectively on several of my sites, try reCAPTCHA.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top