Image not created properly (GD imagecreate PHP)

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I'm in the process of creating some different CAPTCHAs for a system I'm creating (making them available in the admin control panel) and the script doesn't seem to want to work... I can't seem to pinpoint the problem too.

Here is the current CAPTCHA code that I have at the moment... can't go on with creating an actual CAPTCHA if the image creation is not working.
PHP Code: [ Select ]
class math
{
 
    // The captcha answer session name
    public $captcha_session = 'captcha';
   
    // Available fonts
    public $fonts = array('Alfredo_',
                          'almosnow',
                          'arial',
                          'arialbd',
                          'arialbi',
                          'ariali',
                          'ariblk',
                          'babyk__',
                          'bnjinx',
                          'bobcat',
                          'candles_',
                          'goodtime',
                          'Greek_i',
                          'littlelo',
                          'Martina_',
                          'micross',
                          'Minerva_',
                          'narrow',
                          'neon2',
                          'QUIVEIT_',
                          'RONDALO_',
                          'Russrite',
                          'sf movie_poster2',
                          'Skinny__',
                          'SPLASH__',
                          'Vivian__',
                          'woodcut');
   
    // The directory the font files are located (relative to viewed page)
    private $font_dir = './fonts/';
   
    // Used colors array
    private $colors = array();
   
    function math($content)
    {
        // Setting the content type
        $this->content_type = 'image/png';
       
        // Setting the font size
        $this->font_size = 12;
       
        // Creating the image resource
        $this->image = imagecreatetruecolor('150', '150');
       
        // Setting the image headers
        $this->set_headers();
       
        // Setting the font
        $this->set_font();
       
        // Setting the image colors
        $this->set_colors();
       
        // Draw lines before text
        $this->draw_lines(30);
       
        // Generating the content
        $this->generate_content($content);
       
        // Draw lines after text
        $this->draw_lines(20);
       
        // Displaying the output
        $this->_output();
    }
   
    private function set_headers()
    {
        // The image header... makes it possible to actually display the image
        header("Content-Type: " . $this->content_type);
    }
   
    private function set_font($font = 'rand')
    {
        // Checking if the requested font exists in the font array
        if(isset($this->fonts[$font]))
        {
            // It exists... use it as the font
            $this->font = $this->fonts[$font];
        }
        else
        {
            // Creating a random key within the range of existing keys in the font array
            $key = rand(0, count($this->fonts));
           
            // Using the font that the randomly created key selects
            $this->font = $this->fonts[$key];
        }
    }
   
    private function set_colors()
    {
        // A grey color (for the shadow)
        $this->colors['grey'] = imagecolorallocate($this->image, 128, 128, 128);
       
        // A black color (for the text)
        $this->colors['black'] = imagecolorallocate($this->image, 0, 0, 0);
    }
   
    private function generate_content($content)
    {
        // A random angle (up to 90 degrees) for the content
        $angle = rand(0, 90);
       
        // The font we are using in the captcha
        $font = $this->font_dir . $this->font . '.ttf';
       
        //imagestring($this->image, 1, 6, 6,  $content, $this->color['grey']);
        //imagestring($this->image, 1, 5, 5,  $content, $this->color['black']);
       
        // The shadow
        imagettftext($this->image, $this->font_size, $angle, 6, 131, $this->color['grey'], $font, $content);
       
        // The actual text
        imagettftext($this->image, $this->font_size, $angle, 5, 130, $this->color['black'], $font, $content);
    }
   
    private function _output()
    {
        // Output the PNG image
        imagepng($this->image);
       
        // Free memory
        imagedestroy($this->image);
    }
   
    private function draw_lines($amount)
    {
        // Starting the counter
        $count = 0;
       
        // Adding the lines... making sure they don't surpass the amount requested
        while($count < $amount)
        {
            // Adding a line for the current loop
            imageline($this->image, rand(0, 150), rand(0, 150), rand(0, 150), rand(0, 150), $this->rand_color());
           
            // Telling the counter we have added one line
            $count++;
        }
    }
   
    private function rand_color()
    {
        // Creating a completely random color
        return imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
    }
 
}
 
