Page 1 of 1

Verification code error

Posted: Sun Mar 19, 2006 11:45 am
by Citizen
When I run this script:

Code: Select all

<?php

function gen_rand_img()
{
    /**
     *    We define some static items here
     */

    ## Define our random sentences
    $sentences = array(
                                    'This is a random string',
                                    'Lorum Ipsum Doler sit amet',
                                    'The quick brown fox jumps over the lazy dog',
                                    'Kick the tires and light the fires Big Daddy'
                                    );

    ## Define font file
    putenv('GDFONTPATH='.realpath('.')); // Set GD font path to this directory
    $font = 'ProggyCleanSZBP'; // Leave extension off

    ## Define some random colors
    ## http://december.com/html/spec/colordec.html
    $bg_colors = array( ## BACKGROUND COLORS
                                    '176.196.222', // Blue
                                    '204.153.204', // Purple
                                    '204.204.204', // Gray
                                    '227.81.82', // Red
                                    '150.200.162'     // Green
                                    );
    $font_colors = array( ## FONT COLORS
                                    '0.0.139', // Blue
                                    '104.34.139', // Purple
                                    '79.79.79', // Gray
                                    '128.0.0', // Red
                                    '59.94.15'     // Green
                                    );

    $img_width = 150; ## Image Width
    $img_height = 75; ## Image height
    $fnt_size = 24;  ## Font-Size
    $let_space = 10; ## Letter Spacing
    $str_length = 6; ## Length of Random String
    /**************/

    ## Define random string generation function:
    function gen_random_string($length=5, $str='')
    {
        for($i=1; $i<=$length; $i++)
        {
            $ord = rand(48, 90);

            if( ( ($ord>=48) && ($ord<=57) ) || ( ($ord>=65) && ($ord<=90) ) )
            {
                $str .= chr($ord);
            }
            else
            {
                $str .= gen_random_string(1);
            }
        }
        return $str;
    }

    ## Get the random string
    $str_snt = rand(0,1); // 0: sentences, 1: random string

    if($str_snt == 0)
    {
        $rnd_str = str_replace(' ', '', strtoupper($sentences[rand(0,count($sentences)-1)]));
        $rnd_str = substr($rnd_str, rand(0,strlen($rnd_str)-$str_length), $str_length);
    }
    else
    {
        $rnd_str = gen_random_string($str_length);
    }

    /**
     *    Image functions
     */
    ## Create the image
    $img = imagecreate($img_width, $img_height) or die('Can not initialize GD Image Library');

    ## Define Background & Text colors
    list($br, $bg, $bb) = explode('.', $bg_colors[rand(0, count($bg_colors)-1)]);
    list($tr, $tg, $tb) = explode('.', $font_colors[rand(0, count($font_colors)-1)]);
    $bg_color = imagecolorallocate($img, $br, $bg, $bb);
    $txt_color = imagecolorallocate($img, $tr, $tg, $tb);
    $line_clr = imagecolorallocate($img, 0, 0, 0);

    ## Create box around image
    imageline($img, 0, 0, $img_width, 0, $line_clr); # Top left to top right
    imageline($img, 0, 0, 0, $img_height, $line_clr); # Top left to bottom left
    imageline($img, 0, $img_height-1, $img_width, $img_height-1, $line_clr); # Bottom Left to Bottom right
    imageline($img, $img_width-1, 0, $img_width-1, $img_height, $line_clr); # Top right to bottom Right

    ## Write string into image
    for($i=0; $i<strlen($rnd_str); $i++)
    {
        imagettftext($img, $fnt_size, rand(-30, 30), (($i*$fnt_size)+$let_space), rand($fnt_size+5, ($img_height-$fnt_size-5)), $txt_color, $font, $rnd_str{$i});
    }

    imagegif($img, 'rand_image.gif');
    return $rnd_str;
}

if(isset($_POST) && !empty($_POST))
{
    if(md5(strtoupper($_POST['vcode'])) == $_POST['_vcode'])
    {
        echo '<h3 style="color: #090;">Verified!!</h3>';
    }
    else
    {
        echo '<h3 style="color: #f00;">Unverified!!</h3>';
    }
}

$str = gen_rand_img();

?>
I get this error:

Fatal error: Call to undefined function: imagettftext() in /home/visitsha/public_html/vcode.php on line 99

Posted: Sun Mar 19, 2006 1:38 pm
by kaYak
Perhaps you do not have the GD library installed on your server? This function requires this library to be installed along with the FreeType library. :arrow: http://us2.php.net/imagettftext. That is the only explanation I can think of.

Posted: Sun Mar 19, 2006 2:00 pm
by Citizen
How do I install that on my server? Is there a better way to do image verificaiton?

Posted: Sun Mar 19, 2006 3:16 pm
by kaYak
If you own a shared hosting account I would recommend asking the host to please install it if that is the case. It will apply to all people using php on the server so it is likely that the server admin is the only one allowed to install it.

Posted: Sun Mar 19, 2006 3:20 pm
by John Cartwright
or if you want to do it yourself,
http://php.net/gd wrote: Installation

To enable GD-support configure PHP --with-gd[=DIR], where DIR is the GD base install directory. To use the recommended bundled version of the GD library (which was first bundled in PHP 4.3.0), use the configure option --with-gd. GD library requires libpng and libjpeg to compile.

In Windows, you'll include the GD2 DLL php_gd2.dll as an extension in php.ini. The GD1 DLL php_gd.dll was removed in PHP 4.3.2. Also note that the preferred truecolor image functions, such as imagecreatetruecolor(), require GD2.

Note: To enable exif support in Windows, php_mbstring.dll must be loaded prior to php_exif.dll in php.ini.

To disable GD support in PHP 3 add --without-gd to your configure line.

Enhance the capabilities of GD to handle more image formats by specifying the --with-XXXX configure switch to your PHP configure line.