skip to Main Content

Whenever I try to serve an image with PHP, it will both state the image is broken/corrupt, and give the following warning in Google Chrome:

Resource interpreted as Document but transferred with MIME type
image/png

However, the situation is as follows;

  1. This script runs fine on multiple servers, just not on this one.
  2. I have used multiple images and extensions
  3. The images are working perfectly fine if I put them in an imgsrc as a base64 decoded image
  4. For testing purposes, I have put it on the first line in my index.php, didn’t work
  5. My files aren’t UTF-8 BOM
  6. I personally can’t see any surprise-headers

Examples of what I’ve tried:

$imgpath = 'assets/img/dropdown-arrow.png';
$type      = pathinfo($imgpath, PATHINFO_EXTENSION);
$data      = file_get_contents($imgpath);
$base64    = 'data:image/' . $type . ';base64,' . base64_encode($data);
header('Content-Type: image/png;'); // also tried with charset=UTF-8 and such
echo base64_decode($base64);
exit();

Example 2 (the working on other servers example):

// Set the content type header - in this case image/png
header('Content-Type: image/png; charset=UTF-8');
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($img, 0, 0, 0);

// removing the black from the placeholder
imagecolortransparent($img, $background);

// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($img, false);

// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($img, true);

// Output the image
imagepng($img);

— So, again; I have already cut my framework out as the middle man, trying to run this straight from the index.php’s first line.

so, for some weird reason, the moment I set the content-type to image/png;; everything goes downhill.

Would anybody have any clue as to why this is happening? Is this something I missed with my code? Is it something I can’t possibly fix with my code (server-side)? Did I just miss something extremely obvious?


Basic Serverinfo:

I’m running on Apache 2.4.5 with PHP-FPM. (Though switching to PHP-FASTCGI changed nothing) on PHP 7.2.3


Response Headers:

Connection: Keep-Alive

Content-Type: image/png

Date: Fri, 13 Jul 2018 19:51:37 GMT

Keep-Alive: timeout=5, max=99

Server: Apache/2.4.25 (Debian)

Transfer-Encoding: chunked


UPDATE/FIX

Apparently, someone (Not me, really!) put a space before the <?php tag in one small file which was being included across the MVC. Removing that space fixed every issue. It took quite a few hours to find, so, the lesson for anyone reading: Make sure you keep your code neatly formatted and always put your <?php tags at the very start.

2

Answers


  1. You can output it directly, no need to do a base64_encode/decode

    $imgpath = 'assets/img/dropdown-arrow.png';
    $data    = file_get_contents($imgpath);
    header('Content-Type: image/png;');
    echo $data;
    exit();
    
    Login or Signup to reply.
  2. in example 2, your header says that your data is utf8 encoded.
    Why?
    your data is a binary file
    and no utf8 encoded

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search