سؤال

I am trying to decode an image blob from SQL database ( where it is stored after base64_encode) in PHP.

I am using

header('Pragma: public');
header('Cache-control: max-age=0');
header('Content-Type: image');
header("Content-Encoding: gzip");
header("Vary: Accept-Encoding");
echo gzencode( base64_decode ($result[0]['Icon']) ); // result is the result of query from DB

While it works some time when my browser language is English but it does not decode properly if my browser language is other than English.

I am debugging with charles proxy to look at the response. I am not able to post the response here. The size of the response for English is 3 bytes less than other responses.

But for other languages it comes as something like this with extra 3 bytes.

T�e�s�t�i�n�g� � 

I checked the DB for the encoded string for the image. It is the same in DB but different when the response is sent. Hence the image is displayed correctly if the language is English but broken if the language is not English. Please help me.

هل كانت مفيدة؟

المحلول

I got this fixed myself.The reason is because PHP is adding a UTF-8 byte order mark (BOM) at the beginning. So this problem gets fixed when the output buffer is cleaned using ob_clean(); The following worked perfectly.

header('Pragma: public');
header('Cache-control: max-age=0');
header('Content-Type: image');
header("Content-Encoding: gzip");
header("Vary: Accept-Encoding");
ob_clean();
echo gzencode( base64_decode ($result[0]['Icon']) ); // result is the result of query from DB

نصائح أخرى

Do you know name of the image or at least extension? You have to use it in your header Content-Type. For example header('Content-type: image/jpeg');

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top