Skip to content

Commit

Permalink
Optimize thumbnail generation
Browse files Browse the repository at this point in the history
Thanks @RePod!
  • Loading branch information
Difegue committed Jul 23, 2022
1 parent ef7ef7a commit 4c54e59
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/LANraragi/Utils/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,32 @@ 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]" );
} else {
$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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4c54e59

Please sign in to comment.