1

I am trying to create a captcha that takes words from a file I've named dictionary.txt and uses GD Library to turn them into captcha images. But something is wrong. I can get my dictionary phrases to show up as session variables but they won't make an image. So it must be the GD Library code. There is a final target file named captcha_validate.php but that's not important right now. I am just trying to get the image to work.

form_captcha.php

<form class="" action="captcha_validate.php" method="post">
    Captcha Verify:
    <input type="text" name="captcha" value="" /><br /><br />
    <img src="captcha.php" /><br />
    <input type="submit" name="submit" value="Submit" />
</form>

captcha.php

    <?php

    session_start();

    session_regenerate_id();

    $file = file('dictionary.txt', FILE_IGNORE_NEW_LINES);

    $length = count($file) - 1;
    $random = rand(0, $length);

    $_SESSION['captcha'] = $file[$random];
    session_write_close();

    function msg($msg) {
        $container = imagecreate(250, 170);
        $black = imagecolorallocate($container, 0, 0, 0);
        $white = imagecolorallocate($container, 255, 255, 255);
        $font = "C:\Windows\Fonts\arial.ttf";
        imagefilledrectangle($container, 0, 0, 250, 170, $black);

        $px = (imagesx($container) / (strlen($msg)/1.15));
        $py = (imagesy($container) / 3.5);

        imagefttext($container, 28, -27, $px, $py, $white, $font, $msg);
        header("Content-type: image/png");
        imagepng($container, null);
        imagedestroy($container);
    }

    msg($file[$random]);

     ?>
2
  • Imagepng man page says NULL is invalid if the quality and filters arguments are not used.
    – u_mulder
    Jan 13, 2017 at 6:55
  • I did this and it's still not working. imagepng($container, null, 0, PNG_NO_FILTER);
    – Matt Lee
    Jan 13, 2017 at 7:10

2 Answers 2

0

Try to test if the font path is set right. I did test yout script and it worked when I set the font path right for my environment.

like:

$font = './SomeFont.ttf';

1
  • Okay so it turns out I was using html in my captcha file and I wasn't supposed to use it in that case.
    – Matt Lee
    Jan 13, 2017 at 19:32
0

Okay so it turns out I was using html in the captcha.php file and I shouldn't have used it in that case. That was the main reason why my code wasn't working. Thanks for trying, anyone who commented or answered.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.