使用iptcembed在JPG中写入IPTC数据时没有UTf-8

时间:2010-08-02 13:18:13

标签: php utf-8 iptc

我使用此功能

function iptc_make_tag($rec, $data, $value){
    $length = strlen($value);
    $retval = chr(0x1C) . chr($rec) . chr($data);

   if($length < 0x8000)
   {
      $retval .= chr($length >> 8) .  chr($length & 0xFF);
   }
      else
   {
       $retval .= chr(0x80) . 
               chr(0x04) . 
               chr(($length >> 24) & 0xFF) . 
               chr(($length >> 16) & 0xFF) . 
               chr(($length >> 8) & 0xFF) . 
               chr($length & 0xFF);
   }

    return $retval . $value;

}

(来自http://php.net/manual/de/function.iptcembed.php

在我的jpgs中写字幕。 当我用Picasa / Picasaweb阅读jpgs时,每个变音符号和其他特殊字符都是错误的。

这个功能还没有为unicode做好准备吗? 如何在jpgs中保存utf-8编码字符串?

感谢您的帮助, 基督教

2 个答案:

答案 0 :(得分:1)

我必须编写1:90 IPTC字段才能在Photoshop中正确显示我的UTF-8字符串。假设您在名为$ iptc的变量中组装IPTC数据,您应该从以下三行开始,以确保正确的UTF-8处理:

// These two lines ensure that UTF8-Encoding will work (set the 1:90 field in the envelop)
// @see http://cpanforum.com/threads/2114 for a hint
$utf8seq = chr(0x1b) . chr(0x25) . chr(0x47);
$length = strlen($utf8seq);
$iptc = chr(0x1C) . chr(1) . chr('090') . chr($length >> 8) . chr($length & 0xFF) . $utf8seq;

此后您可以继续添加IPTC字段,例如使用http://php.net/manual/en/function.iptcembed.php上的示例中描述的iptc_make_tag函数,最后将它们嵌入到JPEG中:

// Embed the IPTC data
$content = iptcembed($iptc, $path);

希望这能为其他人节省我今天必须做的研究和调试......

答案 1 :(得分:0)

部分问题是strlen()不是多字节感知的 - 它假设输入是单字节编码的。请考虑使用mb_strlen()代替。

此外,chr()仅限ASCII。

我不是二元大师,我对IPTC数据一无所知,所以我不确定这个函数还在做什么以及其他地方可能存在UTF-8问题。

相关问题