0

I use this code to retrieve and display an image:

header("Content-type: image/png"); 
echo file_get_contents(site_domain().image_asset_module_url("header.png",$this->name));

on my local WAMP it works, but on the remote server file_get_contents returns a wrong-encoded string:

Local:

‰PNG  IHDR^jRÀ2¡    pHYsÒÝ~üÿÿIDATxÚ콘Uõµþ¿`ŠŠÔéÃÕ¨¹&&ù'77¹i¦˜è‰=V:RlH‡™aAlH™B¯Jbh...

Remote:

�PNG  IHDR^jR�2�    pHYs��~���IDATx����U����`������ը�&&�'77�i��草=V:Rl...

If I use utf8_encode I get:

PNG  IHDR^jRÀ2¡ pHYsÒÝ~üÿÿIDATxÚì½Uõµþ¿`ÔéÃÕ¨¹&&ù'77¹i¦è=V:RlHaAlHB¯Jbh...

So I always get a break picture on my remote Server - why and what is the solution?

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
Kilhog
  • 3
  • 3

2 Answers2

1

The data is always the same. file_get_contents does not alter data in any way. You're also not dealing with text in some encoding, but with binary data. Any sort of text-encoding or conversion thereof does not apply here.

Your first sample is the binary image data as interpreted as Latin-1 encoded text.
Your second sample is the same binary data as interpreted as UTF-8 encoded text.

I.e., the data is fine, the interpretation is wrong. The interpretation should be set by the Content-Type header, perhaps this is not being set correctly on the remote server. For this problem, inspect the raw HTTP response headers and see How to fix "Headers already sent" error in PHP.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • The header looks good `HTTP/1.1 200 OK Date: Thu, 30 May 2013 09:01:01 GMT Server: Apache Set-Cookie: [506 bytes were stripped] X-Powered-By: PleskLin Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: image/png` – Kilhog May 30 '13 at 09:03
  • And yet you get the output as jumbled text? Compare the HTTP body you receive (which should be the image data) with the raw image data in a hex editor! See if there's any difference, especially any leading bytes at the beginning that shouldn't be there. – deceze May 30 '13 at 09:11
  • In my http body after `Type: image/png` I have `0d 0a 0d 0a 31 65 37 34 65 0d 0a ef bb bf` and after the hex code is the same code from the image png – Kilhog May 30 '13 at 09:24
  • I'm not quite clear on what you said; does the file data look like this: http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header ? Or is there extra stuff? – deceze May 30 '13 at 09:32
  • 1
    I found the problem when I put my controllers in utf8 BOM I have a space that ride in my website, in ANSI the space is invisble. Now I have more than to find or is this space, thank you for all ! – Kilhog May 30 '13 at 09:54
-1

I would have rather used

<?php

$file = 'http://url/to_image.png';
$data = file_get_contents($file);

header('Content-type: image/png');
echo $data;

Or Can you try this

$remoteImage = "http://www.example.com/gifs/logo.gif";
$imginfo = getimagesize($remoteImage);
header("Content-type: $imginfo['mime']");
readfile($remoteImage);
Yogus
  • 2,307
  • 5
  • 20
  • 38
  • @deceze Its simple! No use of `site_domain().image_asset_module_url` – Yogus May 30 '13 at 08:33
  • Thank you for your answer but it does not work I still get the same string – Kilhog May 30 '13 at 08:35
  • If I did not put `site_domain().image_asset_module_url("header.png", $ this->name)` , And I put the URL directly into hard, anyway I error – Kilhog May 30 '13 at 09:11