tags:

views:

72

answers:

1

Hi, I use this function

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;

}

(from http://php.net/manual/de/function.iptcembed.php)

to write captions in my jpgs. Every Umlaut and other special chars are wrong when I read the jpgs with Picasa/Picasaweb.

Is this function not ready for unicode? How can I save utf-8 encode strings in jpgs?

Thanks for your help, Christian

A: 

Part of the issue is that strlen() is not multi-byte aware - it assumes the input is single-byte encoded. Consider using mb_strlen() instead.

Also, chr() is ASCII-only.

I'm not a binary guru, though, and I know nothing about IPTC data, so I'm not exactly sure what else this function is doing and where else there could be UTF-8 issues.

Peter Bailey
The statement "chr() is ASCII-only" doesn't make much sense. `chr` is encoding-agnostic, it just ["packs"](http://php.net/pack) a number between 0 and 0xFF.
Artefacto
What I'm saying is that it's not enocoding-aware for bytes above 0x7F. If you use `chr()` to do character comparisons, this can lead some unexpected results depending on the encoding of your source files.
Peter Bailey
ok, thx a lot guys.
christianhaller