篇首语:本文由编程笔记#小编为大家整理,主要介绍了php exif_read_data()不在utf-8中相关的知识,希望对你有一定的参考价值。
所以我在glob()
中使用foreach
函数从我的网站上的文件夹渲染图像,我想在那里有标题,所以我把标题放在该图像的元数据中,但是当我通过exif_read_data()执行这些元数据时,它将执行不是在utf-8,它显示POL`T?XE而不是这个POLŠTÁŘE。这是我的代码,尝试解决这个问题
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 '
';
}
}
即使我使用的是exiftools,它也没有任何效果。甚至没有utf8_encode()
甚至mb_convert_encoding($meta_data['IFD0']['Title'], 'UTF-8')
。当我使用这个mb_detect_encoding()
函数时,它执行了UTF-8。
我尝试用exiftool将编码设置为utf-8,但它也没有用。你能帮助我吗?
答案
经过几个小时寻找答案而没有找到任何东西(对我来说没什么用)我就这样想出来:我将整个普通字母和特殊字符字母表放到那张图片的元数据上,而不是用$meta_data['IFD0']['Title']
把它输出到bin2hex()
函数和制作和array
为每个字母和特殊字母,而不是与str_replace
替换正常字符的代码。
$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','-','!','+');
然后
str_replace($hex, $letters, bin2hex($meta_data['IFD0']['Title']))
输出之前:aY45Y5~1a534Y68Y
输出之后:šěř45čř5ž1š53čě4ř6ě8ř
(与元数据相同)
注意:即使在$hex
示例中,每个字符必须与$letters
中的位置相同:
$letters = array('a', 'b', 'c', 'd');
$hex = array('6100','6200','6300','6400'); //'a', 'b', 'c', 'd'