Editing JPEG META-data using EXIF corrupts images
Posted: Tue Jun 10, 2008 10:29 am
Hello world,
I've been working on a system that allows the end-user to manage his large amount of image files. He uses Cerious ThumbsPlus 5 to add META data to the images and then places them in a folder on his local webserver. Inside the admin pages he can run a script that reads the META-data from the images, places the values in a database and moves the images to another folder. This works fine.
The trouble starts when he tries to edit an image directly from the admin tool. I'm using the EXIF library to edit the META tags inside the images, but on his server this corrupts the images. After the first edit the colours are scrambled, after the second edit the entire file is corrupted.
The strange thing is that it works fine on my own pc. Both his and my localhosts are running on Windows XP, and are using the same server software (Apache 2.2.4, PHP 5, MySql Server 5). I even installed Thumbs+ 5 on my own to see if that conflicts with the EXIF lib, but they seem to be playing nicely together. Furthermore I've made sure that EXIF and MBstring are being loaded in the correct order on both servers and I'm quite sure I've tried every possible and impossible letter combination at the and of the fopen function.
Here is the code:
The iptc_maketag function:
If anyone could shed some light in this I'd be forever grateful 
I've been working on a system that allows the end-user to manage his large amount of image files. He uses Cerious ThumbsPlus 5 to add META data to the images and then places them in a folder on his local webserver. Inside the admin pages he can run a script that reads the META-data from the images, places the values in a database and moves the images to another folder. This works fine.
The trouble starts when he tries to edit an image directly from the admin tool. I'm using the EXIF library to edit the META tags inside the images, but on his server this corrupts the images. After the first edit the colours are scrambled, after the second edit the entire file is corrupted.
The strange thing is that it works fine on my own pc. Both his and my localhosts are running on Windows XP, and are using the same server software (Apache 2.2.4, PHP 5, MySql Server 5). I even installed Thumbs+ 5 on my own to see if that conflicts with the EXIF lib, but they seem to be playing nicely together. Furthermore I've made sure that EXIF and MBstring are being loaded in the correct order on both servers and I'm quite sure I've tried every possible and impossible letter combination at the and of the fopen function.
Here is the code:
Code: Select all
//define file
$file = "[path to file here]".$filename;
//fetch old iptc values
$size = getimagesize ($file, $info);
$iptc = iptcparse($info["APP13"]);
//edit them (the images come in 2 different flavours, hence the if statement)
if (substr($filename, 0, 2) == "00"){ //example image
$iptc["2#120"][0] = addslashes(strip_tags($keywords));
}
else{ //normal image
$iptc["2#025"][0] = addslashes(strip_tags($code));
$iptc["2#120"][0] = addslashes(strip_tags($name));
$iptc["2#118"][0] = addslashes(strip_tags($text));
}
//create the new IPTC string
//note that I didn't write this part myself, nor the iptc_maketag function (which is posted below) //
foreach (array_keys($iptc) as $s){
$iptc_tag = str_replace("2#", "", $s);
$c = count($iptc[$s]);
for ($i=0; $i < $c; $i++) $iptc_new .= iptc_maketag(2, $iptc_tag, $iptc[$s][$i]);
}
//--//
//embed iptc_string into file
$content = iptcembed($iptc_new, $file, 0);
//write the new content to the file
$fp = fopen($file, "wb");
fwrite($fp, $content);
fclose($fp);
//notify user of success
$message = "Tekening succesvol geupdate! - "
Code: Select all
function iptc_maketag($rec, $dat, $val){
$len = strlen($val);
if ($len < 0x8000) return chr(0x1c).chr($rec).chr($dat).chr($len >> 8).chr($len & 0xff).$val;
else return chr(0x1c).chr($rec).chr($dat).chr(0x80).chr(0x04).chr(($len >> 24) & 0xff).chr(($len >> 16) & 0xff).chr(($len >> 8 ) & 0xff).chr(($len ) & 0xff).$val;
}