resize images using Graphics::Magick
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 23 Jun 2012 21:06:27 +0000 (23:06 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 23 Jun 2012 21:06:27 +0000 (23:06 +0200)
plack/lib/Plack/App/BookReader.pm

index a71b69b..0c07304 100644 (file)
@@ -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 = "<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>";
@@ -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};