dsbruqxgt820011351
dsbruqxgt820011351
2018-08-18 19:58

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 dtqjbbr5283 3年前

    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'
    
    点赞 评论 复制链接分享

相关推荐