1

I have a PHP site that generates images. Doesn't matter if I use GD or Imagick

header('Content-Type: image/jpeg'); 
imagejpeg($source);
$img = new Imagick($source);
$img->setImageFormat("jpeg");
header('Content-Type: image/'.$img->getImageFormat());
echo $img->getImageBlob();

The image gets correctly served in the browser. But when I try to right-click and download it, Chrome wants to save it as "jfif" file. I understand that jfif is the file format, but I would like Chrome to default save it as "jpg" file. How can I change my PHP code so that Chrome does that?

Note 1: Chrome does allow to default save the file as "jpg" file when it is served directly as jpg-file from my web space.

Note 2: Firefox by default allows to save all my images from the website as "jpg". This is the behaviour I like to have.

Se7enDays
  • 2,543
  • 2
  • 17
  • 22

1 Answers1

0

Follow the below steps and check whether it's working or not.

  1. Launch Registry Editor using Windows Search or Windows key + R pop-up window opens and type Regedit and press enter.
  2. Navigate to HKEY_CLASSES_ROOT > MIME > Database > Content Type > image/jpeg
  3. In the right panel, double-click on the Extension key. Its value should read as .jfif.
  4. Change the value to .jpg and click Okay.
  5. Close the Registry Editor.

The change should be instant and you should now be able to save JPG files from the web in .jpg format as it should be.

SwAn
  • 59
  • 3
  • Yes, this works but it is a client-side solution. I cannot ask all my website visitors to do so. I understand is not a big deal to save as jpg instead of jfif but just a minor experience thing I would like to enable on my website. My question is, what can I do server-side to create this behavior? What are the triggers that Chrome uses to determine whether to save as jfif or jpg? Clearly it is not just the MIME type as I have set those server-side. – Se7enDays Nov 04 '22 at 15:03
  • I have tried the below code, which generates images in the browser's window. $image = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/swan.png'); $image->setImageFormat("jpeg"); header('Content-Type: image/'.$image->getImageFormat()); echo $image->getImageBlob(); ************************************************************************ What exactly do you want? display or download? – SwAn Nov 05 '22 at 05:11
  • This is only a temporary fix because something -- I am not sure what but I can say for sure it is written by people who should be punished severely for the rest of their lives -- changes the registry value back to ".jfif" every week or so. – Leo Davidson Mar 11 '23 at 19:11
  • @Se7enDays nothing can be done server side, this isn't a server side issue. Your server is serving up the image data properly, reporting to the client what the content is with the header `Content-Type: image/jpeg` and chrome is looking up that content type locally in their computer's registry which is telling them that content type should have the extension `.jfif` – Andrew May 19 '23 at 20:24