467,921 Members | 1,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,921 developers. It's quick & easy.

Error in function imagettfbbox()

eragon
256MB
Im trying to put a captcha image in my site to stop people from spamming, and when i view the image its self i get this:

Fatal error: Call to undefined function imagettfbbox() in (...)/htdocs/CaptchaSecurityImages.php on line 39

so heres my code:

[php]
<?php
session_start();


class CaptchaSecurityImages {

var $font = 'monofont.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',$c haracters='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 */
header('Content-Type: image/jpeg');
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'] > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>
[/php]

i hope you guys can help, im lost!
Nov 19 '07 #1
  • viewed: 16360
Share:
12 Replies
eragon
256MB
if you would like to see my php info you can click here:

http://player-killer-clan.110mb.com/pifo.php

if that helps at all in any way.

thanks ;)
Nov 19 '07 #2
code green
Expert 1GB
You are calling a function that doesn't exist here
[PHP]$textbox = imagettfbbox($font_size, 0, $this->font, $code)[/PHP]
Is the function a member function?
If so you need to call it as such
[PHP]$textbox = $this->imagettfbbox($font_size, 0, $this->font, $code)[/PHP]
Nov 19 '07 #3
eragon
256MB
i changed that part of the code

Fatal error: Call to undefined method CaptchaSecurityImages::imagettfbbox() in /www/110mb.com/p/l/a/y/e/r/-/k/player-killer-clan/htdocs/CaptchaSecurityImages.php on line 39

now i get that...

or can you suggest another form of making a captcha?
Nov 19 '07 #4
brettl
Your code above should work.. Just make sure that your font and CaptchaSecurityImages.php are in the same directory. Also make sure you have the GD library turned on as well. To include it in your form just use:
Expand|Select|Wrap|Line Numbers
  1. <img src="CaptchaSecurityImages.php?width=160&height=80&characters=5" alt="captcha" />
  2.  
In this example the CaptchaSecurityImages.php is in the same directory as the files that host your form. The width, height and the number of characters are up to you to decide. I'm really not sure where you are calling imagettfbbox() and I don't think you have to make your Captcha work.

Let me know if this works.
Nov 19 '07 #5
brettl
O wait your code should look something like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. function generateCode($characters) {
  4.       /* list all possible characters, similar looking characters and vowels have been removed */
  5.       $possible = '23456789bcdfghjkmnpqrstvwxyz';
  6.       $code = '';
  7.       $i = 0;
  8.       while ($i < $characters) {
  9.          $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  10.          $i++;
  11.       }
  12.       return $code;
  13.  
  14.    }
  15.  
  16.    function CaptchaSecurityImages($width='160',$height='80',$characters='5') {
  17.       $code = $this->generateCode($characters);
  18.       /* font size will be 45% of the image height */
  19.       $font_size = $height * 0.45;
  20.       $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  21.       /* set the colours */
  22.       $background_color = imagecolorallocate($image, 255, 255, 255);
  23.       $text_color = imagecolorallocate($image, 20, 40, 100);
  24.       $noise_color = imagecolorallocate($image, 100, 120, 180);
  25.       /* generate random dots in background */
  26.       for( $i=0; $i<($width*$height)/3; $i++ ) {
  27.          imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  28.       }
  29.       /* generate random lines in background */
  30.       for( $i=0; $i<($width*$height)/150; $i++ ) {
  31.          imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  32.       }
  33.       /* create textbox and add text */
  34.       $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
  35.       $x = ($width - $textbox[4])/2;
  36.       $y = ($height - $textbox[5])/2;
  37.       imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
  38.       /* output captcha image to browser */
  39.       $_SESSION['code'] = $code;
  40.       header('Content-Type: image/jpeg');
  41.       imagejpeg($image);
  42.       imagedestroy($image);
  43.  
  44.  
  45.    }
  46.  
  47. }
  48.  
  49. $width = isset($_GET['width']) && $_GET['height'] < 600 ? $_GET['width'] : '120';
  50. $height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40';
  51. $characters = isset($_GET['characters']) && $_GET['characters'] > 2 ? $_GET['characters'] : '6';
  52.  
  53. $captcha = new CaptchaSecurityImages($width,$height,$characters);
  54.  
This should work. Again let me know.
Nov 19 '07 #6
eragon
256MB
Fatal error: Class 'CaptchaSecurityImages' not found in /www/110mb.com/p/l/a/y/e/r/-/k/player-killer-clan/htdocs/CaptchaSecurityImages.php on line 50

with

[php]
<?php
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='160',$height='80',$c haracters='5') {
$code = $this->generateCode($characters);
/* font size will be 45% of the image height */
$font_size = $height * 0.45;
$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 */
$_SESSION['code'] = $code;
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);


}

$width = isset($_GET['width']) && $_GET['height'] < 600 ? $_GET['width'] : '120';
$height = isset($_GET['height']) && $_GET['height'] < 200 ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 2 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);
?>
[/php]
Nov 20 '07 #7
eragon
256MB
im using this captcha now:

http://www.mindpalette.com/tutorials/captcha/connect.php

and it tells how to connect it ot these specila form thingamaboblets, but it doesn't say how to conneft it with my ismple if-then-else form validator. unless it does and im missing it. oh well, i need sleep, if anybody finds it could you be so kind as to share?

thanks :)
Nov 20 '07 #8
I had the same error.

I changed: "var $font = 'monofont.ttf';"
to "var $font = './monofont.ttf';"
and it worked.

I hate this types of errors ;-)...
Oct 5 '10 #9
Thank you very much Mike... It helps me a lot
Dec 6 '11 #10
It works great. This helps a lot.
Oct 27 '17 #11
2Bits
I am facing the same problem too, with the same code. I lost the procedure that I had done once before, had kept the steps in a txt file, and have forgotten what I had done. Now ever since I re-installed my Xampp, this is the issue taking place in the same version on my laptop, whereas in my PC it is working fine. Though I followed the instructions above, nothing seem to work. Can somebody help me out? Does the PHP.INI file needs to be fiddled with? or does this have anything to do with .htaccess file?
2 Weeks Ago #12
2Bits
The error is in the file php_gd2.dll. This file is located in: xampp/php/ext/php_gd2.dll. If I copy my older file from my PC to the laptop newly installed 32 bit xampp on windows 10 64bit, then the captcha and everything appears without any problem. But now the question is how to solve the xampp 8.0 64bit problem? there the file is php_gd.dll? Any solution to this file?
2 Weeks Ago #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Mairhtin O'Feannag | last post: by
6 posts views Thread by Peter Frost | last post: by
33 posts views Thread by Anthony England | last post: by
1 post views Thread by developer | last post: by
2 posts views Thread by laredotornado | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.