| 1 | package Image::Magick::Thumbnail; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | our $VERSION = '0.06'; | ||
| 6 | |||
| 7 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Image::Magick::Thumbnail - Produces thumbnail images with ImageMagick | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Image::Magick::Thumbnail 0.06; | ||
| 16 | # Load the source image | ||
| 17 | my $src = Image::Magick->new; | ||
| 18 | $src->Read('source.jpg'); | ||
| 19 | |||
| 20 | # Create the thumbnail from it, where the biggest side is 50 px | ||
| 21 | my ($thumb, $x, $y) = Image::Magick::Thumbnail::create($src, 50); | ||
| 22 | |||
| 23 | # Save your thumbnail | ||
| 24 | $thumb->Write('source_thumb.jpg'); | ||
| 25 | |||
| 26 | # Create another thumb, that fits into the geometry | ||
| 27 | my ($thumb2, $x2, $y2) = Image::Magick::Thumbnail::create($src, '60x50'); | ||
| 28 | |||
| 29 | # Create yet another thumb, fitting partial geometry | ||
| 30 | my ($thumb3, $x3, $y3) = Image::Magick::Thumbnail::create($src, 'x50'); | ||
| 31 | |||
| 32 | __END__ | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This module uses the ImageMagick library to create a thumbnail image with no side bigger than you specify. | ||
| 37 | |||
| 38 | There is no OO API, since that would seem to be over-kill. There's just C<create>. | ||
| 39 | |||
| 40 | =head2 SUBROUTINE create | ||
| 41 | |||
| 42 | my ($im_obj, $x, $y) = Image::Magick::Thumbnail->create( $src, $maxsize_or_geometry); | ||
| 43 | |||
| 44 | It takes two arguments: the first is an ImageMagick image object, | ||
| 45 | the second is either the size in pixels you wish the longest side of the image to be, | ||
| 46 | or an C<Image::Magick>-style 'geometry' (eg C<100x120>) which the thumbnail must fit. | ||
| 47 | Missing part of the geometry is fine. | ||
| 48 | |||
| 49 | Returns an C<Imaeg::Magick> image object (the thumbnail), as well as the | ||
| 50 | number of pixels of the I<width> and I<height> of the image, as integer scalars, | ||
| 51 | and (mainly for testing) the ration used in the scaling. | ||
| 52 | |||
| 53 | =head2 WARNINGS | ||
| 54 | |||
| 55 | Will warn on bad or missing arguments if you have C<use>d C<warnings>. | ||
| 56 | |||
| 57 | =head2 PREREQUISITES | ||
| 58 | |||
| 59 | Image::Magick | ||
| 60 | |||
| 61 | =head2 EXPORTS | ||
| 62 | |||
| 63 | None by default. | ||
| 64 | |||
| 65 | =head1 SEE ALSO | ||
| 66 | |||
| 67 | L<perl>, L<Image::Magick>, L<Image::GD::Thumbnail>, | ||
| 68 | and L<Image::Thumbnail> for the same formula for various engines. | ||
| 69 | |||
| 70 | =head1 AUTHOR | ||
| 71 | |||
| 72 | Lee Goddard <LGoddard@CPAN.org> | ||
| 73 | |||
| 74 | =head2 COPYRIGT | ||
| 75 | |||
| 76 | Copyright (C) Lee Godadrd 2001-2008. all rights reserved. | ||
| 77 | Available under the same terms as Perl itself. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | 0 | ▶ | use Image::Magick; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 82 | use Carp; | ||
| 83 | #use warnings::register; | ||
| 84 | |||
| 85 | sub create($$;$) { | ||
| 86 | my ($img, $max) = (shift, shift); | ||
| 87 | |||
| 88 | if (not $img){ | ||
| 89 | if (warnings::enabled()) { | ||
| 90 | Carp::cluck "No image"; | ||
| 91 | } | ||
| 92 | return undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | if (not ref $img or ref $img ne 'Image::Magick'){ | ||
| 96 | if (warnings::enabled()) { | ||
| 97 | Carp::cluck "Not an Image::Magick object"; | ||
| 98 | } | ||
| 99 | return undef; | ||
| 100 | } | ||
| 101 | |||
| 102 | if (not $max){ | ||
| 103 | if (warnings::enabled()) { | ||
| 104 | Carp::cluck "No size or geometry"; | ||
| 105 | } | ||
| 106 | return undef; | ||
| 107 | } | ||
| 108 | |||
| 109 | my ($ox, $oy) = $img->Get('width', 'height'); | ||
| 110 | if (not $ox and not $oy){ | ||
| 111 | if (warnings::enabled()) { | ||
| 112 | Carp::cluck "Could not get image size"; | ||
| 113 | } | ||
| 114 | return undef; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Version 0.05 behaviour | ||
| 118 | # From geo, get the longest side of the box into which to fit: | ||
| 119 | # my ($maxx, $maxy); | ||
| 120 | # if (($maxx, $maxy) = $max =~ /^(\d+)x(\d+)$/i){ | ||
| 121 | # $max = ($ox>$oy)? $maxx : $maxy; | ||
| 122 | # } else { | ||
| 123 | # $maxx = $maxy = $max; | ||
| 124 | # } | ||
| 125 | # $r = ($ox/$maxx) > ($oy/$maxy) ? ($ox/$maxx) : ($oy/$maxy); | ||
| 126 | |||
| 127 | # foreach my $max (qw( 10x40 10x x40 40)){ | ||
| 128 | |||
| 129 | my $r; | ||
| 130 | if ($max =~ /^\s*(\d+)?\s*(x)?\s*(\d+)?\s*$/i){ | ||
| 131 | # warn sprintf( "%s %s %s", ($1||"?"), ($2||"?"), ($3||"?") ); | ||
| 132 | if ($1 and $3){ | ||
| 133 | # warn sprintf "Got both: %s %s", ($ox/$1), ($oy/$3); | ||
| 134 | $r = ($ox/$1) > ($oy/$3) ? ($ox/$1) : ($oy/$3); | ||
| 135 | } | ||
| 136 | elsif (not $1 or not $3){ | ||
| 137 | if (not $2){ | ||
| 138 | # warn "Got one ($max)"; | ||
| 139 | $r = ($ox/$max) > ($oy/$max) ? ($ox/$max) : ($oy/$max); | ||
| 140 | } else { | ||
| 141 | # warn "Got one or other"; | ||
| 142 | $r = ($1) ? ($ox/$1) : ($oy/$3); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | # warn $r==10; | ||
| 146 | } | ||
| 147 | |||
| 148 | else { | ||
| 149 | if (warnings::enabled()) { | ||
| 150 | warn __PACKAGE__."::create expected a second argument of a single positive integer, a valid geometry, or a one-side geometry: please see the POD."; | ||
| 151 | } | ||
| 152 | return undef; | ||
| 153 | } | ||
| 154 | |||
| 155 | my ($x, $y) = (int($ox/$r), int($oy/$r)); | ||
| 156 | |||
| 157 | $img->Thumbnail( | ||
| 158 | width => $x, | ||
| 159 | height => $y | ||
| 160 | ); | ||
| 161 | |||
| 162 | return ($img, $x, $y, $r); | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | 1; | ||
| 167 | |||
| 168 | __END__ | ||