Sam
Sam

Reputation: 53

PHP GD image not displaying in Chrome

We have used Captcha.php on one of our project, it opens in all browsers but we are not able to view in Google chrome Version 22.

Our Captcha script

session_start();
$captcha = new SimpleCaptcha();
$captcha->CreateImage();

class SimpleCaptcha 
{
    function CreateImage()
    {
        header("Content-Type: image/jpeg");

        $md5              = md5(rand(0,9999));
        $pass             = substr($md5, 10, 5);
        $_SESSION["pass"] = $pass;

        $image     = ImageCreatetruecolor(100, 20);
        $clr_white = ImageColorAllocate($image, 0, 0, 0);
        $clr_black = ImageColorAllocate($image, 255, 255, 255);

        imagefill($image, 0, 0, $clr_white);
        imagefontheight(15);
        imagefontwidth(15);
        imagestring($image, 5, 30, 3, $pass, $clr_black);

        return imagejpeg($image);
        imagedestroy($image);
    }
}

HTML Implementation

<img src="code/captcha.php" width="100" height="20" alt="Captcha Code"/>

We are not able to view it on Google Chrome. All the browser return the same image.

Upvotes: 4

Views: 3220

Answers (5)

rsc
rsc

Reputation: 10679

Adding a

ob_clean();

solved for me.

Full example:

$img = imagecreatefromjpeg('my_image.jpg');
ob_clean();
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);

Upvotes: 3

Dave
Dave

Reputation: 1765

There is a known issue that materializes between Chrome & some versions of Kaspersky.

Kaspersky Anti-Virus doesn't cause it, but Kaspersky Endpoint Security does.

For some reason, during the interaction of the two applications when retrieving dynamically created images from the web, the Content Length in the header becomes out by 1 byte.

If you modify the Content Length value in the header, it will work in Chrome, but fail in Firefox, IE & Opera who all follow the RFCs correctly.

I'm not sure which application is actually at fault, but it isn't something you can easily fix yourself.

Upvotes: 0

uınbɐɥs
uınbɐɥs

Reputation: 7351

It may be to do with caching, I've used this a script of mine:

header('Cache-Control: no-cache, must-revalidate');

Just that line should be enough.

Upvotes: 0

Emissary
Emissary

Reputation: 10148

You say Chrome keeps rendering the same image? try sending headers to tell the browser not to cache anything.

header('Pragma: no-cache');
header('cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1999 00:00:00 GMT'); 

Upvotes: 0

Roni
Roni

Reputation: 70

I have faced same problem. Just turned off my kaspersky. It is working fine now. You can also try this.

Upvotes: 0

Related Questions