i am trying to create my own captcha on my page for log in but this php code won't display my captcha image on my web page. so any suggetion will be good. this is my captcha.php
<?php
session_start();
// generate random number and store in session
$randomnr = rand(1000, 9999);
$_SESSION['randomnr2'] = md5($randomnr);
//generate image
$im = imagecreatetruecolor(100, 38);
//colors:
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 200, 35, $black);
// ------------- your fontname -------------
// example font http://www.webpagepublicity.com/free-fonts/a/Anklepants.ttf
$font = 'Anklepants.ttf';
//draw text:
imagettftext($im, 35, 0, 22, 24, $grey, $font, $randomnr);
imagettftext($im, 35, 0, 15, 26, $white, $font, $randomnr);
// prevent client side caching
header("Expires: Wed, 1 Jan 2015 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revаlidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//send image to browser
header ("Content-type: image/PNG");
imagegif($im);
imagedestroy($im);
?>
and my html code to display the captcha image it is trial code.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>books Collection</title>
</head>
<body>
<div >
<form>
<img src="captcha.php" /></br>
<img src="captcha.png" /></br>
<input type="text" name="answer" placeholder="Enter captcha here" />
<input type="submit" value="CHECK" />
</form>
</div>
</body>
</html>
There is definitely some problem with your
ttf
font. Try using another font file or either the font size is too large or too small. I tested your script with another fontAkashi.ttf
and it works flawlessly. Here is the screen capture:try this
or