Hi, I am getting this error: Warning: imagettftext() [function.imagettftext]: Could not find/open font in /xxxxxxx/x/xxxxxxxx/xxxx/xxxx/image.php on line 39 Code (markup): And this is my code: <?php session_start(); $font = "captcha.ttf"; $img = imagecreatetruecolor(200,30); $white = imagecolorallocate($img, 255, 255, 255); $black = imagecolorallocate($img, 0, 0, 0); $grey = imagecolorallocate($img,150,150,150); $red = imagecolorallocate($img, 255, 0, 0); $pink = imagecolorallocate($img, 200, 0, 150); $bg = imagecolorallocate($img, 176, 196, 222); $length = 5; function randomString($length){ $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*1000000); $str = ""; $i = 0; while($i <= $length){ $num = rand() % 33; $tmp = substr($chars, $num, 1); $str = $str . $tmp; $i++; } return $str; } for($i=1;$i<=rand(1,3);$i++){ $color = (rand(1,2) == 1) ? $pink : $red; imageline($img,rand(5,70),rand(5,20), rand(5,70)+5,rand(5,20)+5, $color); } imagefill($img, 0, 0, $bg); $string = randomString(rand(3,4)); $_SESSION['string'] = $string; imagettftext($img, 20, 0, 15, 20, $black, "captcha.ttf", $string); imagepng($img); imagedestroy($img); ?> PHP: Any idea the error is? Thank you.
I am using this code but with other font. I have replaced the font name with your font. It is working here in my all websites. Please try this code. Hope this will solve your problem session_start(); class CaptchaSecurityImages { var $font = 'captcha.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 CaptchaSecurityImages($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 */ imagejpeg($image); imagedestroy($image); $_SESSION['security_code'] = $code; } } $width = isset($_GET['width']) ? $_GET['width'] : '120'; $height = isset($_GET['height']) ? $_GET['height'] : '40'; $characters = isset($_GET['characters']) ? $_GET['characters'] : '6'; header('Content-Type: image/jpeg'); $captcha = new CaptchaSecurityImages($width,$height,$characters); Code (markup): here is the form where I am using it <?php session_start(); if( isset($_POST['submit'])) { if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. echo 'Thank you. Your message said "'.$_POST['message'].'"'; } else { // Insert your code for showing an error message here echo 'Sorry, you have provided an invalid security code'; } } else { ?> <form action="form.php" method="post"> <label for="name">Name: </label><input type="text" name="name" id="name" /><br /> <label for="email">Email: </label><input type="text" name="email" id="email" /><br /> <label for="message">Message: </label><textarea rows="5" cols="30" name="message" id="message"></textarea><br /> <img src="CaptchaSecurityImages.php?width=200&height=60&characters=8" /><br /> <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br /> <input type="submit" name="submit" value="Submit" /> </form> <?php } ?> Code (markup):