new math("anf4r");
?>
  1. class math
  2. {
  3.  
  4.     // The captcha answer session name
  5.     public $captcha_session = 'captcha';
  6.    
  7.     // Available fonts
  8.     public $fonts = array('Alfredo_',
  9.                           'almosnow',
  10.                           'arial',
  11.                           'arialbd',
  12.                           'arialbi',
  13.                           'ariali',
  14.                           'ariblk',
  15.                           'babyk__',
  16.                           'bnjinx',
  17.                           'bobcat',
  18.                           'candles_',
  19.                           'goodtime',
  20.                           'Greek_i',
  21.                           'littlelo',
  22.                           'Martina_',
  23.                           'micross',
  24.                           'Minerva_',
  25.                           'narrow',
  26.                           'neon2',
  27.                           'QUIVEIT_',
  28.                           'RONDALO_',
  29.                           'Russrite',
  30.                           'sf movie_poster2',
  31.                           'Skinny__',
  32.                           'SPLASH__',
  33.                           'Vivian__',
  34.                           'woodcut');
  35.    
  36.     // The directory the font files are located (relative to viewed page)
  37.     private $font_dir = './fonts/';
  38.    
  39.     // Used colors array
  40.     private $colors = array();
  41.    
  42.     function math($content)
  43.     {
  44.         // Setting the content type
  45.         $this->content_type = 'image/png';
  46.        
  47.         // Setting the font size
  48.         $this->font_size = 12;
  49.        
  50.         // Creating the image resource
  51.         $this->image = imagecreatetruecolor('150', '150');
  52.        
  53.         // Setting the image headers
  54.         $this->set_headers();
  55.        
  56.         // Setting the font
  57.         $this->set_font();
  58.        
  59.         // Setting the image colors
  60.         $this->set_colors();
  61.        
  62.         // Draw lines before text
  63.         $this->draw_lines(30);
  64.        
  65.         // Generating the content
  66.         $this->generate_content($content);
  67.        
  68.         // Draw lines after text
  69.         $this->draw_lines(20);
  70.        
  71.         // Displaying the output
  72.         $this->_output();
  73.     }
  74.    
  75.     private function set_headers()
  76.     {
  77.         // The image header... makes it possible to actually display the image
  78.         header("Content-Type: " . $this->content_type);
  79.     }
  80.    
  81.     private function set_font($font = 'rand')
  82.     {
  83.         // Checking if the requested font exists in the font array
  84.         if(isset($this->fonts[$font]))
  85.         {
  86.             // It exists... use it as the font
  87.             $this->font = $this->fonts[$font];
  88.         }
  89.         else
  90.         {
  91.             // Creating a random key within the range of existing keys in the font array
  92.             $key = rand(0, count($this->fonts));
  93.            
  94.             // Using the font that the randomly created key selects
  95.             $this->font = $this->fonts[$key];
  96.         }
  97.     }
  98.    
  99.     private function set_colors()
  100.     {
  101.         // A grey color (for the shadow)
  102.         $this->colors['grey'] = imagecolorallocate($this->image, 128, 128, 128);
  103.        
  104.         // A black color (for the text)
  105.         $this->colors['black'] = imagecolorallocate($this->image, 0, 0, 0);
  106.     }
  107.    
  108.     private function generate_content($content)
  109.     {
  110.         // A random angle (up to 90 degrees) for the content
  111.         $angle = rand(0, 90);
  112.        
  113.         // The font we are using in the captcha
  114.         $font = $this->font_dir . $this->font . '.ttf';
  115.        
  116.         //imagestring($this->image, 1, 6, 6,  $content, $this->color['grey']);
  117.         //imagestring($this->image, 1, 5, 5,  $content, $this->color['black']);
  118.        
  119.         // The shadow
  120.         imagettftext($this->image, $this->font_size, $angle, 6, 131, $this->color['grey'], $font, $content);
  121.        
  122.         // The actual text
  123.         imagettftext($this->image, $this->font_size, $angle, 5, 130, $this->color['black'], $font, $content);
  124.     }
  125.    
  126.     private function _output()
  127.     {
  128.         // Output the PNG image
  129.         imagepng($this->image);
  130.        
  131.         // Free memory
  132.         imagedestroy($this->image);
  133.     }
  134.    
  135.     private function draw_lines($amount)
  136.     {
  137.         // Starting the counter
  138.         $count = 0;
  139.        
  140.         // Adding the lines... making sure they don't surpass the amount requested
  141.         while($count < $amount)
  142.         {
  143.             // Adding a line for the current loop
  144.             imageline($this->image, rand(0, 150), rand(0, 150), rand(0, 150), rand(0, 150), $this->rand_color());
  145.            
  146.             // Telling the counter we have added one line
  147.             $count++;
  148.         }
  149.     }
  150.    
  151.     private function rand_color()
  152.     {
  153.         // Creating a completely random color
  154.         return imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
  155.     }
  156.  
  157. }
  158.  
  159. new math("anf4r");
  160. ?>

