WWW-MeGa
view release on metacpan or search on metacpan
lib/WWW/MeGa/Item.pm view on Meta::CPAN
=head2 data
returns necessary data for rendering the template
=cut
sub data
{
my $self = shift;
my $data =
{
FILE => $self->{file},
PATH => $self->{path},
PATH_REL => $self->{path_rel},
NAME => $self->{file},
};
$data->{EXIF} = $self->exif;
$data->{TYPE} = (split(/::/, Scalar::Util::blessed($self)))[-1];
return $data;
}
=head2 exif
read, return and cache the exif data for the represented file
=cut
sub exif
{
my $self = shift;
return unless $self->{config}->param('exif');
#$self->{cache}->{exif}->{23} = "foo";
return $self->{cache}->{exif}->{$self->{path}} if ($self->{cache}->{exif}->{$self->{path}});
use Image::ExifTool;
my $et = Image::ExifTool->new();
my %data;
warn "reading exif from $self->{path}" if $self->{config}->param('debug');
my $exif = $et->ImageInfo((-d $self->{path}) ? $self->thumbnail_source : $self->{path});
return if $exif->{Error};
$self->{cache}->{exif}->{$self->{path}} = $exif;
return $exif;
}
=head2 thumbnail_sized($size)
reads C<$self->thumbnail_source> and returns a thumbnail in the
requested size. If C<$self->thumbnail_source> does not exist, it use
a icon based on the mime type.
It should not be called directly but through the caching methode C<$self->thumbnail>.
=cut
sub thumbnail_sized
{
use Image::Magick;
my $self = shift;
my $size = shift;
my $type = $self->{config}->param('thumb-type');
my $img = $self->thumbnail_source;
$img = File::Spec->catdir($self->{config}->param('icons'), $self->{type} .'.'. ICON_TYPE)
if !$img or not -r $img;
my @magick =
(
[ 'Read', $img ],
[ 'Resize', $size . 'x' . $size],
[ 'AutoOrient', 1],
[ 'ImageToBlob', { magick => $type } ]
);
my $image = Image::Magick->new;
foreach my $cmd (@magick)
{
my ($m, $p) = @$cmd;
my $ret = $image->$m($p);
return $ret if $m eq $magick[@magick-1]->[0];
warn $ret and return if $ret;
}
}
=head2 thumbnail_source
returns the source for the thumbnail.
Thats the original file that can be scaled via thumbnail_sized. Think
of it as a image represenation for the file type.
This method is empty and should be overwritten for images and videos to
have a real thumbnail.
=cut
sub thumbnail_source
{
}
=head2 thumbnail($size)
returns the actual thumbnail.
If the resized thumb already exist, return the path to that one.
If no, try to create it first by calling C<$self->thumbnail_sized>
=cut
sub thumbnail
{
my $self = shift;
my $size = shift or return $self->{path};
my $type = $self->{config}->param('thumb-type');
my $cache = $self->{config}->param('cache');
my $sized = File::Spec->catdir($cache, $self->{path} . '_' . $size . '.' . $type);
warn "sized: $sized" if $self->{config}->param('debug');
return $sized if -e $sized;
$self->prepare_dir($sized) or warn "could not create dir for $sized";
my $data = $self->thumbnail_sized($size);
if ($data and open my $fh, '>', $sized)
{
binmode($fh);
print $fh $data;
close $fh;
return $sized;
}
warn "could not write thumbnail to $sized: $!";
}
sub prepare_dir
( run in 0.567 second using v1.01-cache-2.11-cpan-5a3173703d6 )