File Coverage

blib/lib/Catalyst/View/GD.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Catalyst::View::GD;
2              
3 2     2   41275 use strict;
  2         6  
  2         90  
4 2     2   13 use warnings;
  2         5  
  2         62  
5              
6 2     2   8279 use NEXT;
  2         24134  
  2         85  
7 2     2   1391 use GD;
  0            
  0            
8             use Scalar::Util 'blessed';
9              
10             use Catalyst::Exception;
11              
12             our $VERSION = '0.01';
13             our $AUTHORITY = 'cpan:STEVAN';
14              
15             use base 'Catalyst::View';
16              
17             __PACKAGE__->mk_accessors(qw[
18             gd_image_type
19             gd_image_content_type
20             gd_image_render_args
21             ]);
22              
23             sub new {
24             my($class, $c, $args) = @_;
25             my $self = $class->NEXT::new($c, $args);
26            
27             my $config = $c->config->{'View::GD'};
28            
29             $args->{gd_image_type} ||= $config->{gd_image_type} || 'gif';
30             $args->{gd_image_content_type} ||= $config->{gd_image_content_type} || ('image/' . $args->{gd_image_type});
31             $args->{gd_image_render_args} ||= $config->{gd_image_render_args} || [];
32            
33             $self->gd_image_type($args->{gd_image_type});
34             $self->gd_image_content_type($args->{gd_image_content_type});
35             $self->gd_image_render_args($args->{gd_image_render_args});
36              
37             return $self;
38             }
39              
40             sub process {
41             my $self = shift;
42             my $c = shift;
43             my @args = @_;
44            
45             my $gd_image_type = $c->stash->{gd_image_type} || $self->gd_image_type;
46             my $gd_image_content_type = $c->stash->{gd_image_content_type} || $self->gd_image_content_type;
47             my $gd_image_render_args = $c->stash->{gd_image_render_args} || $self->gd_image_render_args;
48              
49             my $gd_image = $c->stash->{gd_image};
50              
51             (defined $gd_image)
52             || die "No image to render";
53              
54             (blessed $gd_image && $gd_image->isa('GD::Image'))
55             || die "Bad image ($gd_image), must be an instance of GD::Image";
56            
57             my $render_method = $gd_image->can($gd_image_type);
58            
59             (defined $render_method)
60             || die "Cannot render '$gd_image_type' for '$gd_image' : no '$gd_image_type' available";
61            
62             my $img = eval {
63             $gd_image->$render_method(@{$self->gd_image_render_args})
64             };
65             if ($@) {
66             die "Failed to render '$gd_image' as '$gd_image_type' because: $@";
67             }
68            
69             $c->response->content_type($gd_image_content_type);
70             $c->response->body($img);
71             }
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =head1 NAME
80              
81             Catalyst::View::GD - A Catalyst View for GD images
82              
83             =head1 SYNOPSIS
84              
85             # lib/MyApp/View/GD.pm
86             package MyApp::View::GD;
87             use base 'Catalyst::View::GD';
88             1;
89            
90             # configure in lib/MyApp.pm
91             MyApp->config({
92             ...
93             'View::GD' => {
94             gd_image_type => 'png', # defaults to 'gif'
95             gd_image_content_type => 'images/png', # defaults to 'image/$gd_image_type'
96             gd_image_render_args => [ 5 ], # defaults to []
97             },
98             });
99            
100             sub foo : Local {
101             my($self, $c) = @_;
102             $c->stash->{gd_image} = $self->create_foo_image();
103             $c->forward('MyApp::View::GD');
104             }
105              
106             =head1 DESCRIPTION
107              
108             This is a Catalyst View subclass which can handle rendering GD based
109             image content.
110              
111             =head1 CONFIG OPTIONS
112              
113             =over 4
114              
115             =item I<gd_image_type>
116              
117             This defaults to C<gif> but should be the name of the method to call on the
118             GD::Image instance in order to render the images.
119              
120             =item I<gd_image_render_args>
121              
122             This is an array ref of values to be passed as an argument to the GD::Image
123             render method.
124              
125             =item I<gd_image_content_type>
126              
127             The default for this is built from the C<gd_image_type> parameter, which in
128             most cases will just work, but in some more specific rendering methods in
129             GD::Image it will not and you will need to assign this explicitly.
130              
131             =back
132              
133             =head1 METHODS
134              
135             =over 4
136              
137             =item B<new>
138              
139             This really just handles consuming the configuration parameters.
140              
141             =item B<process>
142              
143             This method will always look in the C<gd_image> stash for an instance of
144             GD::Image and it will then render and serve it according to the
145             configuration setup.
146              
147             It is also possible to override the global configuration on a per-request
148             basis by assigning values in the stash using the same keys as used in
149             the configuration.
150              
151             =back
152              
153             =head1 BUGS
154              
155             All complex software has bugs lurking in it, and this module is no
156             exception. If you find a bug please either email me, or add the bug
157             to cpan-RT.
158              
159             =head1 AUTHOR
160              
161             Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             Copyright 2007 by Infinity Interactive, Inc.
166              
167             L<http://www.iinteractive.com>
168              
169             This library is free software; you can redistribute it and/or modify
170             it under the same terms as Perl itself.
171              
172             =cut