Thursday 5 March 2009

running exiftool/perl scripts from php

This morning I was trying to get my perl script to run in php. Googling for this I found a number of methods:
  1. Perl Extension for PHP
  2. Use the php function proc_open
  3. Use the php function exec
  4. Use the normal command to run the script from the command line as the value of a php variable, but surround it with backticks instead of quotes


I tried #3 and #4 out of those, of which #4 worked.

Next I tried to make it so I could pass the image filename from php to the perl script. I found out how to do that here: Passing parameters to perl script. You just call your script from php like $var = `/usr/bin/perl -T /home/brighto/public_html/photosite/cgi/test2.cgi '$file' 'EXIF'`; Then in the perl script you access your variables through @ARGV[0] for $file and @ARGV[1] for 'EXIF'.

I then modified my perl script so that it would output text in the format for a php array, then in the php file I eval'd the variable that called the perl script.

However, the eval wasn't working as there were some ' in the exif values, so this was messing up the array. I searched for how to escape quotes in perl, I thought there must be a function you could use but I couldn't find any other than in some modules you can add.

So I decided I might as well just use a basic string replacement. I found a couple of helpful tutorials on this: Search & Replace Strings in Perl and Substitution and translation in Perl. In both examples it seems you can only do the replacement when setting a variable value, unlike php where you can just print the str_replace() function.

After that I looked a bit at running perl through fastcgi to speed it up a bit. However it seems I would to re-build perl. Since my web host may well not have a fastcgi version of perl installed I don't think it's worth the trouble of trying it.

After lunch I went on Animal, then checked my spleenmail. Then I changed my php script to call exiftool directly rather than through my perl script. I got it working okay and also managed to get it working using the exec function - I needed to specify the location to perl (so /usr/bin/perl instead of just perl).

The only problem was that the output wasn't in a format easily convertible to a php array. I googled for 'exiftool php' and found this thread about using the exiftool library from php (in place of native php function). There the request is to get exiftool to output xml.

I played about with trying get PHP to parse the XML that exiftool output, then read that ExifTool can output JSON, so I tried that instead.

After dinner I watched Lost s05e08 with Mac and Ben, ultimate as usual, but I hope they have more about the time when there was a giant statue on the island in the future.

Then after that I did a bit more work on php script that calls exiftool directly. Using the -j parameter to get ExifTool to output the tags in JSON format it looked good, but there were some fields missing, like the GPS Longitude. So I got the two scripts working on the webserver, and then posted to the ExifTool forum so hopefully someone can help me there.

After that I checked my email, the canon lens forum on dpreview and the websqueeze.

The weather was cloudy but sunny again most of the day. It did sleet for a few minutes, but then soon stopped and became sunny again. There was a nice sunset today.

Food
Breakfast: Lemon marmalade toast sandwich; cup o' tea.
Lunch: Mature cheddar cheese with french style salad sandwich; clementine; mince pie; caramel Wacko (Michael Jackson endorsed fake Caramel Rocky); cup o' tea.
Dinner: chicken & mushroom in a white wine sauce pie; green beans; potato; swede. Coffee.
Supper: American style chocolate brownie cookie; milk chocolate digestive; cup o' tea.

3 comments:

Unknown said...

Hi,

I am desperatley trying to rad out some special EXIF Data via php. I found your experiments with ExifTool. Can you splease, please end me what you came up with (easy an simple for a not even hobby programmer).

You would help me a lot.
Thanks
Jürgen

Rusty said...

Hi Jürgen

Seems like Blogger won't let me use any html formatting in comments, so I posted my reply to you as a new blog post: using exiftool to extract metadata into a PHP array

slarti said...

The good old LOST days
Also thank you for all those links :)