| 1 | package Image::Imager::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 | 1 | 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 | ▶ | use Imager; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 1 | my %fields = | |||||||||||||||||||
| 41 | ( | ||||||||||||||||||||
| 42 | file_src => '', | ||||||||||||||||||||
| 43 | file_dst => '', | ||||||||||||||||||||
| 44 | height => 0, | ||||||||||||||||||||
| 45 | width => 0, | ||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||
| 50 | 1 | my ($proto,%options) = @_; | |||||||||||||||||||
| 51 | 1 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 52 | 1 | my $self = {%fields}; | |||||||||||||||||||
| 53 | 1 | bless $self, $class; | |||||||||||||||||||
| 54 | 1 | ▶ | while (my ($key,$value) = each(%options)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 4 | ▶ | if (exists($fields{$key})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 4 | $self->{$key} = $value; | |||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||
| 58 | 0 | die __PACKAGE__ . "::new: invalid option '$key'\n"; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 1 | return $self; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 1 | ▶ | sub save { | ||||||||||||||||||
|
Subroutine
save: called CC=8 SCAR=31.5
Pod
undocumented
| |||||||||||||||||||||
| 65 | 1 | my $self = shift; | |||||||||||||||||||
| 66 | 1 | ▶ | my $type = shift || 'jpeg'; | ||||||||||||||||||
|
Condition50%1/2
shift() || 'jpeg'
MC/DC0%0/1
shift() || 'jpeg'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 67 | 1 | my $ret; | |||||||||||||||||||
| 68 | 1 | my $height_d = $self->{height}; | |||||||||||||||||||
| 69 | 1 | my $width_d = $self->{width}; | |||||||||||||||||||
| 70 | 1 | my $srcImage = Imager->new(); | |||||||||||||||||||
| 71 | 1 | ▶ | unless ($srcImage->open(file=>$self->{file_src})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 1 | my $errImage = Imager->new(xsize=>600, ysize=>15, | |||||||||||||||||||
| 73 | channels=>3, bits=>16); | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | #$white = $errImage->colorAllocate(255,255,0); | ||||||||||||||||||||
| 76 | 1 | 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 | $errImage->string(text=>"Ciao", x=>0,y=>0,size=>10,color=>$red); | |||||||||||||||||||
| 80 | 1 | ▶ | $errImage->write(data => \$ret, type => $type) or die $errImage->errstr; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | return $ret; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | my ($width_s,$height_s) = ($srcImage->getwidth,$srcImage->getheight); | |||||||||||||||||||
| 85 | 0 | ▶ | if ($height_d == 0) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 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 | ▶ | if ($type eq 'gif') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 0 | $opts{interlace} = 1; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | 0 | ▶ | $dstImage->write(file => $self->{file_dst}, type => $type,%opts) or die $dstImage->errstr; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | # salvo in cache; | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | sub width { | ||||||||||||||||||
|
Subroutine
width: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 102 | 0 | my $self = shift; | |||||||||||||||||||
| 103 | 0 | ▶ | return @_ ? $self->{width} = shift : $self->{width}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | ▶ | sub height { | ||||||||||||||||||
|
Subroutine
height: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||
| 108 | 0 | ▶ | return @_ ? $self->{height} = shift : $self->{height}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | ▶ | sub file_src { | ||||||||||||||||||
|
Subroutine
file_src: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||
| 113 | 0 | ▶ | return @_ ? $self->{file_src} = shift : $self->{file_src}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | 0 | ▶ | sub file_dst { | ||||||||||||||||||
|
Subroutine
file_dst: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||||||||||
| 117 | 0 | ▶ | return @_ ? $self->{file_dst} = shift : $self->{file_dst}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | 1 | 1; | |||||||||||||||||||