From 1652f5a0c2d3f14967c6fe2096a821bdc7ab17e9 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 23 Jun 2012 23:06:27 +0200 Subject: [PATCH 1/1] resize images using Graphics::Magick --- plack/lib/Plack/App/BookReader.pm | 38 +++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/plack/lib/Plack/App/BookReader.pm b/plack/lib/Plack/App/BookReader.pm index a71b69b..0c07304 100644 --- a/plack/lib/Plack/App/BookReader.pm +++ b/plack/lib/Plack/App/BookReader.pm @@ -9,6 +9,8 @@ use DirHandle; use URI::Escape; use Plack::Request; use Data::Dump qw(dump); +use File::Path qw(make_path); +use Graphics::Magick; # Stolen from rack/directory.rb my $dir_file = "%s%s%s%s"; @@ -59,13 +61,35 @@ sub return_dir_redirect { } sub serve_path { - my($self, $env, $dir, $fullpath) = @_; - - if (-f $dir) { - - if ( - - return $self->SUPER::serve_path($env, $dir, $fullpath); + my($self, $env, $path, $fullpath) = @_; + + if (-f $path) { + + my $req = Plack::Request->new($env); + if ( my $reduce = $req->param('reduce') ) { + $reduce = int($reduce); # BookReader javascript somethimes returns float + warn "# -scale 1/$reduce $path\n"; + + my $cache_path = "cache/$path.reduce.$reduce.jpg"; + if ( $reduce <= 1 ) { + $cache_path = $path; + } elsif ( ! -e $cache_path ) { + my $image = Graphics::Magick->new( magick => 'jpg' ); + $image->Read($path); + my ( $w, $h ) = $image->Get('width','height'); + $image->Resize( + width => $w / $reduce, + height => $h / $reduce + ); + $image->Write( filename => $cache_path ); + warn "# created $cache_path ", -s $cache_path, " bytes\n"; + } + + return $self->SUPER::serve_path($env, $cache_path, $fullpath); + + } + + return $self->SUPER::serve_path($env, $path, $fullpath); } my $dir_url = $env->{SCRIPT_NAME} . $env->{PATH_INFO}; -- 2.20.1