I'm getting the following error:
Quote:
The image "http://127.0.0.1/www/captcha/captcha/math.php" cannot be displayed because it contains errors.

The script used to work fine with imgestring() function (used in generate_content() function) but then I converted to imagettftext (I used it 5 years ago in a different CAPTCHA code which worked) but it doesn't work... (I also changed a lot of other stuff [like add fonts] when I changed to imagettftext) and now it doesn't work...
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Nevermind, I figured it out!! :D In generate_content(); I used $this->color['..'] where it should be $this->colors['...'].

And also, the following function helped me solve the problem where some of the fonts in the font array were written wrong (oops... I tried my best).
PHP Code: [ Select ]
    public function fexists()
    {
        echo count($this->fonts) . " available fonts<br />";
        echo "<hr />";
        foreach($this->fonts as $key => $font)
        {
            if($this->font == $font)
            {
                echo "<u><strong>{$key}</strong> => {$font}</u><br />";
            }
            else
            {
                echo "<strong>{$key}</strong> => {$font}<br />";
            }
           
            if(!file_exists($this->font_dir . $font . '.ttf'))
            {
                echo "<strong>{$this->font_dir}{$font}.ttf</strong> is not right!<br />";
            }
        }
    }
  1.     public function fexists()
  2.     {
  3.         echo count($this->fonts) . " available fonts<br />";
  4.         echo "<hr />";
  5.         foreach($this->fonts as $key => $font)
  6.         {
  7.             if($this->font == $font)
  8.             {
  9.                 echo "<u><strong>{$key}</strong> => {$font}</u><br />";
  10.             }
  11.             else
  12.             {
  13.                 echo "<strong>{$key}</strong> => {$font}<br />";
  14.             }
  15.            
  16.             if(!file_exists($this->font_dir . $font . '.ttf'))
  17.             {
  18.                 echo "<strong>{$this->font_dir}{$font}.ttf</strong> is not right!<br />";
  19.             }
  20.         }
  21.     }

What helped me solve all of these errors (from the start) is commenting out the headers... once the PNG's headers were stopped from being sent to the browser, I started getting the error "Unknown variable $color on line ##"... something to that effect.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Here is the correct code...
PHP Code: [ Select ]
<?php
class math
{
 
    // The captcha answer session name
    public $captcha_session = 'captcha';
   
    // Available fonts
    private $fonts = array('arial',
                           'arialbd',
                           'arialbi',
                           'ariali',
                           'ariblk',
                           'bnjinx',
                           'bobcat',
                           'candles_',
                           'micross',
                           'narrow',
                           'neon2'
                           );
   
    // The directory the font files are located (relative to viewed page)
    private $font_dir = './fonts/';
   
    // Used colors array
    private $colors = array();
   
    function math($content)
    {
        // Setting the content type
        $this->content_type = 'image/png';
       
        // Setting the font size
        $this->font_size = 30;
       
        // Creating the image resource
        $this->image = imagecreatetruecolor('150', '150');
       
        // Setting the image headers
        $this->set_headers();
       
        // Setting the image colors
        $this->set_colors();
       
        // Setting the font
        $this->set_font();
       
        // Draw lines before text
        $this->draw_lines(30);
       
        // Generating the content
        $this->generate_content($content);
       
        // Draw lines after text
        $this->draw_lines(20);
       
        // Displaying the output
        $this->_output();
    }
   
