0

I create an image with PHP and output it in the following way

$im = imagecreatetruecolor($w,$h);
...
header('Content-type: image/png');
header('Cache-Control: no-store, no-cache');
imagepng($im);
imagedestroy($im);

If I right click on the image and select 'Save image as...' in Chrome browser I get a "Failed - Network error". It does not save the image to a file. If I do the same with FireFox, Safari, or IE it works fine. I have tried to add the content disposition header like so:

header('Content-disposition: inline; filename=image.png');

This did not fix it. Chrome also shows the filename to be saved as a .php file, the name of the generating PHP file, not a .png extension as FireFox for example does. Anybody knows what I am doing wrong?

Chris
  • 41
  • 8
  • Try sending the headers before doing anything else. Maybe there's a whitespace in the output? – Markus Zeller Nov 26 '22 at 23:54
  • I have tried putting headers at the very beginning. It didn't make any difference. The PNG image is perfectly fine displayed by the Chrome browser. Thus it knows it is a PNG image. Why it cannot download it as a file is a mystery. Looks like a bug to me. – Chris Nov 28 '22 at 16:17
  • What exactly do you want to achive? Calling a PHP script showing the image or providing a download? If you want to provide a download then you probably need to do a redirect and then send the Content-Disposition header. – Markus Zeller Nov 28 '22 at 16:29
  • I want that the image is displayed AND that right-clicking on it allows one to save the image file. The displaying part works fine. The saving part does not work on Google Chrome browser. I want to make it also work on Google Chrome. – Chris Nov 29 '22 at 23:36

0 Answers0