X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/tgal/blobdiff_plain/227d23a02fd8d9ce91e47889c7497a1548a6f855..d8ab1deaa025fbf9736ada9d709314d9a67b2369:/mason/.perl-lib/TrivGal.pm diff --git a/mason/.perl-lib/TrivGal.pm b/mason/.perl-lib/TrivGal.pm index 9b05723..96f66f1 100644 --- a/mason/.perl-lib/TrivGal.pm +++ b/mason/.perl-lib/TrivGal.pm @@ -32,6 +32,7 @@ use Exporter qw{import}; use File::stat; use Image::ExifTool qw{}; use Image::Imlib2; +use Image::Size qw{}; use User::pwent; use POSIX; @@ -148,6 +149,8 @@ export qw{%SIZE}; our %SIZE = (smallthumb => 96, medthumb => 144, bigthumb => 228, + small => 480, + embed => 720, view => 1200); export qw{init}; @@ -211,53 +214,80 @@ package TrivGal::Image { my $imgpath = "$IMGROOT/$path"; my $st = stat $imgpath or die "no image `$path'"; return bless { - path => $path, + path => $path, imgpath => $imgpath, mtime => $st->mtime, - img => undef + img => undef, + _wd => undef, _ht => undef, + sz => undef }, $cls; } - sub scale ($$) { - my ($me, $scale) = @_; + sub _getsz ($) { + my ($me) = @_; + return if defined $me->{_wd}; + + my ($wd, $ht, $err) = Image::Size::imgsize $me->{imgpath}; + defined $wd or die "failed to read size of `$me->{path}': $err"; + my $sz = $wd; if ($sz < $ht) { $sz = $ht; } + @$me{"_wd", "_ht", "sz"} = ($wd, $ht, $sz); + } + + sub sz ($) { my ($me) = @_; $me->_getsz; return $me->{sz}; } + + sub scale ($$;$) { + my ($me, $scale, $forcep) = @_; + my $m = HTML::Mason::Request->instance; 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"; - my $exif = new Image::ExifTool; - $exif->ExtractInfo($imgpath); - my $orient = $exif->GetValue("Orientation", "ValueConv"); - $img = $me->{img} = Image::Imlib2->load($imgpath); - if (defined $orient) { - my ($rot, $flip) = @{$ORIENT{$orient}}; - if ($rot) { $img->image_orientate($rot); } - if ($flip) { $img->flip_horizontal(); } + my $url; + + if ($me->sz <= $sz) + { $url = $m->interp->apply_escapes("$IMGURL/$path", "u"); } + else { + my $tail = "scale.$sz/$path"; + my $thumb = "$CACHE/$tail"; + my $thumburl = $m->interp->apply_escapes("$CACHEURL/$tail", "u"); + + my $st = stat $thumb; + if ($st && $st->mtime > $me->{mtime}) + { $url = $thumburl; } + elsif (!$forcep) { + $url = + $m->interp->apply_escapes("$SCRIPTURL/$path", "u") . + "?scale=$scale"; + } else { + my ($dir, undef, $ext) = TrivGal::split_path $thumb; + my $ty = $TYPE{lc $ext} or die "unknown type `$ext'"; + + my $img = $me->{img}; + unless (defined $img) { + my $exif = new Image::ExifTool; + $exif->ExtractInfo($me->{imgpath}); + my $orient = $exif->GetValue("Orientation", "ValueConv"); + $img = $me->{img} = Image::Imlib2->load($me->{imgpath}); + if (defined $orient) { + my ($rot, $flip) = @{$ORIENT{$orient}}; + if ($rot) { $img->image_orientate($rot); } + if ($flip) { $img->flip_horizontal(); } + } + } + + my $sc = $sz/$me->sz; + 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, { mode => 0771 }; + $scaled->save($new); + make_path $dir, { mode => 0771 }; + rename $new, $thumb; + $url = $thumburl; } } - 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; + return $url; } } @@ -285,6 +315,7 @@ sub listdir ($) { my (@d, @f); my $ix = undef; + $path =~ s#/$##; if (-f "$path/.tgal.index") { open my $f, "<", "$path/.tgal.index"; my $item = undef; @@ -297,12 +328,14 @@ sub listdir ($) { $comment = defined $comment ? $comment . "\n" . $_ : $_; } else { if ($item && $comment) { $item->comment($comment); } - my ($indexp, $name, $c) = - /^ (! \s+)? # index flag + my ($flags, $name, $c) = + /^ (?: ([-!]+) \s+)? # flags (\S+) \s* # filename (\S | \S.*\S )? # start of the comment \s* $/x; + my $indexp = $flags =~ /!/; + my $hidep = $flags =~ /-/; $name = urldecode $name; my $list; $item = TrivGal::Item->new($name); @@ -311,7 +344,7 @@ sub listdir ($) { die "can't index a folder" if $indexp; } else { $list = \@f; - my ($dir, $base, $ext) = TrivGal::split_path $name; + my (undef, undef, $ext) = split_path $name; die "unknown image type" unless $TYPE{lc $ext}; if ($indexp) { die "two index images" if defined $ix; @@ -319,7 +352,7 @@ sub listdir ($) { } } $comment = $c; - push @$list, $item; + push @$list, $item unless $hidep; } } if ($item && $comment) { $item->comment($comment); } @@ -333,12 +366,12 @@ sub listdir ($) { closedir $d; ENT: for my $e (sort @e) { - my ($dir, $base, $ext) = split_path $e; + my (undef, undef, $ext) = split_path $e; my $dotp = $e =~ /^\./; my $st = stat "$path/$e"; my $list = undef; if ($dotp) { } - elsif (-d $st) { $list = \@d; } + elsif (-d $st) { $list = \@d; $e .= "/"; } elsif ($TYPE{lc $ext} && -f $st) { $list = \@f; } $list and push @$list, TrivGal::Item->new($e); }