    private function set_headers()
    {
        // The image header... makes it possible to actually display the image
        header("Content-Type: " . $this->content_type);
    }
   
    private function set_font($font = 'rand')
    {
        // Checking if the requested font exists in the font array
        if(isset($this->fonts[$font]))
        {
            // It exists... use it as the font
            $this->font = $this->fonts[$font];
        }
        else
        {
            // Creating a random key within the range of existing keys in the font array
            $key = rand(0, (count($this->fonts) - 1));
           
            // Using the font that the randomly created key selects
            $this->font = $this->fonts[$key];
        }
    }
   
    private function set_colors()
    {
        // A grey color (for the shadow)
        $this->colors['grey'] = imagecolorallocate($this->image, 128, 128, 128);
       
        // A black color (for the text)
        $this->colors['white'] = imagecolorallocate($this->image, 255, 255, 255);
    }
   
    private function generate_content($content)
    {
        // A random angle (up to 90 degrees) for the content
        $angle = rand(0, 90);
       
        // The font we are using in the captcha
        $font = $this->font_dir . $this->font . '.ttf';
       
        // The shadow
        imagettftext($this->image, ($this->font_size + 2), $angle, 30, 129, $this->colors['grey'], $font, $content);
       
        // The actual text
        imagettftext($this->image, $this->font_size, $angle, 31, 130, $this->colors['white'], $font, $content);
    }
   
    private function _output()
    {
        // Output the PNG image
        imagepng($this->image);
       
        // Free memory
        imagedestroy($this->image);
    }
   
    private function draw_lines($amount)
    {
        // Starting the counter
        $count = 0;
       
        // Adding the lines... making sure they don't surpass the amount requested
        while($count < $amount)
        {
            // Adding a line for the current loop
            imageline($this->image, rand(0, 150), rand(0, 150), rand(0, 150), rand(0, 150), $this->rand_color());
           
            // Telling the counter we have added one line
            $count++;
        }
    }
   
    private function rand_color()
    {
        // Creating a completely random color
        return imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
    }
 
    public function fexists()
    {
        echo count($this->fonts) . " available fonts<br />";
        echo "<hr />";
        foreach($this->fonts as $key => $font)
        {
            if($this->font == $font)
            {
                echo "<u><strong>{$key}</strong> => {$font}</u><br />";
            }
            else
            {
                echo "<strong>{$key}</strong> => {$font}<br />";
            }
           
            if(!file_exists($this->font_dir . $font . '.ttf'))
            {
                echo "<strong>{$this->font_dir}{$font}.ttf</strong> is not right!<br />";
            }
        }
    }
}
 
$cap = new math("2+3-3");
 
