Reputation: 13
I am unable to display the captcha image generated using core PHP. It shows a square thumbnail instead of the captcha image. The font file and path is up to date.
instead of square thumbnail (just like there's no image and still image path is open), it should show the captcha
Code: index.php (For displaying on front end)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Captcha Demo - UandBlog</title>
</head>
<body style="background-color:#D8D8D8;">
<div style="width:500px; margin:0 auto; margin-top:100px; background:#F0F0F0; padding:10px;">
<h2>Captcha Demo - UandBlog</h2>
<form action="submitPage.php" method="post">
<table width="100%" border="0" cellspacing="5" cellpadding="5">
<tr style="color:#F00;">
<td colspan="2" style="font-family:Arial, Helvetica, sans-serif; font-size:15px;">
<?php if(isset($_REQUEST['msg1'])){?>
<?php echo $_REQUEST['msg1'];?>
<?php } ?>
</td>
</tr>
<tr>
<td>Name:</td>
<td><input name="name" type="text" required></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="email" type="email"></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="msg" cols="" rows=""></textarea></td>
</tr>
<tr>
<td style=" text-transform:uppercase;" valign="top">captcha:</td>
<td>*Please fill in the captcha security below</td>
</tr>
<tr>
<td valign="top"> </td>
<td ><input id="6_letters_code_1" name="6_letters_code_1" required type="text" style="border:1px solid #e2d5ca !important;"></td>
</tr>
<tr>
<td class="blk" valign="top"> </td>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="text-align:left;"> <img src="captcha_code_file.php?rand=<?php echo rand();?>" id='captchaimg' width="40%"></td>
<td style="text-align:right;"></td>
</tr>
<tr>
<td colspan="2" style="font-family:Arial, Helvetica, sans-serif; font-size:11px;"> Can't read the image? click <a href='javascript: refreshCaptcha();' style="color:#F00;">here</a> to refresh</td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td>
<td><button class="submit_btn" name="Submit1" type="submit">Submit</button></td>
</tr>
</table>
</form>
</div>
<script type='text/javascript'>
function refreshCaptcha()
{
var img = document.images['captchaimg'];
img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
</body>
</html>
Code: captcha_code_file.php (for generating captcha image)
<?php
session_start();
//Settings: You can customize the captcha here
$image_width = 200;
$image_height = 60;
$characters_on_image = 5;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 140;
$random_lines = 5;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
$_SESSION['6_letters_code_1'] = $code;
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
?>
Upvotes: 1
Views: 547
Reputation: 33823
After re-working the captcha code I figured the issue - namely that the font path is incorrect ( certainly on my system it is ) and when a fully qualified path was used the captcha works fine. So - I'd suggest using a full path to the font used as opposed to the ./
syntax you have here... I modified the hexrgb
funtion to output an object simply because I prefer it.
If, with your code, you comment out the last few lines that output the image ( or try to ) you ought to get ( well, I did ) an error message
Warning: imagettfbbox() [function.imagettfbbox.html]: Invalid font filename in.....
<?php
session_start();
$width = 200;
$height = 60;
$characters = 5;
$font = 'c:/wwwroot/webfonts/arial.ttf';
$chars = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 140;
$random_lines = 5;
$captcha_text_color="0x142864";
$captcha_noise_color = "0x142864";
$code = '';
$i = 0;
while( $i < $characters ) {
$code .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
$i++;
}
$font_size = $height * 0.75;
$image = @imagecreate( $width, $height );
$background_color = imagecolorallocate($image, 255, 255, 255);
$rgb = hexrgb( $captcha_text_color );
$text_color = imagecolorallocate($image,
$rgb->r,
$rgb->g,
$rgb->b
);
$rgb = hexrgb( $captcha_noise_color );
$noise_color = imagecolorallocate($image,
$rgb->r,
$rgb->g,
$rgb->b
);
/* generating the dots randomly in background */
for( $i=0; $i < $random_dots; $i++ ) {
imagefilledellipse( $image, mt_rand( 0, $width ), mt_rand( 0, $height ), 2, 3, $noise_color );
}
/* generating lines randomly in background of image */
for( $i=0; $i < $random_lines; $i++ ) {
imageline( $image, mt_rand( 0, $width ), mt_rand( 0, $height ), mt_rand( 0, $width ), mt_rand( 0, $height ), $noise_color );
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox( $font_size, 0, $font, $code );
$x = ( $width - $textbox[4] )/2;
$y = ( $height - $textbox[5] )/2;
imagettftext( $image, $font_size, 0, $x, $y, $text_color, $font , $code );
header('Content-Type: image/jpeg');
imagejpeg( $image );
imagedestroy( $image );
$_SESSION['6_letters_code_1'] = $code;
function hexrgb( $hexstr ){
$int = hexdec( $hexstr );
return (object)array(
'r' => 0xFF & ($int >> 0x10),
'g' => 0xFF & ($int >> 0x8),
'b' => 0xFF & $int
);
}
?>
An example captcha it produced
Upvotes: 1