dongshuao2309 2014-02-11 13:50
浏览 80

从数据库显示图像时,ANSI < - > UTF-8字符集问题(PHP)

I am loading images from a MySQL database, to display them in an Web GUI.

This is pretty standard and worked pretty well, till I tried to install the software in russia...

Here a example of the code that loads the image:

// Load overview image
if ($global_mode == 'overview') {

    // Load the image from the database.
    mysql_select_db("$db_x");   
    $sql = "SELECT $db_x.sensor_images.image
    FROM $db_x.sensor_images
    WHERE $db_x.sensor_images.image_id = '" . $global_image_id . "'";
    $sql = mysql_query($sql); 
    $row = mysql_fetch_assoc($sql);

    // Image output.
    header('Content-type: image/jpeg');
    echo $row['image'];
}

I installed the software on many european based laptops and I never had the problem, that images were not displayed...

Apparently on russian laptops (Windows 7, XAAMP, MySQL) this was not the case, images were not displayed.

I started to do some research and found out (on my laptop, where images get displayed...), that if I change the Encoding of the php file (in this case the show_image.php), I could replicate the error I had on russian laptops.

enter image description here

If the encoding is set to ANSI, the images get displayed...

Here I have disabled the header, so the browser displays the binary data (the encoding of the PHP file is set to ANSI)...

EXAMPLE A

enter image description here

Now I set the Encoding of the PHP file to UTF-8

enter image description here

By doing this images do not get displayed any more...

This is the output when I try to display the data without the header...

EXAMPLE B

enter image description here

As you can see, the output is different...

On my laptop (european):

ANSI: images get displayed, the data (without header) looks like EXAMPLE A

UTF-8: images get not displayed, the data (without header) looks like EXAMPLE B

On russian laptops:

ANSI: images get not displayed, the data (without header) looks like EXAMPLE B

UTF-8: images get not displayed, the data (without header) looks like EXAMPLE B

I still don't understand why changing the encoding of a php file has an impact on the output of binary data, respectively an image...

On the russian laptops the PHP files get always interpreted as if the encoding was set to UTF-8, no matter if I set it to ANSI or something else...

Please help!

Thx.

  • 写回答

3条回答 默认 最新

  • duanhuang1699 2014-02-11 14:15
    关注

    In your IDE you see "UTF-8" and "UTF-8 without BOM". You're choosing UTF-8, which in this case means with BOM. The BOM is prepended to the file and is the first thing that's output. This may a) break the output of your header, thereby breaking the data display, and b) giving the browser a clue that the following data is supposedly UTF-8 encoded, hence the browser is interpreting the data as UTF-8, which results in a lot of UNICODE REPLACEMENT CHARACTERS �. Check your error logs, you should see PHP complain about Headers already sent.

    The data you're sending is always the same, it's just interpreted in different encodings depending on the machine's default and the presence or absence of a UTF-8 BOM.

    The only reason it breaks at all under any circumstances is that you're outputting the wrong headers and/or are sending additional content before or after the image data. Check with a low-level tool like curl what exactly is output, and find and remove anything that doesn't belong.

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制