2

I was hoping to get in touch with someone on a situation that I cannot find the solution to anywhere.

I am trying to create a captcha on my website using php and although I was able to create an image and create the random captcha text.

I am unable to over lay the two. Here is my code:

<?PHP

session_start();


function generateRandomString($length = 10) {
    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    $len = strlen($letters);
    $letter = $letters[rand(0, $len - 1)];
    $text_color = imagecolorallocate($image, 0, 0, 0);
    $word = "";
    for ($i = 0; $i < 6; $i++) {
        $letter = $letters[rand(0, $len - 1)];
        imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
        $word .= $letter;
    }
    $_SESSION['captcha_string'] = $word;

}


function security_image(){

   // $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
    //$font = 'content/fonts/comic.ttf';

    $width = '110';
    $height = '20';
    $font_size = $height * 0.75;
   // $image = @imagecreate($width, $height) or die('GD not installed');
               global $image;
    $image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");

               $background_color = imagecolorallocate($image, 255, 255, 255);  
               imagefilledrectangle($image,0,0,200,50,$background_color);


               $line_color = imagecolorallocate($image, 64,64,64); 
               for($i=0;$i<10;$i++) {
    imageline($image,0,rand()%50,200,rand()%50,$line_color);
               }

               $pixel_color = imagecolorallocate($image, 0,0,255);
               for($i=0;$i<1000;$i++) {
                              imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
               }

               //$textbox = imagettfbbox($font_size, 0, $font, $code);
               //$textbox = imagettfbbox($font_size, 0, $randomString);

    $x = ($width - $textbox[4]) / 2;
    $y = ($height - $textbox[5]) / 2;
   // imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
   imagettftext($image, $font_size, 0, $x, $y, $text_color , $word);


    $images = glob("*.png");
    foreach ($images as $image_to_delete) {
        @unlink($image_to_delete);
    }
    imagepng($image, "image" . $_SESSION['count'] . ".png");

    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);

}

security_image();




?>

I have no idea what I’m doing wrong. I’ve spent over 10 hours on this “display text” issue. I don’t understand and I am desperate for help. I even downloaded working captcha version from other resources that break once I upload it to my server. I have no idea whats going on. At first I thought there was something wrong with my server but the fact that I can even create the pixels, lines image means that it is at least working.

Please help!!!! 

UPDATE--------------------------------------------- Thank you for your suggestions. Here is the edited code. I'm still getting the same issue.

<?PHP

session_start();


function security_image(){
    global $image;

   // $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();

    $font = 'content/fonts/comic.ttf';

    $width = '110';
    $height = '20';
    $font_size = $height * 0.75;

    $image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");

    $background_color = imagecolorallocate($image, 255, 255, 255);  
    imagefilledrectangle($image,0,0,200,50,$background_color);


    $line_color = imagecolorallocate($image, 64,64,64); 
    for($i=0;$i<10;$i++) {
    imageline($image,0,rand()%50,200,rand()%50,$line_color);
    }

    $pixel_color = imagecolorallocate($image, 0,0,255);
    for($i=0;$i<1000;$i++) {
        imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
    }

    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    $len = strlen($letters);
    $letter = $letters[rand(0, $len - 1)];
    $text_color = imagecolorallocate($image, 0, 0, 0);
    $word = "";
    for ($i = 0; $i < 6; $i++) {
        $letter = $letters[rand(0, $len - 1)];
        imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
        $word .= $letter;
    }
    $_SESSION['captcha_string'] = $word;

    /*texbox unitinitialized (removed for the sake of just showing the word size doesnt matter)
        $x = ($width - $textbox[4]) / 2;
        $y = ($height - $textbox[5]) / 2;
    */

    $x = ($width) / 2;
    $y = ($height) / 2;
    imagettftext($image,$font, 4, $x, $y, $word);



    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);

}
security_image();?>

i made some suggested changes but the code seems to still do the same thing. Just display the lines and pixels as expected but the text still is missing... ?

3
  • please excuse the commented code. those were my other attempts that failed but i kept it in there to show what i have tried already. Commented Nov 20, 2014 at 14:32
  • Your imagestring should be in your security_image function. This is the function that will add the text into your image :-). Do something like : ImageString($image, 4, $x, $y, $_SESSION['word']); Commented Nov 20, 2014 at 14:44
  • thanks i added that and its still giving me the same error. for some reason the text just doesnt want to show...? :I Commented Nov 20, 2014 at 21:43

2 Answers 2

1

There are some several "errors" in your functions, let's fix them:

In generateRandomString()

  • generateRandomString($length = 10)

$lenght is not used its scope.

  • $text_color = imagecolorallocate($image, 0, 0, 0);

$image is uninitialized


In security_image() function:

  • $textbox is uninitialized

  • $text_color and $word is uninitialized.

  • And Wrong parameter count for imagettftext() You add 7 parameters, and forget the font file parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a bunch. ok i reorganized the code to i got rid of $length, since i wasnt using it. i moved $text_color below where $image is initialized. Actually i moved the whole text generation function in one function. I removed $textbox because i wasnt using it. And i added the $font parameter in imagettftext, i didnt initialize any font file paramiter? am i supposed to? i uncommented my $font parameter. I'm still getting the same result as before. i can see lines and pixels but no text... im wondering if the text is actually there, and its behind the image...
0

found the problem. using this: http://php.net/manual/en/function.imagettftext.php

i was able to see that the font location was incorrect. using the example on that page and editing it to my needs worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.