//$cap->fexists();
?>
  1. <?php
  2. class math
  3. {
  4.  
  5.     // The captcha answer session name
  6.     public $captcha_session = 'captcha';
  7.    
  8.     // Available fonts
  9.     private $fonts = array('arial',
  10.                            'arialbd',
  11.                            'arialbi',
  12.                            'ariali',
  13.                            'ariblk',
  14.                            'bnjinx',
  15.                            'bobcat',
  16.                            'candles_',
  17.                            'micross',
  18.                            'narrow',
  19.                            'neon2'
  20.                            );
  21.    
  22.     // The directory the font files are located (relative to viewed page)
  23.     private $font_dir = './fonts/';
  24.    
  25.     // Used colors array
  26.     private $colors = array();
  27.    
  28.     function math($content)
  29.     {
  30.         // Setting the content type
  31.         $this->content_type = 'image/png';
  32.        
  33.         // Setting the font size
  34.         $this->font_size = 30;
  35.        
  36.         // Creating the image resource
  37.         $this->image = imagecreatetruecolor('150', '150');
  38.        
  39.         // Setting the image headers
  40.         $this->set_headers();
  41.        
  42.         // Setting the image colors
  43.         $this->set_colors();
  44.        
  45.         // Setting the font
  46.         $this->set_font();
  47.        
  48.         // Draw lines before text
  49.         $this->draw_lines(30);
  50.        
  51.         // Generating the content
  52.         $this->generate_content($content);
  53.        
  54.         // Draw lines after text
  55.         $this->draw_lines(20);
  56.        
  57.         // Displaying the output
  58.         $this->_output();
  59.     }
  60.    
  61.     private function set_headers()
  62.     {
  63.         // The image header... makes it possible to actually display the image
  64.         header("Content-Type: " . $this->content_type);
  65.     }
  66.    
  67.     private function set_font($font = 'rand')
  68.     {
  69.         // Checking if the requested font exists in the font array
  70.         if(isset($this->fonts[$font]))
  71.         {
  72.             // It exists... use it as the font
  73.             $this->font = $this->fonts[$font];
  74.         }
  75.         else
  76.         {
  77.             // Creating a random key within the range of existing keys in the font array
  78.             $key = rand(0, (count($this->fonts) - 1));
  79.            
  80.             // Using the font that the randomly created key selects
  81.             $this->font = $this->fonts[$key];
  82.         }
  83.     }
  84.    
  85.     private function set_colors()
  86.     {
  87.         // A grey color (for the shadow)
  88.         $this->colors['grey'] = imagecolorallocate($this->image, 128, 128, 128);
  89.        
  90.         // A black color (for the text)
  91.         $this->colors['white'] = imagecolorallocate($this->image, 255, 255, 255);
  92.     }
  93.    
  94.     private function generate_content($content)
  95.     {
  96.         // A random angle (up to 90 degrees) for the content
  97.         $angle = rand(0, 90);
  98.        
  99.         // The font we are using in the captcha
  100.         $font = $this->font_dir . $this->font . '.ttf';
  101.        
  102.         // The shadow
  103.         imagettftext($this->image, ($this->font_size + 2), $angle, 30, 129, $this->colors['grey'], $font, $content);
  104.        
  105.         // The actual text
  106.         imagettftext($this->image, $this->font_size, $angle, 31, 130, $this->colors['white'], $font, $content);
  107.     }
  108.    
  109.     private function _output()
  110.     {
  111.         // Output the PNG image
  112.         imagepng($this->image);
  113.        
  114.         // Free memory
  115.         imagedestroy($this->image);
  116.     }
  117.    
  118.     private function draw_lines($amount)
  119.     {
  120.         // Starting the counter
  121.         $count = 0;
  122.        
  123.         // Adding the lines... making sure they don't surpass the amount requested
  124.         while($count < $amount)
  125.         {
  126.             // Adding a line for the current loop
  127.             imageline($this->image, rand(0, 150), rand(0, 150), rand(0, 150), rand(0, 150), $this->rand_color());
  128.            
  129.             // Telling the counter we have added one line
  130.             $count++;
  131.         }
  132.     }
  133.    
  134.     private function rand_color()
  135.     {
  136.         // Creating a completely random color
  137.         return imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
  138.     }
  139.  
  140.     public function fexists()
  141.     {
  142.         echo count($this->fonts) . " available fonts<br />";
  143.         echo "<hr />";
  144.         foreach($this->fonts as $key => $font)
  145.         {
  146.             if($this->font == $font)
  147.             {
  148.                 echo "<u><strong>{$key}</strong> => {$font}</u><br />";
  149.             }
  150.             else
  151.             {
  152.                 echo "<strong>{$key}</strong> => {$font}<br />";
  153.             }
  154.            
  155.             if(!file_exists($this->font_dir . $font . '.ttf'))
  156.             {
  157.                 echo "<strong>{$this->font_dir}{$font}.ttf</strong> is not right!<br />";
  158.             }
  159.         }
  160.     }
  161. }
  162.  
  163. $cap = new math("2+3-3");
  164.  
  165. //$cap->fexists();
  166. ?>
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

Any reason you're doing this from scratch rather than using reCAPTCHA?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

None really, more of a learning experience for me. Create a system and then work on making it faster and better. It's not a project I'm working for someone, it's something I started myself for the sake of strengthening my PHP knowledge (while I learn C).

Also I'm in more power of how things work if it's made by me.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

No worries, learning is always a good reason :)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 175 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.