| 1 | package AxKit::App::Gallery::Plugin; | ||
| 2 | |||
| 3 | # Copyright (c) 2003 Nik Clayton | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions | ||
| 8 | # are met: | ||
| 9 | # 1. Redistributions of source code must retain the above copyright | ||
| 10 | # notice, this list of conditions and the following disclaimer. | ||
| 11 | # 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | # notice, this list of conditions and the following disclaimer in the | ||
| 13 | # documentation and/or other materials provided with the distribution. | ||
| 14 | # | ||
| 15 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
| 16 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 18 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 19 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 20 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 21 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 22 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 23 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 24 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 25 | # SUCH DAMAGE. | ||
| 26 | # | ||
| 27 | # $Id: Plugin.pm,v 1.5 2003/11/09 12:33:21 nik Exp $ | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | use Apache::Constants qw(OK DECLINED); | ||
| 31 | use Apache::Request; | ||
| 32 | use URI; | ||
| 33 | use URI::Escape; | ||
| 34 | use Imager; # Scaling | ||
| 35 | use Image::Info; # For width/height | ||
| 36 | |||
| 37 | # The job here is two-fold. First, to decide if AxKit should process this | ||
| 38 | # resource (and thus return OK), second, if it shouldn't process the | ||
| 39 | # resource, and a jpeg should be created, then to create the jpeg, cache | ||
| 40 | # it, set $r->filename() to the location of the cached jpeg, and then | ||
| 41 | # return DECLINED. | ||
| 42 | |||
| 43 | sub handler { | ||
| 44 | my $r = Apache::Request->new(shift); | ||
| 45 | |||
| 46 | # $r->log_error('In the plugin handler'); | ||
| 47 | # $r->log_error('args: ' . $r->args()); | ||
| 48 | |||
| 49 | # Always return OK for a directory | ||
| 50 | # $r->log_error('Checking to see if it\'s a directory: ' . $r->filename()); | ||
| 51 | return OK if -d $r->filename(); | ||
| 52 | |||
| 53 | return OK unless substr($r->content_type(), 0, 6) eq 'image/'; | ||
| 54 | |||
| 55 | # $r->log_error('Filename is: ' . $r->filename()); | ||
| 56 | |||
| 57 | my $format = $r->param('format'); | ||
| 58 | # $r->log_error("Format param was $format"); | ||
| 59 | # If no format parameter was passed in then AxKit can process the URI | ||
| 60 | # If the format param is 'html' then AxKit can process the URI | ||
| 61 | return OK if ! defined $format; | ||
| 62 | return OK if $format eq 'html'; | ||
| 63 | |||
| 64 | # $r->log_error('Filename is: ' . $r->filename()); | ||
| 65 | |||
| 66 | # Make sure the specified size is one we're configured to | ||
| 67 | # support. If it isn't then use the default size | ||
| 68 | my $sizelist = $r->dir_config('GallerySizes'); | ||
| 69 | $sizelist = '133 640 800 1024' unless defined $sizelist; | ||
| 70 | my @sizes = split(/\s+/, $sizelist); | ||
| 71 | my $size = $r->param('size'); | ||
| 72 | if($size eq 'thumb') { | ||
| 73 | $size = $sizes[0]; | ||
| 74 | } else { | ||
| 75 | $size = $sizes[1] unless grep { $_ eq $size } @sizes; | ||
| 76 | } | ||
| 77 | |||
| 78 | # Get the larger of the image's width and height. If this is smaller than | ||
| 79 | # the size user has requested, change the size to 'full' | ||
| 80 | # XXX should write code to do this | ||
| 81 | |||
| 82 | # $r->log_error("New size is $size"); | ||
| 83 | # $r->log_error('Filename is: ' . $r->filename()); | ||
| 84 | |||
| 85 | # If the size is 'full' then we're sending back the full size | ||
| 86 | # image. There's no work to do, so just return DECLINED | ||
| 87 | return DECLINED if $size eq 'full'; | ||
| 88 | |||
| 89 | # Now we know what size the image should be, check to see if a | ||
| 90 | # cached copy already exists. | ||
| 91 | my $cache_dir = $r->dir_config('GalleryCache'); | ||
| 92 | # my $uri = URI->new($r->uri()); | ||
| 93 | # my $cachepath = "$cache_dir/$uri"; | ||
| 94 | my $cachepath = $cache_dir . $r->filename(); | ||
| 95 | |||
| 96 | my $cachedfile = "$cachepath/$size.jpg"; | ||
| 97 | $cachedfile = uri_unescape($cachedfile); | ||
| 98 | |||
| 99 | # $r->log_error("Cachedfile is $cachedfile"); | ||
| 100 | |||
| 101 | my $image = Imager->new(); | ||
| 102 | |||
| 103 | if(! -f $cachedfile | ||
| 104 | || -z $cachedfile | ||
| 105 | || (stat($r->filename()))[9] > (stat($cachedfile))[9]) { | ||
| 106 | # $r->log_error("will open " . $r->filename()); | ||
| 107 | |||
| 108 | $image->open(file => $r->filename()) | ||
| 109 | or die $image->errstr(); | ||
| 110 | |||
| 111 | # $r->log_error("opened original image"); | ||
| 112 | |||
| 113 | my($w, $h) = ($image->getwidth(), $image->getheight()); | ||
| 114 | |||
| 115 | my $quality = $r->dir_config('GalleryThumbQuality'); | ||
| 116 | $quality = 'normal' if $quality ne 'preview'; | ||
| 117 | |||
| 118 | my $thumb = $image->scale(qtype => $quality, | ||
| 119 | $w > $h | ||
| 120 | ? (xpixels => $size) | ||
| 121 | : (ypixels => $size)); | ||
| 122 | |||
| 123 | # $r->log_error("scaled image"); | ||
| 124 | |||
| 125 | $thumb->write(file => $cachedfile); | ||
| 126 | |||
| 127 | # $r->log_error("Wrote scaled image to $cachedfile"); | ||
| 128 | } | ||
| 129 | |||
| 130 | $r->filename($cachedfile); | ||
| 131 | return DECLINED; | ||
| 132 | } | ||
| 133 | |||
| 134 | 1; |