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