File Coverage

blib/lib/Catalyst/View/GD/Thumbnail.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Catalyst::View::GD::Thumbnail;
2              
3 1     1   20402 use warnings;
  1         3  
  1         29  
4 1     1   5 use strict;
  1         2  
  1         30  
5 1     1   725 use parent 'Catalyst::View';
  1         265  
  1         5  
6             use Image::Info qw/image_type/;
7             use Image::Resize;
8             use List::Util qw/min max/;
9              
10             our $VERSION = '0.13';
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__
73              
74             =encoding utf8
75              
76             =head1 NAME
77              
78             Catalyst::View::GD::Thumbnail - Catalyst view to resize images for thumbnails.
79              
80             =cut
81              
82             =head1 SYNOPSIS
83              
84             Create a thumbnail view:
85              
86             script/myapp_create view Thumbnail Thumbnail
87              
88             Then in your controller:
89              
90             sub thumbnail :Local :Args(1) {
91             my ($self, $c, $image_file_path) = @_;
92              
93             $c->stash->{x} = 100;
94             # Create a 100px wide thumbnail
95              
96             #or
97              
98             $c->stash->{y} = 100;
99             # Create a 100px tall thumbnail
100              
101             $c->stash->{image} = $image_file_path;
102             $c->forward('View::Thumbnail');
103             }
104              
105              
106             =head1 DESCRIPTION
107              
108             Catalyst::View::GD::Thumbnail resizes images to produce thumbnails, with options to set the desired x or y
109             dimensions. Uses the GD image library for those who are already using something more advanced than Imager.
110              
111             =head2 Options
112              
113             The view is controlled by setting the following values in the stash:
114              
115             =over
116              
117             =item image
118              
119             Contains the file path for the full-size source image.
120              
121             This is a mandatory option.
122              
123             =item x
124              
125             The width (in pixels) of the thumbnail.
126              
127             This is optional, but at least one of the C<x> or C<y> parameters must be set.
128              
129             =item y
130              
131             The height (in pixels) of the thumbnail.
132              
133             This is optional, but at least one of the C<x> or C<y> parameters must be set.
134              
135             =back
136              
137             =head2 Image formats
138              
139             The generated thumbnails will always be produced in the same format (PNG, JPG, etc)
140             as the source image.
141              
142             Catalyst::View::GD::Thumbnail uses the L<Image::Resize> module to crop and resize images,
143             so it accept any image format supported by I<Image::Resize>: bmp, gif, jpeg, png, xbm, xpm.
144              
145             Please see the L<Image::Resize> documentation for more details and installation notes.
146              
147             =head1 BUGS
148              
149             Please report any bugs or feature requests to C<bug-catalyst-view-thumbnail at rt.cpan.org>, or through
150             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-GD-Thumbnail>. I will be notified, and then you'll
151             automatically be notified of progress on your bug as I make changes.
152              
153             =head1 AUTHOR
154              
155             Nick Logan (ugexe) <F<ug@skunkds.com>>
156              
157             =head1 LICENSE AND COPYRIGHT
158              
159             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.