Imager-Album
view release on metacpan or search on metacpan
sub set_working_dir {
my $self = shift;
my $base = shift || $ENV{HOME}."/.Imager__Album_$$";
if (!-d $base) {
mkdir($base,0777)
or die "Couldn't create working dir '$base': $!\n";
}
if (!-d "$base/preview") {
mkdir("$base/preview",0777)
or die "Couldn't create working dir '$base/preview': $!\n";
}
$self->{'dir'} = $base;
}
sub add_image {
my $self = shift;
my $fname = shift;
my $num = $self->{'seq'}++;
my ($name, $path, $suffix) = fileparse($fname, @suf);
my %hash;
return undef unless -f $fname;
$hash{path} = $fname;
$hash{name} = $name;
$hash{caption} = $name;
$hash{rotated} = 0;
$hash{valid} = 0;
push(@{$self->{'ordering'}}, $num);
$self->{'images'}->{$num} = \%hash;
}
sub get_image {
my ($self, $imageno) = @_;
return $self->{'images'}->{$imageno};
}
sub get_preview_path {
my ($self, $imageno) = @_;
my $path = File::Spec->catfile($self->{'dir'},
'preview',
$imageno.".png");
return $path;
}
# Find which images need to have previews
# updated and call update_preview on those.
sub update_previews {
my $self = shift;
my %images = %{$self->{'images'}};
my @process = grep { !$images{$_}->{valid} } keys %images;
my $imageno;
my $c = 1;
for $imageno (@process) {
print "Updating preview $c/".@process."\n";
$c++;
$self->update_preview($imageno);
}
}
# Update an images preview file
sub update_preview {
my $self = shift;
my $imageno = shift;
my %opts = %{$self->{'preview_opts'}};
my $image = $self->{'images'}->{$imageno};
my $file = $image->{'path'};
my $img = Imager->new();
if (!$img->read(file=>$file)) {
print "ERROR $file: ".$img->errstr;
return ();
}
my $prev = $img->scale(%opts);
if ($image->{'rotated'}) {
$prev = $prev->rotate(degrees=>$image->{'rotated'}*90);
}
my $dir = $self->{'dir'};
$prev->write(file=>"$dir/preview/$imageno.png") or die $!;
$image->{'valid'} = 1;
}
sub rotate_image_cw {
my ($self, @imagenos) = @_;
my @images = map { $self->{'images'}->{$_} } @imagenos;
for (@images) {
$_->{'rotated'}++;
$_->{'valid'} = 0;
}
}
sub rotate_image_ccw {
my ($self, @imagenos) = @_;
my @images = map { $self->{'images'}->{$_} } @imagenos;
for (@images) {
$_->{'rotated'}--;
$_->{'valid'} = 0;
}
}
sub change_order {
my ($self, $from, $to) = @_;
my $aref = $self->{'ordering'};
my $tmp = splice(@{$aref}, $from, 1);
splice(@{$aref}, $to, 0, $tmp);
}
# Give ids
sub remove_images {
my ($self, @imagenos) = @_;
delete $self->{'images'}->{$_} for @imagenos;
my $imageno;
( run in 0.990 second using v1.01-cache-2.11-cpan-bbe5e583499 )