Error in function imagettfbbox()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eragon
    Contributor
    • Mar 2007
    • 431

    Error in function imagettfbbox()

    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/CaptchaSecurity Images.php on line 39

    so heres my code:

    [php]
    <?php
    session_start() ;


    class CaptchaSecurity Images {

    var $font = 'monofont.ttf';

    function generateCode($c haracters) {
    /* list all possible characters, similar looking characters and vowels have been removed */
    $possible = '23456789bcdfgh jkmnpqrstvwxyz' ;
    $code = '';
    $i = 0;
    while ($i < $characters) {
    $code .= substr($possibl e, mt_rand(0, strlen($possibl e)-1), 1);
    $i++;
    }
    return $code;
    }

    function CaptchaSecurity Images($width=' 120',$height='4 0',$characters= '6') {
    $code = $this->generateCode($ characters);
    /* font size will be 75% of the image height */
    $font_size = $height * 0.75;
    $image = @imagecreate($w idth, $height) or die('Cannot initialize new GD image stream');
    /* set the colours */
    $background_col or = imagecoloralloc ate($image, 255, 255, 255);
    $text_color = imagecoloralloc ate($image, 20, 40, 100);
    $noise_color = imagecoloralloc ate($image, 100, 120, 180);
    /* generate random dots in background */
    for( $i=0; $i<($width*$hei ght)/3; $i++ ) {
    imagefilledelli pse($image, mt_rand(0,$widt h), mt_rand(0,$heig ht), 1, 1, $noise_color);
    }
    /* generate random lines in background */
    for( $i=0; $i<($width*$hei ght)/150; $i++ ) {
    imageline($imag e, mt_rand(0,$widt h), mt_rand(0,$heig ht), mt_rand(0,$widt h), mt_rand(0,$heig ht), $noise_color);
    }
    /* create textbox and add text */
    $textbox = imagettfbbox($f ont_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
    $x = ($width - $textbox[4])/2;
    $y = ($height - $textbox[5])/2;
    imagettftext($i mage, $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($imag e);
    imagedestroy($i mage);
    $_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 CaptchaSecurity Images($width,$ height,$charact ers);

    ?>
    [/php]

    i hope you guys can help, im lost!
  • eragon
    Contributor
    • Mar 2007
    • 431

    #2
    if you would like to see my php info you can click here:



    if that helps at all in any way.

    thanks ;)

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      You are calling a function that doesn't exist here
      [PHP]$textbox = imagettfbbox($f ont_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]

      Comment

      • eragon
        Contributor
        • Mar 2007
        • 431

        #4
        i changed that part of the code

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

        now i get that...

        or can you suggest another form of making a captcha?

        Comment

        • brettl
          New Member
          • Sep 2007
          • 41

          #5
          Your code above should work.. Just make sure that your font and CaptchaSecurity Images.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:
          Code:
          <img src="CaptchaSecurityImages.php?width=160&height=80&characters=5" alt="captcha" />
          In this example the CaptchaSecurity Images.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.

          Comment

          • brettl
            New Member
            • Sep 2007
            • 41

            #6
            O wait your code should look something like this:
            Code:
             
            
            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',$characters='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);
            This should work. Again let me know.

            Comment

            • eragon
              Contributor
              • Mar 2007
              • 431

              #7
              Fatal error: Class 'CaptchaSecurit yImages' not found in /www/110mb.com/p/l/a/y/e/r/-/k/player-killer-clan/htdocs/CaptchaSecurity Images.php on line 50

              with

              [php]
              <?php
              function generateCode($c haracters) {
              /* list all possible characters, similar looking characters and vowels have been removed */
              $possible = '23456789bcdfgh jkmnpqrstvwxyz' ;
              $code = '';
              $i = 0;
              while ($i < $characters) {
              $code .= substr($possibl e, mt_rand(0, strlen($possibl e)-1), 1);
              $i++;
              }
              return $code;

              }

              function CaptchaSecurity Images($width=' 160',$height='8 0',$characters= '5') {
              $code = $this->generateCode($ characters);
              /* font size will be 45% of the image height */
              $font_size = $height * 0.45;
              $image = imagecreate($wi dth, $height) or die('Cannot initialize new GD image stream');
              /* set the colours */
              $background_col or = imagecoloralloc ate($image, 255, 255, 255);
              $text_color = imagecoloralloc ate($image, 20, 40, 100);
              $noise_color = imagecoloralloc ate($image, 100, 120, 180);
              /* generate random dots in background */
              for( $i=0; $i<($width*$hei ght)/3; $i++ ) {
              imagefilledelli pse($image, mt_rand(0,$widt h), mt_rand(0,$heig ht), 1, 1, $noise_color);
              }
              /* generate random lines in background */
              for( $i=0; $i<($width*$hei ght)/150; $i++ ) {
              imageline($imag e, mt_rand(0,$widt h), mt_rand(0,$heig ht), mt_rand(0,$widt h), mt_rand(0,$heig ht), $noise_color);
              }
              /* create textbox and add text */
              $textbox = imagettfbbox($f ont_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
              $x = ($width - $textbox[4])/2;
              $y = ($height - $textbox[5])/2;
              imagettftext($i mage, $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($imag e);
              imagedestroy($i mage);


              }

              $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 CaptchaSecurity Images($width,$ height,$charact ers);
              ?>
              [/php]

              Comment

              • eragon
                Contributor
                • Mar 2007
                • 431

                #8
                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 :)

                Comment

                • Mike Captcha

                  #9
                  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 ;-)...

                  Comment

                  • epsita123
                    New Member
                    • Dec 2011
                    • 1

                    #10
                    Thank you very much Mike... It helps me a lot

                    Comment

                    • payal1993
                      New Member
                      • Oct 2017
                      • 1

                      #11
                      Thank u soo much

                      It works great. This helps a lot.

                      Comment

                      • Bushka Ferns
                        New Member
                        • Feb 2021
                        • 2

                        #12
                        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?
                        Last edited by Bushka Ferns; Feb 18 '21, 10:17 PM. Reason: had forgotten a few points

                        Comment

                        • Bushka Ferns
                          New Member
                          • Feb 2021
                          • 2

                          #13
                          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?

                          Comment

                          Working...