PHP GD image not showing up in Chrome

We used Captcha.php in one of our projects, it opens in all browsers, but we cannot view chrome Version 22 in Google.

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 embedding

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

We cannot view it in Google Chrome. All browsers return the same image.

+4
source share
5 answers

I faced the same problem. Just turned off my Kaspersky. Now it works fine. You can also try this.

0
source

Are you saying that Chrome keeps 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'); 
0
source

Perhaps this is due to caching, I used this script of mine:

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

Only this line should be sufficient.

0
source

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

Kaspersky Anti-Virus does not cause this, but Kaspersky Endpoint Security does.

For some reason, during the interaction of the two applications when retrieving dynamically created images from the Internet, the length of the content in the header becomes 1 byte.

If you change the Content Length value in the header, it will work in Chrome, but not in Firefox, IE, and Opera, which all comply with RFC correctly.

I'm not sure which application is really to blame, but this is not something you can easily fix yourself.

0
source

Adding

 ob_clean(); 

decided for me.

Full example:

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

Source: https://habr.com/ru/post/1442448/


All Articles