dsbruqxgt820011351 2018-08-18 19:58
浏览 98

php exif_read_data()不在utf-8中

So I'm using glob() function in foreach to render images from folder on my site and I want to have titles in there, so I put title in meta data of that image, but when I execute those meta data via exif_read_data() it will execute not in utf-8, it shows POL`T?XE this instead of this POLŠTÁŘE. This is my code with some tries of solving this problem

header("Content-Type: text/html; charset=utf-8");
ini_set('exif.encode_unicode', 'UTF-8');
iconv_set_encoding('input_encoding', 'UTF-8');
iconv_set_encoding('output_encoding', 'UTF-8');
iconv_set_encoding('internal_encoding', 'UTF-8');

$dirs = array_filter(glob('img/*'), 'is_dir');
foreach ($dirs as $dir) {
  $files = glob($dir.'/*.{jpg,png,JPG,PNG,jpeg,JPEG}', GLOB_BRACE);
  foreach($files as $file) {
    $meta_data = exif_read_data($file, 0, true);
    echo '
    <img src="'.$file.'" title="'.$meta_data['IFD0']['Title'].'" />

Nothing of it worked, even when I used exiftools. Not even with utf8_encode() and not even mb_convert_encoding($meta_data['IFD0']['Title'], 'UTF-8'). When I used this mb_detect_encoding() function it executed UTF-8.

And I tried to set encoding to utf-8 with exiftool and it didn't work either. Can you help me?

  • 写回答

1条回答 默认 最新

  • dtqjbbr5283 2018-08-18 23:42

    After literally hours of looking for answer and not finding anything (nothing worked for me) I figured out it like that: I put whole normal alphabet and special characters alphabet to metadata of that picture and than I outputed it with $meta_data['IFD0']['Title'] to bin2hex() function and made and array for each letter and special letter and than with str_replace replaced code with normal characters.

    $hex = array('0000','6100','6200','6300','6400','6500','6600','6700','6800','6900','6a00','6b00','6c00','6d00','6e00','6f00','7000','7100','7200','7300','7400','7500','7600','7700','7800','7900','7a00','4100','4200','4300','4400','4500','4600','4700','4800','4900','4a00','4b00','4c00','4d00','4e00','4f00','5000','5100','5200','5300','5400','5500','5600','5700','5800','5900','5a00','e100','0d01','0f01','e900','1b01','ed00','4801','f300','5901','6101','6501','fa00','6f01','fd00','7e01','c100','0c01','0e01','c900','1a01','cd00','4701','d300','5801','6001','6401','da00','6e01','dd00','7d01','2c00', '2000','3100','3200','3300','3400','3500','3600','3700','3800','3900','3000','2d00','2100','2b00');
    $letters = array('', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','á','č','ď','é','ě','í','ň','ó','ř','š','ť','ú','ů','ý','ž','Á','Č','Ď','É','Ě','Í','Ň','Ó','Ř','Š','Ť','Ú','Ů','Ý','Ž', ',', ' ','1','2','3','4','5','6','7','8','9','0','-','!','+');

    And than

    str_replace($hex, $letters, bin2hex($meta_data['IFD0']['Title']))

    Output before: aY45Y5~1a534Y68Y

    Output after: šěř45čř5ž1š53čě4ř6ě8ř (which is same as metadata)

    NOTE: Every character has to be on the same position as in $hex even in $letters example:

    $letters = array('a', 'b', 'c', 'd');
    $hex = array('6100','6200','6300','6400'); //'a', 'b', 'c', 'd'
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 关于#java#的问题,请各位专家解答!(相关搜索:java程序)
    • ¥15 linux tsi721的驱动编译后 insmod 提示 报错
    • ¥20 multisim测数据
    • ¥15 求无向连通网的所有不同构的最小生成树
    • ¥15 模拟器的framebuffer问题
    • ¥15 opencv检测轮廓问题
    • ¥15 单点式登录SSO怎么爬虫获取动态SSO_AUTH_ACCESS_Token
    • ¥30 哈夫曼编码译码器打印树形项目
    • ¥20 求完整顺利登陆QQ邮箱的python代码
    • ¥15 怎么下载MySQL,怎么卸干净原来的MySQL