From 4c54e59e531744ec56164fc98f6f8a2d5f1837cc Mon Sep 17 00:00:00 2001 From: difegue Date: Sat, 23 Jul 2022 02:08:44 +0200 Subject: [PATCH] Optimize thumbnail generation Thanks @RePod! --- lib/LANraragi/Utils/Archive.pm | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/LANraragi/Utils/Archive.pm b/lib/LANraragi/Utils/Archive.pm index 3b8b9c96d..9ad2c14f3 100644 --- a/lib/LANraragi/Utils/Archive.pm +++ b/lib/LANraragi/Utils/Archive.pm @@ -36,13 +36,19 @@ sub is_pdf { return ( $suffix eq ".pdf" ); } -# generate_thumbnail(original_image, thumbnail_location) +# generate_thumbnail(original_image, thumbnail_location, use_sample) # use ImageMagick to make a thumbnail, height = 500px (view in index is 280px tall) +# If use_sample is true, the sample algorithm will be used instead of scale. sub generate_thumbnail { - my ( $orig_path, $thumb_path ) = @_; + my ( $orig_path, $thumb_path, $use_sample ) = @_; my $img = Image::Magick->new; + # For JPEG, the size option (or jpeg:size option) provides a hint to the JPEG decoder + # that it can reduce the size on-the-fly during decoding. This saves memory because + # it never has to allocate memory for the full-sized image + $img->Set( option => 'jpeg:size=500x' ); + # If the image is a gif, only take the first frame if ( $orig_path =~ /\.gif$/ ) { $img->Read( $orig_path . "[0]" ); @@ -50,7 +56,12 @@ sub generate_thumbnail { $img->Read($orig_path); } - $img->Thumbnail( geometry => '500x1000' ); + # Sample is very fast due to not applying filters. + if ($use_sample) { + $img->Sample( geometry => '500x1000' ); + } else { # The "-scale" resize operator is a simplified, faster form of the resize command. + $img->Scale( geometry => '500x1000' ); + } $img->Set( quality => "50", magick => "jpg" ); $img->Write($thumb_path); undef $img; @@ -179,7 +190,7 @@ sub extract_thumbnail { } # Thumbnail generation - generate_thumbnail( $arcimg, $thumbname ); + generate_thumbnail( $arcimg, $thumbname, $page > 0 ); # Clean up safe folder remove_tree($temppath);