diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Multi/Image.pm | 39 | ||||
-rw-r--r-- | lib/VNDB/DB/VN.pm | 3 | ||||
-rw-r--r-- | lib/VNDBUtil.pm | 19 |
3 files changed, 30 insertions, 31 deletions
diff --git a/lib/Multi/Image.pm b/lib/Multi/Image.pm index 0ddb1853..ad1d436c 100644 --- a/lib/Multi/Image.pm +++ b/lib/Multi/Image.pm @@ -10,6 +10,7 @@ use warnings; use POE; use Image::Magick; use Time::HiRes 'time'; +use VNDBUtil 'imgsize'; sub spawn { @@ -65,13 +66,15 @@ sub cv_process { # num, res my $im = Image::Magick->new; $im->Read($img); $im->Set(magick => 'JPEG'); - my($old, $new) = do_resize($im, $VNDB::S{cv_size}); + my($ow, $oh) = ($im->Get('width'), $im->Get('height')); + my($nw, $nh) = imgsize($ow, $oh, @{$VNDB::S{cv_size}}); + $im->Thumbnail(width => $nw, height => $nh); $im->Set(quality => 80); $im->Write($img); $_[KERNEL]->post(pg => do => 'UPDATE vn_rev SET image = image*-1 WHERE image = ?', [ -1*$id ]); $_[KERNEL]->call(core => log => 'Processed cover image %d in %.2fs: %.2fkB (%dx%d) -> %.2fkB (%dx%d)', - $id, time-$start, $os/1024, $$old[0], $$old[1], (-s $img)/1024, $$new[0], $$new[1]); + $id, time-$start, $os/1024, $ow, $oh, (-s $img)/1024, $nw, $nh); $_[KERNEL]->yield('cv_check'); } @@ -100,46 +103,24 @@ sub scr_process { # num, res $im->Write($sf); # create thumbnail - my($old, $new) = do_resize($im, $VNDB::S{scr_size}); + my($ow, $oh) = ($im->Get('width'), $im->Get('height')); + my($nw, $nh) = imgsize($ow, $oh, @{$VNDB::S{scr_size}}); + $im->Thumbnail(width => $nw, height => $nh); $im->Set(quality => 90); $im->Write($st); $_[KERNEL]->post(pg => do => 'UPDATE screenshots SET processed = true, width = ?, height = ? WHERE id = ?', - [ $$old[0], $$old[1], $id ] + [ $ow, $oh, $id ] ); $_[KERNEL]->call(core => log => 'Processed screenshot #%d in %.2fs: %.1fkB -> %.1fkB (%dx%d), thumb: %.1fkB (%dx%d)', - $id, time-$start, $os/1024, (-s $sf)/1024, $$old[0], $$old[1], (-s $st)/1024, $$new[0], $$new[1] + $id, time-$start, $os/1024, (-s $sf)/1024, $ow, $oh, (-s $st)/1024, $nw, $nh ); $_[KERNEL]->yield('scr_check'); } - - -# non-POE helper function -sub do_resize { # im, [ maxwidth, maxheight ] - my($im, $dim) = @_; - - my($w, $h) = ($im->Get('width'), $im->Get('height')); - $dim = [ $w, $h ] if !$dim; - my($ow, $oh) = ($w, $h); - if($w > $$dim[0] || $h > $$dim[1]) { - if($w/$h > $$dim[0]/$$dim[1]) { # width is the limiting factor - $h *= $$dim[0]/$w; - $w = $$dim[0]; - } else { - $w *= $$dim[1]/$h; - $h = $$dim[1]; - } - } - $im->Thumbnail(width => $w, height => $h); - - return ([$ow, $oh], [$w, $h]); -} - - 1; diff --git a/lib/VNDB/DB/VN.pm b/lib/VNDB/DB/VN.pm index da479a9a..394e8c06 100644 --- a/lib/VNDB/DB/VN.pm +++ b/lib/VNDB/DB/VN.pm @@ -205,10 +205,11 @@ sub dbScreenshotGet { # Fetch random VN + screenshots sub dbScreenshotRandom { return shift->dbAll(q| - SELECT vs.scr, vr.vid, vr.title + SELECT vs.scr, vr.vid, vr.title, s.width, s.height FROM vn_screenshots vs JOIN vn v ON v.latest = vs.vid JOIN vn_rev vr ON vr.id = v.latest + JOIN screenshots s ON vs.scr = s.id WHERE vs.nsfw = FALSE AND v.hidden = FALSE ORDER BY RANDOM() LIMIT 4| diff --git a/lib/VNDBUtil.pm b/lib/VNDBUtil.pm index 6d4d437e..3060b75b 100644 --- a/lib/VNDBUtil.pm +++ b/lib/VNDBUtil.pm @@ -7,7 +7,7 @@ use warnings; use Exporter 'import'; use Unicode::Normalize 'NFKD'; -our @EXPORT = qw|shorten bb2html gtintype normalize normalize_titles normalize_query|; +our @EXPORT = qw|shorten bb2html gtintype normalize normalize_titles normalize_query imgsize|; sub shorten { @@ -200,5 +200,22 @@ sub normalize_query { } +# arguments: <image size>, <max dimensions> +# returns the size of the thumbnail with the same aspect ratio as the full-size +# image, but fits within the specified maximum dimensions +sub imgsize { + my($ow, $oh, $sw, $sh) = @_; + return ($ow, $oh) if $ow <= $sw && $oh <= $sh; + if($ow/$oh > $sw/$sh) { # width is the limiting factor + $oh *= $sw/$ow; + $ow = $sw; + } else { + $ow *= $sh/$oh; + $oh = $sh; + } + return ($ow, $oh); +} + + 1; |