| 1 | package Catalyst::View::GD::Thumbnail; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use parent 'Catalyst::View'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Image::Info qw/image_type/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Image::Resize; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use List::Util qw/min max/; | ||
| 9 | |||
| 10 | our $VERSION = '0.14'; | ||
| 11 | |||
| 12 | sub process { | ||
| 13 | my ($self, $c) = @_; | ||
| 14 | my $file = $c->stash->{thumbnail}->{image}; | ||
| 15 | my $x = $c->stash->{thumbnail}->{x}; | ||
| 16 | my $y = $c->stash->{thumbnail}->{y}; | ||
| 17 | |||
| 18 | if(!defined $c->stash->{thumbnail}->{quality}) { | ||
| 19 | $c->stash->{thumbnail}->{quality} = ""; | ||
| 20 | } | ||
| 21 | |||
| 22 | my $quality = $c->stash->{thumbnail}->{quality}; | ||
| 23 | my $mime = image_type($file)->{file_type}; | ||
| 24 | my $resize = Image::Resize->new($file); | ||
| 25 | my $source_aspect = $resize->width / $resize->height; | ||
| 26 | $x ||= $y * $source_aspect; | ||
| 27 | $y ||= $x / $source_aspect; | ||
| 28 | my $thumbnail = $resize->resize($x,$y); | ||
| 29 | |||
| 30 | if($mime eq 'BMP') { | ||
| 31 | $thumbnail = $thumbnail->bmp; | ||
| 32 | } | ||
| 33 | elsif($mime eq 'GIF') { | ||
| 34 | $thumbnail = $thumbnail->gif; | ||
| 35 | } | ||
| 36 | elsif($mime eq 'JPEG') { | ||
| 37 | if($quality eq "" || $quality > 100 || $quality < 0) { | ||
| 38 | $thumbnail = $thumbnail->jpeg; | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | $thumbnail = $thumbnail->jpeg($quality); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | elsif($mime eq 'PNG') { | ||
| 45 | $thumbnail = $thumbnail->png; | ||
| 46 | if($quality eq "" || $quality > 9 || $quality < 0) { | ||
| 47 | $thumbnail = $thumbnail->png; | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | $thumbnail = $thumbnail->png($quality); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | elsif($mime eq 'X-XBITMAP') { | ||
| 54 | $thumbnail = $thumbnail->xbm; | ||
| 55 | } | ||
| 56 | elsif($mime eq 'X-XPIXMAP') { | ||
| 57 | $thumbnail = $thumbnail->xpm; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | my $error = qq/Unsupported image format/; | ||
| 61 | $c->log->error($error); | ||
| 62 | $c->error($error); | ||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | |||
| 66 | $c->response->content_type($mime); | ||
| 67 | $c->response->body($thumbnail); | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; | ||
| 71 | |||
| 72 | __END__ | ||