| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Image::Imager::Thumbnail; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 15063 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 48 |  | 
| 4 | 1 |  |  | 1 |  | 8 | use warnings; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 267 |  | 
| 5 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 NAME | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | Image::Imager::Thumbnail - Produces thumbnail images with Imager | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | use Image::Imager::Thumbnail; | 
| 14 |  |  |  |  |  |  | my $tb = new	Image::Imager::Thumbnail ( | 
| 15 |  |  |  |  |  |  | file_src  => $src, | 
| 16 |  |  |  |  |  |  | file_dst  => $dst, | 
| 17 |  |  |  |  |  |  | width     => $w, | 
| 18 |  |  |  |  |  |  | height    => $h | 
| 19 |  |  |  |  |  |  | ); | 
| 20 |  |  |  |  |  |  | $tb->save; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | __END__ | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | This module uses the Imager library to create a thumbnail image with no side bigger than you specify. | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =cut | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =head1 PREREQUISITES | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | Imager | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =cut | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 1 |  |  | 1 |  | 42511 | use Imager; | 
|  | 1 |  |  |  |  | 184425 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | my %fields = | 
| 41 |  |  |  |  |  |  | ( | 
| 42 |  |  |  |  |  |  | file_src               => '', | 
| 43 |  |  |  |  |  |  | file_dst               => '', | 
| 44 |  |  |  |  |  |  | height             => 0, | 
| 45 |  |  |  |  |  |  | width	             => 0, | 
| 46 |  |  |  |  |  |  | ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub new { | 
| 50 | 1 |  |  | 1 | 0 | 193 | my ($proto,%options) = @_; | 
| 51 | 1 |  | 33 |  |  | 11 | my $class = ref($proto) || $proto; | 
| 52 | 1 |  |  |  |  | 8 | my $self = {%fields}; | 
| 53 | 1 |  |  |  |  | 5 | bless $self, $class; | 
| 54 | 1 |  |  |  |  | 8 | while (my ($key,$value) = each(%options)) { | 
| 55 | 4 | 50 |  |  |  | 11 | if (exists($fields{$key})) { | 
| 56 | 4 |  |  |  |  | 20 | $self->{$key} = $value; | 
| 57 |  |  |  |  |  |  | } else { | 
| 58 | 0 |  |  |  |  | 0 | die __PACKAGE__ . "::new: invalid option '$key'\n"; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  | } | 
| 61 | 1 |  |  |  |  | 5 | return $self; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub save { | 
| 65 | 1 |  |  | 1 | 0 | 7 | my $self				= shift; | 
| 66 | 1 |  | 50 |  |  | 8 | my $type				= shift || 'jpeg'; | 
| 67 | 1 |  |  |  |  | 3 | my $ret; | 
| 68 | 1 |  |  |  |  | 3 | my $height_d 			= $self->{height}; | 
| 69 | 1 |  |  |  |  | 3 | my $width_d 			= $self->{width}; | 
| 70 | 1 |  |  |  |  | 12 | my $srcImage 				= Imager->new(); | 
| 71 | 1 | 50 |  |  |  | 34 | unless ($srcImage->open(file=>$self->{file_src})) { | 
| 72 | 1 |  |  |  |  | 15045 | my $errImage		= Imager->new(xsize=>600, ysize=>15, | 
| 73 |  |  |  |  |  |  | channels=>3, bits=>16); | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | #$white = $errImage->colorAllocate(255,255,0); | 
| 76 | 1 |  |  |  |  | 164 | my $red = Imager::Color->new( 255, 0, 0 ); | 
| 77 |  |  |  |  |  |  | #$red = $errImage->colorAllocate(255,0,0); | 
| 78 |  |  |  |  |  |  | #$errImage->string(gdSmallFont,0,0,"Unable to find " . $self->{file},$red); | 
| 79 | 1 |  |  |  |  | 387 | $errImage->string(text=>"Ciao", x=>0,y=>0,size=>10,color=>$red); | 
| 80 | 1 | 50 |  |  |  | 38 | $errImage->write(data => \$ret, type => $type) or die $errImage->errstr; | 
| 81 | 0 |  |  |  |  |  | return $ret; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  |  | my ($width_s,$height_s) = ($srcImage->getwidth,$srcImage->getheight); | 
| 85 | 0 | 0 |  |  |  |  | if ($height_d == 0) { | 
|  |  | 0 |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | my $ratio = $width_d/$width_s; | 
| 87 | 0 |  |  |  |  |  | $height_d = $height_s * $ratio; | 
| 88 |  |  |  |  |  |  | } elsif ($width_d == 0) { | 
| 89 | 0 |  |  |  |  |  | my $ratio = $height_d/$height_s; | 
| 90 | 0 |  |  |  |  |  | $width_d = $width_s * $ratio; | 
| 91 |  |  |  |  |  |  | } | 
| 92 | 0 |  |  |  |  |  | my $dstImage			= $srcImage->scaleX(pixels=>$width_d)->scaleY(pixels=>$height_d); | 
| 93 | 0 |  |  |  |  |  | my %opts; | 
| 94 | 0 | 0 |  |  |  |  | if ($type eq 'gif') { | 
| 95 | 0 |  |  |  |  |  | $opts{interlace} = 1; | 
| 96 |  |  |  |  |  |  | } | 
| 97 | 0 | 0 |  |  |  |  | $dstImage->write(file => $self->{file_dst}, type => $type,%opts) or die $dstImage->errstr; | 
| 98 |  |  |  |  |  |  | # salvo in cache; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub width { | 
| 102 | 0 |  |  | 0 | 0 |  | my $self 	= shift; | 
| 103 | 0 | 0 |  |  |  |  | return @_ ? $self->{width} = shift : $self->{width}; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | sub height { | 
| 107 | 0 |  |  | 0 | 0 |  | my $self 	= shift; | 
| 108 | 0 | 0 |  |  |  |  | return @_ ? $self->{height} = shift : $self->{height}; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | sub file_src { | 
| 112 | 0 |  |  | 0 | 0 |  | my $self 	= shift; | 
| 113 | 0 | 0 |  |  |  |  | return @_ ? $self->{file_src} = shift : $self->{file_src}; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  | sub file_dst { | 
| 116 | 0 |  |  | 0 | 0 |  | my $self 	= shift; | 
| 117 | 0 | 0 |  |  |  |  | return @_ ? $self->{file_dst} = shift : $self->{file_dst}; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  | 1; |