| 1 | package Apache::Image; | ||
| 2 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 0 | ▶ | use Apache2::RequestRec (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use Apache2::RequestIO (); | ||
| 6 | use Apache2::Const -compile => qw(OK NOT_FOUND HTTP_MOVED_TEMPORARILY); | ||
| 7 | use Apache2::TrapSubRequest; | ||
| 8 | |||
| 9 | # XXX - Bug with getting uri/path_info | ||
| 10 | # If /z/image exists it works | ||
| 11 | # If /z/image does not, it is all broken | ||
| 12 | # If /z/image/thubnail exists it drops the first part of path info ! | ||
| 13 | # all bad | ||
| 14 | |||
| 15 | # XXX We should use Image::Thumbnail !!! | ||
| 16 | use Image::Magick; | ||
| 17 | |||
| 18 | use Carp; | ||
| 19 | use version; our $VERSION = qv('0.0.4'); | ||
| 20 | |||
| 21 | # TODO Can this be made to be Apaceh 1.3 (mod_perl 1) ? If not document the reason | ||
| 22 | |||
| 23 | sub handler { | ||
| 24 | my $r = shift; | ||
| 25 | |||
| 26 | # URI (original) & Configuration | ||
| 27 | my $uri=$r->path_info; | ||
| 28 | |||
| 29 | # Get the size from config | ||
| 30 | my $MaxSize=$r->dir_config("ImageMaxSize") || 50; | ||
| 31 | |||
| 32 | # Force this size? Or only resize if larger? | ||
| 33 | my $Force=$r->dir_config("ImageForce") || 0; | ||
| 34 | |||
| 35 | # Get our image from Apache | ||
| 36 | my $image; | ||
| 37 | my $subr = $r->lookup_uri($uri); | ||
| 38 | # XXX Do other things like POST data etc. | ||
| 39 | $subr->args($r->args); | ||
| 40 | $subr->run_trapped(\$image); | ||
| 41 | |||
| 42 | # XXX Check not found or other error | ||
| 43 | # XXX Check the Pragma and Expiry times for caching and setting local expiry times | ||
| 44 | |||
| 45 | # Check mime type is image (else return based on mime type) | ||
| 46 | if ($subr->content_type !~ m|^image/([^\s]+)|) { | ||
| 47 | # print STDERR "Not a valid type for $rest " . $subr->content_type . "\n"; | ||
| 48 | # XXX Get this from configuration | ||
| 49 | $r->internal_redirect("/z/resource/thumbnail/unknown.jpg"); | ||
| 50 | # TODO Support mime type mapping to files (can we use Apache for this?) | ||
| 51 | # TODO Future - Support creation of Thumbnails from other sources | ||
| 52 | return Apache2::Const::OK; | ||
| 53 | } | ||
| 54 | |||
| 55 | # XXX Caching - Write entries to a directory and use internal redirect | ||
| 56 | # to get them - only problem may be that a tif wants to be a jpg | ||
| 57 | # thumbnail | ||
| 58 | # $r->internal_redirect("/unknown.jpg"); | ||
| 59 | |||
| 60 | # Process the image into a thumbnail and output | ||
| 61 | my $im = Image::Magick->new; | ||
| 62 | $im->BlobToImage($image); | ||
| 63 | |||
| 64 | # Resize if Force or Too big ! | ||
| 65 | if ($Force || ($im->Get('width') > $MaxSize) || ($im->Get('height') > $MaxSize)) { | ||
| 66 | $im->Scale(geometry => $MaxSize . 'x' . $MaxSize); | ||
| 67 | } | ||
| 68 | |||
| 69 | # XXX Is it always ? | ||
| 70 | $r->content_type('image/jpg'); | ||
| 71 | $r->rflush(); | ||
| 72 | print $im->ImageToBlob(); | ||
| 73 | |||
| 74 | # XXX Save to cache (files to redirect to) (internal Apache caching) | ||
| 75 | |||
| 76 | return Apache2::Const::OK; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | 1; # Magic true value required at end of module | ||
| 81 | __END__ | ||