GD Library Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akkad
    New Member
    • Jun 2007
    • 18

    GD Library Problem

    ok let met tell u what is the problem , i have downloaded the GD library code from interent and when i am using it the picture is not appearing on the page. i don't know why ?

    i am using PHP 4.3.9 on Appache server under linux operating system ( fedora 3).
    though the same code it is working on the localhost (WAMP) with php 5.2 and apche 2.2
    so plz if someone have an idea about the problem plz answer me.





    [code=php]
    <?php
    session_start() ;

    if( isset($_POST['submit'])) {
    if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSIO N['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'].'"';
    unset($_SESSION['security_code']);
    } else {
    // Insert your code for showing an error message here
    echo 'Sorry, you have provided an invalid security code';
    }
    } else {
    ?>

    <form action="form.ph p" method="post">
    <label for="name">Name : </label><input type="text" name="name" id="name" /><br />
    <label for="email">Ema il: </label><input type="text" name="email" id="email" /><br />
    <label for="message">M essage: </label><textarea rows="5" cols="30" name="message" id="message"></textarea><br />
    <img src="CaptchaSec urityImages.php width=100&heigh t=40&characters =5" /><br />
    <label for="security_c ode">Security Code: </label><input id="security_co de" name="security_ code" type="text" /><br />
    <input type="submit" name="submit" value="Submit" />
    </form>

    <?php
    }
    ?>[/code]

    ------------the second code----------------------
    [code=php]
    <?php
    session_start() ;

    /*
    * File: CaptchaSecurity Images.php
    * Author: Simon Jarvis
    * Copyright: 2006 Simon Jarvis
    * Date: 03/08/06
    * Updated: 07/02/07
    * Requirements: PHP 4/5 with GD and FreeType libraries
    * Link: http://www.white-hat-web-design.co....ph p-captcha.php
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details:
    * http://www.gnu.org/licenses/gpl.html
    *
    */

    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',$c haracters='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);

    ?>[/code]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Akkad.

    Is your script generating any errors? See this thread.

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      Your image src doesn't make sense. Did you forget to put a question mark at the start of your query string?

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Hi,

        I have recently deployed this code and I had to make s lightchange to it. The line that defines $_SESSION['secutiy_code'] works a lot better and more reliably if you move it from CaptchSecurityI mages() to generateCode(), immediatly before the return.

        This was the only way I could get the $_SESSION to validate later on.

        I just thought I would pass this on as a quick tip.

        As for the image not displaying it is also important that the CaptchaSecurity Images.php file is in the same directory as the php page that is using it.

        Finally, double check whether the GD libraries are installed correctly and are available. This caused me a bit of bother at first but now it works just fine.

        These are all just in case adding '?' into you img src doesn't work. That should definitley be there

        Cheers
        nathj
        Last edited by nathj; Jul 23 '07, 02:14 PM. Reason: Extar info

        Comment

        • Akkad
          New Member
          • Jun 2007
          • 18

          #5
          Hi everyboy , thanx for the replies:

          1. the quesiton mark is there when i am calling the CaptchaSecurity Images.php:
          < img src="CaptchaSec urityImages.php ?width=100&heig ht=40&character s=5" />
          2.i have made the changes related to the $_SESSION['security_code'] ;
          3. also i added the lines:
          error_reporting (E_ALL);
          ini_set('displa y_errors', True);
          but there is no error in the form page , or in the from processing page.
          4.issues related to GD library installation checking , if some one can just inform me about the installation of GD library on Fedora and what are the libraries needed for proper working of this library and how to enable it from php.ini , in detailed way plz

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            If you aren't getting any errors, GD is likely working fine.

            Comment

            Working...