Extracting Exif Usercomment Problem

liunx

Guest
I'm a brand new user here, and I'm working on creating a basic PHP photo gallery. The gallery portion seems to be working well at this point (displaying a thumbnail and link for each photo inside a directory), but the page to display an individual photo is giving me quite the headache. What I wanted to do was extract a few EXIF properties from each JPG file to write onto the page... I had no trouble doing so using exif_read_data() to get the date and certain camera settings, but when I attempt to print the <b>UserComment</b> field, I get a jarbled mess in Firefox (but not IE).<br /><br />After some searching and testing (keep in mind I am completely new to PHP and web development in general so I may well be missing something here), I believe there is an issue with the character encoding of the string PHP is returning me. The EXIF UserComment field for each of my JPG files contains a comment string in Unicode, and evidently there is some issue when retrieving it through the following code:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><? $exif = exif_read_data($fileloc, 0, true);<br /><br />$computedvals = $exif['COMPUTED'];<br /><br />$caption = $computedvals['UserComment']; ?><br /><html><body><br /><? echo ($caption); ?><br /></body><html><!--c2--></div><!--ec2--><br /><br />Here is an example of the output I'm getting:<br /><br /><a href="http://www.skyinmotion.com/viewphoto.php?gallery=Landscape&filepath=http://www.skyinmotion.com/photos/landscape&filename=CRW_5054.jpg" target="_blank">http://www.skyinmotion.com/viewphoto.php?g...me=CRW_5054.jpg</a><br /><br />Note the last line of text before the "back" link at the bottom. For me, when using Firefox there is a jarbled mess of unreadable characters between the proper characters, and when using IE the spacing is incorrect -- either way, there is some type of character encoding issue. The actual contents of the UserComment field for this file are: "Lake Guntersville, Guntersville, AL"<br /><br />My guess is that I need to correct this by converting the string PHP is returning in my $caption variable in the code above to Unicode, but from what character set I am not sure.<br /><br />Any help would be appreciated!<!--content-->
Welcome to the forums brettjrob! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br /><!--quoteo(post=179542:date=May 18 2006, 06:59 PM:name=brettjrob)--><div class='quotetop'>QUOTE(brettjrob @ May 18 2006, 06:59 PM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=179542"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->Here is an example of the output I'm getting:<br /><br />Note the last line of text before the "back" link at the bottom. For me, when using Firefox there is a jarbled mess of unreadable characters between the proper characters, and when using IE the spacing is incorrect -- either way, there is some type of character encoding issue. The actual contents of the UserComment field for this file are: "Lake Guntersville, Guntersville, AL"<!--QuoteEnd--></div><!--QuoteEEnd--><br />It looks fine in my Firefox. See attached image.<!--content-->
<!--quoteo(post=179544:date=May 18 2006, 06:17 PM:name=TCH-Bruce)--><div class='quotetop'>QUOTE(TCH-Bruce @ May 18 2006, 06:17 PM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=179544"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->Welcome to the forums brettjrob! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br />It looks fine in my Firefox. See attached image.<!--QuoteEnd--></div><!--QuoteEEnd--><br />Thanks, I appreciate it!<br /><br />That is interesting how it looks different in your Firefox. I am utterly clueless about "character encoding" and "character sets" so I won't even try to speculate as to what settings on each of our systems or browsers may be the cause. I did notice, though, that in your screenshot there are spaces between each character which are not present in the actual EXIF data embedded in the JPG file. My real goal here is to obtain a string that contains <i>exactly</i> what is in the EXIF UserComment field.<br /><br />For what it's worth, I downloaded Apache and PHP 4.4.2 this afternoon and installed them on my WinXP machine and, with all the pertinent settings remaining on default, I did not encounter this issue when testing locally -- the string $caption from the code above was identical to what I had typed into the EXIF field, so perhaps there is some configuration on this server that I need to work around?<!--content-->
I have my default font set to Verdana and 16 for the size in my Firefox. That may be what's stretching it. I thought it looked kind of nice. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
I see the page the same as Bruce.<br /><br />Somehow I have a feeling that the user comment is encoded in something other than the "Universal" (UTF-8) or "Western" (windows/ISO) encoding... because I know English typed under Traditional Chinese (BIG-5) systems appears wider and larger than when typed with "normal" English systems.<!--content-->
The UserComment text appears to be in Unicode encoding, as identified by the $computedvals['UserCommentEncoding'] field.<br /><br />I don't believe you can convert the encoding, as that would appear to require the mbstring (Multibyte String) extension, which is not enabled by default on TCH servers.<br /><br />I was able to get the UserComment to display correctly in the browser by printing a regular character on the same line with the UserComment text:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?php echo (" " . $caption . " "); ?><!--c2--></div><!--ec2--><br />I printed a space on both sides to maintain centering, although only one is necessary to make the browser display the UserComment text correctly.<br /><br />The UserComment text is still encoded, which you can see if you do a "ViewSource" on the page, but printing the extra space characters seems to alert the browser to do a conversion on the fly.<!--content-->
Welcome to the forum, brettjrob. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Welcome to the forum, brettjrob <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Oops... where were my manners...<br /><br />Welcome to the forum, brettjrob!<!--content-->
 
Back
Top