NAME

Prima::Image::Exif - manipulate Exif records

DESCRIPTION

The module allows to parse and create Exif records. The records could be read from JPEG files, and stored in these using the extra appdata hash field.

SYNOPSIS

use Prima qw(Image::Exif);

# load image with extras
my $jpeg = Prima::Image->load($ARGV[0], loadExtras => 1);
die $@ unless $jpeg;

my ( $data, $error ) = Prima::Image::Exif->read_extras($jpeg,
	load_thumbnail => 1,
	tag_as_string  => 1
);

if ( $error eq 'XMP data' && defined $data ) {
	require XML::LibXML;
	my $xml = XML::LibXML->load_xml(string => $data);
	for my $node ($xml->findnodes('//*')) {
		my @p = $node->childNodes;
		next unless @p == 1;
		my $p = $node->nodePath;
		$p =~ s{\/\b[-\w]+\:}{.}g;
		$p =~ s{\.(xmpmeta|rdf)}{}ig;
		$p =~ s{^\.}{};
		print "$p: $p[0]\n";
	}
	($data, $error) = ({}, undef);
}

die "cannot read exif: $error\n" if defined $error;

for my $k ( sort keys %$data ) {
	my $v = $data->{$k};
	if ( $k eq 'thumbnail' ) {
		if ( ref($v)) {
			print "thumbnail ", $v->width, 'x', $v->height, "\n";
		} else {
			print "error loading thumbnail: $v\n";
		}
		next;
	}
	for my $dir ( @$v ) {
		my ( $tag, $name, @data ) = @$dir;
		print "$k.$tag $name @data\n";
	}
}

# create new image
$jpeg->size(300,300);

# create a thumbnail - not too big as jpeg appdata max length is 64k
my $thumbnail = $jpeg->dup;
delete $thumbnail->{extras};
$thumbnail->size(150,150);

# compile an exif chunk
my $ok;
($ok, $error) = Prima::Image::Exif->write_extras($jpeg,
	thumbnail => $thumbnail,
	gpsinfo   => $data->{gpsinfo},
);
die "cannot create exif data: $error\n" unless $ok;

$jpeg->save('new.jpg') or die $@;

API

parse $CLASS, $EXIF_STRING

Returns two scalars, a data reference and an error. If there is no data reference, the error is fatal, otherwise a warning (i.e. assumed some data were parsed, but most probabyl not all).

The data is a hash where there are the following keys may be set: image, photo, gpsinfo, thumbnail. These are individual categories containing the exif tags. Each hash value contains an array of tags, except thumbnail that contains a raw image data. Each tag is an array in the following format: [ tag, format, @data ] where the tag is a numeric tag value, the format is a type descriptor (such as int8 and ascii), and data is 1 or more scalars containing the data.

The module recognized some common tags that can be accessed via %Prima::Image::Exif::tags.

read_extras $CLASS, $IMAGE, %OPTIONS

Given a loaded Prima image, loads exif data from extras; returns two scalar, a data reference and an error.

Options supported:

load_thumbnail

If set, tries to load thumbnail as a Prima image. In this case, replaces the thumbnail raw data with the image loaded, or in case of an error, with an error string

tag_as_string

If set, replaces known tag numeric values with their string names

compile $CLASS, $DATA

Accepts DATA in the format described above, creates an exif string. Returns two scalars, am exif string and an error. If the string is not defined, the error is.

write_extras $CLASS, $IMAGE, %DATA

Checks if image codec is supported, creates Exif data and saves these in $IMAGE->{extras} . Return two scalars, a success flag and an error.

SEE ALSO

Prima::image-load

The module does not provide comprehensive support for tags and their values. For a more thorough dive see these modules: Image::ExifTool, Image::EXIF.

The module doesn't parse XMP records but warns if get supplied these. The XMP records are easily parsed by any XML parser, f ex XML::LibXML.

AUTHOR

Dmitry Karasik, <dmitry@karasik.eu.org>.