use Errno;
use Exporter qw{import};
-use File::Path qw{make_path};
use File::stat;
use Image::Imlib2;
use User::pwent;
###--------------------------------------------------------------------------
### Scaled images.
-export qw{scaled};
-sub scaled ($$) {
- my ($scale, $path) = @_;
-
- my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
- my $imgpath = "$IMGROOT/$path";
- my $ist = stat $imgpath or die "no image `$path'";
- my $thumb = "$CACHE/scaled.$scale/$path";
- my $thumburl = "$CACHEURL/scaled.$scale/$path";
- my $tst = stat $thumb;
- if (defined $tst && $tst->mtime > $ist->mtime) { return $thumburl; }
- my ($dir, $base, $ext) = split_path $thumb;
- my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
-
- my $img = Image::Imlib2->load($imgpath);
- my ($wd, $ht) = ($img->width, $img->height);
- my $max = $wd > $ht ? $wd : $ht;
- if ($max <= $sz) { return "$IMGURL/$path"; }
- my $sc = $sz/$max;
- my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
-
- $scaled->image_set_format($ty->imlibfmt);
- $scaled->set_quality(90);
- my $new = "$TMP/t${$}$ext";
- make_path $TMP;
- $scaled->save($new);
- make_path $dir;
- rename $new, $thumb;
- return $thumburl;
+package TrivGal::Image {
+ use File::Path qw{make_path};
+ use File::stat;
+
+ sub new ($$) {
+ my ($cls, $path) = @_;
+ my $imgpath = "$IMGROOT/$path";
+ my $st = stat $imgpath or die "no image `$path'";
+ return bless {
+ path => $path,
+ mtime => $st->mtime,
+ img => undef
+ }, $cls;
+ }
+
+ sub scale ($$) {
+ my ($me, $scale) = @_;
+
+ my $path = $me->{path};
+ my $sz = $SIZE{$scale} or die "unknown scale `$scale'";
+ my $thumb = "$CACHE/scale.$sz/$path";
+ my $thumburl = "$CACHEURL/scale.$sz/$path";
+ my $st = stat $thumb;
+ if (defined $st && $st->mtime > $me->{mtime}) { return $thumburl; }
+
+ my ($dir, $base, $ext) = TrivGal::split_path $thumb;
+ my $ty = $TYPE{lc $ext} or die "unknown type `$ext'";
+
+ my $img = $me->{img};
+ unless (defined $img) {
+ my $imgpath = "$IMGROOT/$path";
+ $img = $me->{img} = Image::Imlib2->load($imgpath);
+ }
+
+ my ($wd, $ht) = ($img->width, $img->height);
+ my $max = $wd > $ht ? $wd : $ht;
+ if ($max <= $sz) { return "$IMGURL/$path"; }
+ my $sc = $sz/$max;
+ my $scaled = $img->create_scaled_image($sc*$wd, $sc*$ht);
+
+ $scaled->image_set_format($ty->imlibfmt);
+ $scaled->set_quality(90);
+ my $new = "$TMP/t${$}$ext";
+ make_path $TMP;
+ $scaled->save($new);
+ make_path $dir;
+ rename $new, $thumb;
+ return $thumburl;
+ }
}
###--------------------------------------------------------------------------
<div class=gallery>
% for my $d (@$dd) {
% my ($ddd, $fff, $iii) = listdir $real . "/" . $d->name;
-% my $tn;
-% if ($iii) { $tn = join_paths $path, $d->name, $iii->name; }
-% else { $tn = undef; }
+% my $tn = $iii ?
+% TrivGal::Image->new(join_paths $path, $d->name, $iii->name) :
+% undef;
<& .thumbnail, target => $d->name . "/", comment => $d->comment,
img => $tn, size => "bigthumb",
caption => $m->interp->apply_escapes($d->name, "h") &>\
<div class=gallery>
% for my $f (@$ff) {
<& .thumbnail, target => $f->name, comment => $f->comment,
- img => $path . $f->name, size => "bigthumb",
+ img => TrivGal::Image->new($path . $f->name),
+ size => "bigthumb",
caption => $m->interp->apply_escapes($f->name, "h") &>\
% }
</div>
my $realdir = join_paths $IMGROOT, $dir;
my $urldir = join_paths $SCRIPTURL, $dir;
my ($dd, $ff, $ii) = listdir $realdir;
- my $vw = scaled "view", $path;
+ my $vw = TrivGal::Image->new($path)->scale("view");
my $fi = undef;
FILE: for (my $i = 0; $i < @$ff; $i++)
%
<div class=thumbstrip>
% for my $f (@$ff) {
-% my $img = $dir . "/" . $f->name;
+% my $img = TrivGal::Image->new($dir . "/" . $f->name);
<& .thumbnail, target => $f->name, img => $img, size => "bigthumb",
caption => $m->interp->apply_escapes($f->name, "h"),
focus => $f->name eq "$base$ext" &>\
%###-------------------------------------------------------------------------
<%def .thumbnail>\
% my $tn;
-% if (defined $img) { $tn = scaled $size, $img; }
+% if (defined $img) { $tn = $img->scale($size); }
% else { $tn = "$STATICURL/folder.svg"; }
% if ($focus) {
<figure class="thumb <% $size %>" id=focusthumb>