| 1 | package Catalyst::Plugin::Upload::Image::Magick::Thumbnail::Fixed; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Catalyst::Request::Upload; | ||
| 7 | use Catalyst::Utils; | ||
| 8 | use Catalyst::Exception; | ||
| 9 | |||
| 10 | use File::Temp; | ||
| 11 | use Image::Magick; | ||
| 12 | use Image::Magick::Thumbnail::Fixed; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Catalyst::Plugin::Upload::Image::Magick::Thumbnail::Fixed - Making thumbnail image is kept ratio in fixed size image. | ||
| 17 | |||
| 18 | =head1 VERSION | ||
| 19 | |||
| 20 | Version 0.04 | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | our $VERSION = '0.04'; | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | In your Catalyst project, | ||
| 29 | |||
| 30 | use Catalyst qw/Upload::Image::Magick::Thumbnail::Fixed/; | ||
| 31 | |||
| 32 | You can execute "thumbnail_fixed" method in Catalyst::Request::Upload object | ||
| 33 | |||
| 34 | sub resize_to: Local { | ||
| 35 | my ($self, $c) = @_; | ||
| 36 | |||
| 37 | my $upload = $c->request->upload('file_field'); | ||
| 38 | my $thumbnail = $upload->thumbnail_fixed({ | ||
| 39 | density => '60x70', | ||
| 40 | format => 'png', | ||
| 41 | quality => 100 | ||
| 42 | }); | ||
| 43 | |||
| 44 | # $thumbnail is Image::Magick object | ||
| 45 | my ($width, $height) = $thumbnail->Get('width', 'height'); | ||
| 46 | |||
| 47 | # ... | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | =head1 METHODS | ||
| 52 | |||
| 53 | =head2 thumbnail_fixed($args) | ||
| 54 | |||
| 55 | Create "fixed" size thumbnail image. | ||
| 56 | |||
| 57 | =over 2 | ||
| 58 | |||
| 59 | =item $args | ||
| 60 | |||
| 61 | $args is hash reference. | ||
| 62 | Please see below arguments detail. | ||
| 63 | |||
| 64 | =over 2 | ||
| 65 | |||
| 66 | =item density | ||
| 67 | |||
| 68 | String formatted width x height. | ||
| 69 | See below example format. | ||
| 70 | |||
| 71 | "80x60" | ||
| 72 | |||
| 73 | =item width | ||
| 74 | |||
| 75 | Number format of thumbnail width. | ||
| 76 | Priority of density option is higher than width option. | ||
| 77 | Default value is 60pixel. | ||
| 78 | |||
| 79 | =item height | ||
| 80 | |||
| 81 | Number format of thumbnail height. | ||
| 82 | Priority of density option is higher than height option. | ||
| 83 | Default value is 60pixel. | ||
| 84 | |||
| 85 | =item format | ||
| 86 | |||
| 87 | String of image format. | ||
| 88 | You can choose one of gif, jpg or png. | ||
| 89 | |||
| 90 | =item quality | ||
| 91 | |||
| 92 | Image quality option. highest value is 100. | ||
| 93 | minimam value is 0. default 70. | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | See also L<Image::Magick::Thumbnail>, L<Image::Magick::Thumbnail::Fixed> | ||
| 100 | |||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | { | ||
| 105 | |||
| 106 | package Catalyst::Request::Upload; | ||
| 107 | |||
| 108 | sub thumbnail_fixed { | ||
| 109 | my ( $self, $args ) = @_; | ||
| 110 | |||
| 111 | Catalyst::Exception->throw( | ||
| 112 | "Please require Catalyst::Plugin::Upload::Image::Magick") | ||
| 113 | unless ( $self->can("is_image") ); | ||
| 114 | Catalyst::Exception->throw( | ||
| 115 | "This file is not image : " . $self->filename ) | ||
| 116 | unless ( $self->is_image ); | ||
| 117 | |||
| 118 | unless ( $self->{_thumbnail_fixed} ) { | ||
| 119 | $self->{_thumbnail_fixed} = Image::Magick::Thumbnail::Fixed->new; | ||
| 120 | } | ||
| 121 | |||
| 122 | if ( exists $args->{density} && $args->{density} =~ m|\d+x\d+| ) { | ||
| 123 | ( $args->{width}, $args->{height} ) = | ||
| 124 | map { s/\s+//g; $_ } split( /x/, $args->{density} ); | ||
| 125 | delete $args->{density}; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | $args->{width} = 60 unless ( exists $args->{width} ); | ||
| 129 | $args->{height} = 60 unless ( exists $args->{height} ); | ||
| 130 | } | ||
| 131 | |||
| 132 | $args->{format} = "jpg" unless ( exists $args->{format} ); | ||
| 133 | $args->{input} = $self->tempname; | ||
| 134 | $args->{output} = File::Temp->new( | ||
| 135 | DIR => Catalyst::Utils::class2tempdir( | ||
| 136 | "Catalyst::Plugin::Upload::Image::Magick::Thumbnail::Fixed", 1 | ||
| 137 | ), | ||
| 138 | TEMPLATE => "thumbnail_XXXXXX", | ||
| 139 | EXT => $args->{format} | ||
| 140 | ); | ||
| 141 | |||
| 142 | eval { $self->{_thumbnail_fixed}->thumbnail(%$args); }; | ||
| 143 | if ($@) { | ||
| 144 | Catalyst::Exception->throw($@); | ||
| 145 | } | ||
| 146 | elsif ( !-e $args->{output} ) { | ||
| 147 | Catalyst::Exception->throw( | ||
| 148 | "Can't create thumbnail : " . $args->{output} ); | ||
| 149 | } | ||
| 150 | |||
| 151 | my $thumb = Image::Magick->new; | ||
| 152 | $thumb->Read( $args->{output} ); | ||
| 153 | |||
| 154 | ### for File::Temp's cleanup | ||
| 155 | $self->{_thumbnail_temp} = {} unless ($self->{_thumbnail_temp}); | ||
| 156 | $self->{_thumbnail_temp}->{ $thumb->Get('filename') } = $args->{output}; | ||
| 157 | |||
| 158 | |||
| 159 | return $thumb; | ||
| 160 | } | ||
| 161 | |||
| 162 | } | ||
| 163 | |||
| 164 | =head1 AUTHOR | ||
| 165 | |||
| 166 | Toru Yamaguchi, C<< <zigorou at cpan.org> >> | ||
| 167 | |||
| 168 | =head1 SEE ALSO | ||
| 169 | |||
| 170 | =head1 BUGS | ||
| 171 | |||
| 172 | Please report any bugs or feature requests to | ||
| 173 | C<bug-catalyst-plugin-upload-image-magick-thumbnail-fixed at rt.cpan.org>, or through the web interface at | ||
| 174 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-Upload-Image-Magick>. | ||
| 175 | I will be notified, and then you'll automatically be notified of progress on | ||
| 176 | your bug as I make changes. | ||
| 177 | |||
| 178 | =head1 SUPPORT | ||
| 179 | |||
| 180 | You can find documentation for this module with the perldoc command. | ||
| 181 | |||
| 182 | perldoc Catalyst::Plugin::Upload::Image::Magick | ||
| 183 | |||
| 184 | You can also look for information at: | ||
| 185 | |||
| 186 | =over 4 | ||
| 187 | |||
| 188 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 189 | |||
| 190 | L<http://annocpan.org/dist/Catalyst-Plugin-Upload-Image-Magick> | ||
| 191 | |||
| 192 | =item * CPAN Ratings | ||
| 193 | |||
| 194 | L<http://cpanratings.perl.org/d/Catalyst-Plugin-Upload-Image-Magick> | ||
| 195 | |||
| 196 | =item * RT: CPAN's request tracker | ||
| 197 | |||
| 198 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-Upload-Image-Magick> | ||
| 199 | |||
| 200 | =item * Search CPAN | ||
| 201 | |||
| 202 | L<http://search.cpan.org/dist/Catalyst-Plugin-Upload-Image-Magick> | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =head1 ACKNOWLEDGEMENTS | ||
| 207 | |||
| 208 | =head1 Current Maintainer | ||
| 209 | |||
| 210 | The current maintainer of this module is Adam Hopkins. Any questions or comments should be sent to him | ||
| 211 | at srchulo@cpan.org | ||
| 212 | |||
| 213 | =head1 COPYRIGHT & LICENSE | ||
| 214 | |||
| 215 | Copyright 2006 Toru Yamaguchi, all rights reserved. | ||
| 216 | |||
| 217 | This program is free software; you can redistribute it and/or modify it | ||
| 218 | under the same terms as Perl itself. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | 1; # End of Catalyst::Plugin::Upload::Image::Magick::Thumbnail::Fixed |