[Originally posted by argehl on 2010-01-02 12:02:34-08]
Is there any way to replicate the behavior of the command-line app when using "perl exiftool -g1 -j image.jpg" in the shell? I'm writing a cgi script for use as a JSON exif data retriever but can't find the settings (options?) to be set for a JSON output. I'd like:
$exifTool->Options('OPTION FOR JSON OUTPUT?');
$info = $exifTool->ImageInfo('image.jpg');
Assuming this would fill up $info with some ASCII text full of JSON info to be printed on the page directly with
print $info;
Is this possible? Thanks.
[Originally posted by exiftool on 2010-01-02 12:16:38-08]
The JSON output (in fact all text output except the verbose modes)
is handled by the exiftool application. However, you could borrow
the PrintJSON and EscapeJSON subroutines from the application.
This may save you some work if you can figure out how to use them.
- Phil
[Originally posted by argehl on 2010-01-02 12:28:24-08]
I see that PrintJSON() is being called under sub GetImageInfo($$); I don't think it'll be possible for me to make an customized call to PrintJSON and expect right results. There's no simple way to print JSON data other than the command line app? Cant the app be 'invoked' (sorry, new to Perl) from within the CGI script? Thanks.
[Originally posted by exiftool on 2010-01-02 12:49:34-08]Sure, if you want to do it that way you can invoke exiftool
from a CGI script like this:
my $result = `exiftool -json image.jpg`;
(Assuming of course that your CGI script has permission to
execute exiftool.)
- Phil
[Originally posted by argehl on 2010-01-02 16:57:21-08]Thanks. My host doesn't seem to support that (not shell_exec, either). Isn't there a possibility to write a wrapper to the command-line application so that it may be called from another script safely? It's essentially the same thing as using shell. Something like
$exiftool->execString("-a -g1 -j image.jpg");
so that people will have access to so many other features. (I really need that JSON output

)
[Originally posted by exiftool on 2010-01-02 18:53:44-08]OK, here you go. With the PrintJSON() and EscapeJSON()
subroutines from the exiftool application (cut and paste
as necessary), the following script
will give you JSON output:
#!/usr/bin/perl -w
use strict;
use Image::ExifTool ':Public';
sub PrintJSON($$$);
# define global variables used by PrintJSON()
my $joinLists;
my $listSep = ', ';
my $binaryOutput;
my $json = 1;
my %jsonChar = ( '"'=>'"', '\\'=>'\\', "\t"=>'t', "\n"=>'n', "\r"=>'r' );
# extract the information
my $file = shift or die "Must specify file name\n";
my $info = ImageInfo($file, { List => 1, Struct => 1 });
# print in JSON format
my $key;
print "{\n";
foreach $key (sort keys %$info) {
print ' "', GetTagName($key), '": ';
PrintJSON(\*STDOUT, $$info{$key}, ' ');
print "\n";
}
print "}\n";
exit 0;
I should charge for this stuff...

- Phil
[Originally posted by argehl on 2010-01-02 20:25:37-08]Wow, that works brilliantly! Are you a superhero the whole week or just on weekends!?

Though of course replicating the flexibility of the CL application is going to be much more complicated and not worth the trouble, I'm thinking (with grouping options etc.).
Thanks a lot!