You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axkit-dev@xml.apache.org by ma...@sergeant.org on 2006/08/06 01:30:25 UTC

[SVN] [49] Make next/prev links work on images.

Revision: 49
Author:   matt
Date:     2006-08-05 23:29:53 +0000 (Sat, 05 Aug 2006)

Log Message:
-----------
Make next/prev links work on images.
Use Image::Epeg if it's available for resizing.

Modified Paths:
--------------
    trunk/plugins/demo/gallery

Modified: trunk/plugins/demo/gallery
===================================================================
--- trunk/plugins/demo/gallery	2006-08-05 23:28:40 UTC (rev 48)
+++ trunk/plugins/demo/gallery	2006-08-05 23:29:53 UTC (rev 49)
@@ -40,8 +40,10 @@
 use RDF::Core::Literal;
 use RDF::Core::Statement;
 use RDF::Core::Model::Serializer;
-use AxKit2::Utils qw(uri_decode);
+use AxKit2::Utils qw(uri_decode uri_encode);
 
+use constant EPEG_AVAILABLE => eval { require Image::Epeg };
+
 our $DEFAULT_SIZE = '133 640 800 1024';
 
 sub hook_xmlresponse {
@@ -67,7 +69,7 @@
     my $format = $client->param('format') || 'html';
     
     if ($format eq 'html') {
-        return $self->serve_html_page($input, $ct);
+        return $self->serve_image_page($input, $ct);
     }
     
     # Now we just serve the raw image, possibly resized
@@ -88,7 +90,8 @@
         $client->headers_out->header('Content-Type', $ct);
         $client->send_http_headers;
         local $/;
-        $client->write(<$fh>);
+        my $out = <$fh>;
+        $client->write(\$out);
         return OK;
     }
     
@@ -115,6 +118,36 @@
     
     $self->log(LOGINFO, "Resizing image $file to size $size");
     
+    my ($type) = $ct =~ /\/(.*)$/;
+    
+    my $out;
+    
+    if ($type eq 'jpeg' && EPEG_AVAILABLE) {
+        my $epg = Image::Epeg->new($file);
+        $epg->resize($size, $size, Image::Epeg::MAINTAIN_ASPECT_RATIO());
+        $out = $epg->get_data();
+        if (!$out) {
+            # Epeg didn't work. Resort to Imager anyway
+            $self->resize_image($size, $file, $type, \$out);
+        }
+    }
+    else {
+        $self->resize_image($size, $file, $type, \$out);
+    }
+    
+    $cache->set("$file+$size", $out);
+    
+    $client->headers_out->header('Content-Length', length($out));
+    $client->headers_out->header('Content-Type', $ct);
+    $client->send_http_headers;
+    # using ->get here makes sure Cache::Cache expires stuff
+    $client->write(\$out);
+    return OK;
+}
+
+sub resize_image {
+    my ($self, $size, $file, $type, $out) = @_;
+    
     my $image = Imager->new;
     
     $image->open(file => $file)
@@ -126,7 +159,7 @@
     
     my $quality = $self->get_cfg('GalleryThumbQuality') || 'preview';
     $quality = 'normal' if $quality ne 'preview';
-    $quality = 'normal' if $client->param('size') ne 'thumb';
+    $quality = 'normal' if $self->client->param('size') ne 'thumb';
     
     $self->log(LOGINFO, "Scaling to $size");
     
@@ -144,22 +177,11 @@
         coef => [-0.2, 1, -0.2]
         ) if $quality eq 'normal';
     
-    my $out;
-    my ($type) = $ct =~ /\/(.*)$/;
-    $thumb->write(data => \$out, type => $type)
+    $thumb->write(data => $out, type => $type)
         or die "Cannot write to scalar: ", $thumb->errstr;
-    $cache->set("$file+$size", $out);
-    
-    
-    $client->headers_out->header('Content-Length', length($out));
-    $client->headers_out->header('Content-Type', $ct);
-    $client->send_http_headers;
-    # using ->get here makes sure Cache::Cache expires stuff
-    $client->write(\$out);
-    return OK;
 }
 
-sub serve_html_page {
+sub serve_image_page {
     my ($self, $input, $ct) = @_;
     
     $self->log(LOGINFO, "Serving Imagesheet");
@@ -172,6 +194,31 @@
     my $path;
     ($path, $file) = $file =~ /(.*)\/(.*)/;    # Extract the path/file info
     
+    my $mm = File::MMagic->new;
+    
+    opendir(DIR, $path);
+    my ($prev, $next);
+    my $found = 0;
+    while (my $entry = readdir(DIR)) {
+        print "Entry: $entry\n";
+        next if $entry =~ /^\./;
+        next if -d $entry;
+        if ($entry eq $file) {
+            $found++;
+            next;
+        }
+        my $type = $mm->checktype_filename("$path/$entry");
+        print "$entry is of type: $type\n";
+        next unless $type =~ /^image\//;
+        if ($found) {
+            $next = $entry;
+            last;
+        }
+        else {
+            $prev = $entry;
+        }
+    }
+    
     my $uri = $self->client->headers_in->request_uri;
     $uri =~ s/\?.*//;
     
@@ -252,7 +299,10 @@
     }
     
     my $out = $input->transform(
-        XSLT($self->config->docroot . '/stylesheets/imagesheet2html.xsl')
+        XSLT($self->config->docroot . '/stylesheets/imagesheet2html.xsl',
+             $prev ? ('prev' => uri_encode($prev)) : (),
+             $next ? ('next' => uri_encode($next)) : (),
+             )
     );
     
     return OK, $out;