1

My php file creates a png image. The image is ready and is saved into the given folder, but cannot load it on the page (it just show the place of the image, but empty).

This is the "captcha_image.php":

<?php
    $dirPath="/opt/lampp/htdocs/WebSiteFolder/dfxCaptcha/";
    $font='/opt/lampp/htdocs/WebSiteFolder/DejaVuSerif-Bold.ttf';
    $imgWidth=200;
    $imgHeight=50;        
    global $image;
    $image = imagecreatetruecolor($imgWidth, $imgHeight) or die("Cannot initialize a new GD image stream.");
    $background_color = imagecolorallocate($image, 0, 0, 0);
    $text_color = imagecolorallocate($image, 255, 255, 255);    
    imagefilledrectangle($image, 0, 0, $imgWidth, $imgHeight, $background_color);    
    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';    
    $len = strlen($letters);
    $letter = $letters[rand(0, $len - 1)];
    $word = "";    
    for ($i = 0; $i < 4; $i++) {
        $letter = $letters[rand(0, $len - 1)];         
        imagettftext($image, 15, 0, $i*50+25, 50, $text_color, $font, $letter);
        $word .= $letter;
    }
    $_SESSION['captcha_string'] = $word;
    $images = glob($dirPath."*.png");
    foreach ($images as $image_to_delete) {
        @unlink($image_to_delete);
    }
    header ("Content-type: image/png");
    imagepng($image, $dirPath."image" . time() . ".png");
?>

I use this in the HTML:

<img src="includes/captcha_image.php" id="captcha">

How can I display the image to the page?

1 Answer 1

2

Your captcha_image.php file doesn't output the file to the browser, because you gave two arguments to the imagepng function.

The documentation says:

to

The path or an open stream resource (which is automatically being closed after this function returns) to save the file to. If not set or NULL, the raw image stream will be outputted directly.

You probably want to specify no second argument – this will mean the image is not saved, however. You did not show us your logic for accepting CAPTCHAs, but it seems to involve session management, in which case you don't need to save the file.

This should be your last line:

imagepng($image);

Additionally, please consider using an existing CAPTCHA solution. What you are doing with your code produces clear text, which is extremely readable to OCR bots nowadays. Depending on the purpose of your project you might even get away with no CAPTCHA (e.g. for comments on blog articles you can specify hidden form fields, then use a spam filter). If a CAPTCHA is truly crucial, you will need a better solution than what you have.

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

6 Comments

So, do I need to run the php file first and after load the created image using the png file as a source?
@ZsG No, if you use src="includes/captcha_image.php" for the img tag, the PHP file will execute, create an image, then provide it directly to the browser, without needing to save it as a PNG file. You only need to make sure the PHP file actually renders as a PNG file, which my answer should address.
@ZsG Furthermore, if you are not storing your images, you do not need the $dirPath variable or the entire file deleting logic in your script.
To the addition: I use a more complicated solution to the image, it is just a simplified example.
@ZsG Good. I included it for the sake of completeness of my answer.
|

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.