snowboardr
Programmer
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??
Jason
[red]Army[/red] : [white]Combat Engineer[/white] : [blue]21B[/blue]
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]