Wednesday 6 January 2010

using exiftool to extract metadata into a PHP array

Hi Jürgen

The relevant bit of my code is below:
$start = microtime(true);
//below string is: 'path to perl' '-switch to print warnings about dubious constructs' 'path to exiftool perl script' '-all Exif tags' '-all IPTC tags' '-group results' '-return results in JSON format' '-format GPS co-ordinates as a string' '-escape HTML chars' '-small speed boost' 'path to image'
//$metadata = `/usr/bin/perl -w HOME_DIR/perlModules/ExifTool/exiftool -EXIF:all -XMP:all -File:all -g -j -c "%s" -E -a -fast '$file'`;
$metadata = HOME_DIR.'/perlModules/ExifTool/exiftool -EXIF:MeteringMode -EXIF:Flash -EXIF:ExposureMode -EXIF:WhiteBalance -EXIF:LightSource -EXIF:Model -EXIF:Make -EXIF:ModifyDate -EXIF:ExposureTime -EXIF:FNumber -EXIF:ExposureProgram -EXIF:ISO -EXIF:DateTimeOriginal -EXIF:CreateDate -EXIF:ExposureCompensation -EXIF:FocalLength -EXIF:SubSecTime -EXIF:SubSecTimeOriginal -EXIF:SubSecTimeDigitized -EXIF:FocalLengthIn35mmFormat -XMP-dc:title -XMP-iptcExt:PersonInImage -XMP-iptcExt:Event -XMP-xmp:Rating -EXIF:GPSLatitudeRef -EXIF:GPSLatitude -EXIF:GPSLongitudeRef -EXIF:GPSLongitude -EXIF:GPSAltitudeRef -EXIF:GPSAltitude -XMP-iptcExt:LocationCreated -XMP-iptcExt:LocationShown -XMP-photoshop:Headline -XMP-dc:description -XMP-dc:subject -XMP-dc:rights -XMP-xmpRights:Marked -XMP-microsoft:LensModel -XMP-microsoft:LensManufacturer -XMP-dk:all -XMP-lr:HierarchicalSubject -XMP-photoshop:ColorMode -XMP-photoshop:SupplementalCategories -File:ImageWidth -File:ImageHeight -File:FileType -File:MIMEType -ICC_Profile:ProfileDescription -EXIF:ColorSpace -EXIF:InteropIndex -g -j -struct -c "%s" -fast '."'$file'";
echo "\n\$metadata='".$metadata."'\n";
$metadata = `$metadata`;
//Decode the json
$metadata = json_decode($metadata, true);
$exif = &$metadata[0]['EXIF'];
$xmp = &$metadata[0]['XMP'];
$file = &$metadata[0]['File'];
$icc = &$metadata[0]['ICC_Profile'];
$end = microtime(true);
echo 'Time taken: '.($end - $start);
echo "<pre>Metadata:\n";
print_r($metadata);
echo '</pre>';


Then after that I select each property and store it in the database (which is a lot of code, and not much point me posting since your database structure wouldn't be the same as mine), but here's an example:
//Image last modified
if( isset($exif['ModifyDate']) )
{
$sql .= ", ModifyDate = '".$conn->real_escape_string($exif['ModifyDate'])."'\n";
}


Hope that helps

Rusty

5 comments:

Unknown said...

Thanks for the answer and code. I cannot get it running.

installing and playing around with exiftool I am able to a)
In a browser navigate to:
mypage/cgi-bin/ExifTool/exiftool/t/images/ExifTool.jpg

and I get as a result:
"Consult the exiftool documentation for a full list of options."

Which to me means, I successfully launched exiftool.

b) when running your php script I get a result like:
Time taken: 0.041465997695923
Metadata:
Array
(
[0] => Array
(
[EXIF] =>
[XMP] =>
[File] =>
[ICC_Profile] =>
)

)

which to me seems as if no data has been read from my image file.

At this point I

Rusty said...

Your comment seems to have been cut off, but start by trying this in php:
$result = `/path/to/exiftool -ver`;
echo $result;

Note the command is enclosed by backticks ` not '.

If you get the version number of exiftool then at least you know you can execute exiftool from your php script okay.

Let me know if that works okay.

Dave

Unknown said...

hi,

you're great trying to help an idiot like me. I hope the communication via mail works and we do not have to use the blog. So I am trying here, I hope you get the mail alreight.

Nope, your php code
$result = `/path/to/exiftool -ver`;
echo $result;
with my path to exiftool returned empty

I am sure I can run perl on my server since this file "helloworld.pl" when calling in browser
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello World";

returns
Hello World
(but without the second line it returns an error 500)

so, perl works.

On my local system I have no problem via command line to
exiftool t/images/test.jpg

and get all the exif data.


Where am I wrong?

-- (Please can we try vie eMail?)

Rusty said...

I don't know if I got your email or not, I got one from blogger that I replied to, but the reply address was no-reply@blogger.com, so I'm guessing you won't have got it.

If you want to email me, my email is the following shower heads uk at hotmail dot com

Dave

Unknown said...

I didn't succeed composing your mail address. Anways, here is mine

soloATonlinehome.de

As for perl/php: no success yet... :-(