File Coverage

blib/lib/Image/GD/Thumbnail.pm
Criterion Covered Total %
statement 9 21 42.8
branch 0 10 0.0
condition n/a
subroutine 3 4 75.0
pod 0 1 0.0
total 12 36 33.3


line stmt bran cond sub pod time code
1             package Image::GD::Thumbnail;
2            
3             our $VERSION = '0.041';
4 1     1   11371 use Carp;
  1         3  
  1         84  
5 1     1   6 use strict;
  1         2  
  1         38  
6 1     1   6 use warnings;
  1         8  
  1         349  
7            
8             =head1 NAME
9            
10             Image::GD::Thumbnail - produce thumbnail images with GD
11            
12             =head1 SYNOPSIS
13            
14             use GD;
15             use Image::GD::Thumbnail;
16            
17             # Load your source image
18             open IN, 'E:/Images/test.jpg' or die "Could not open.";
19             my $srcImage = GD::Image->newFromJpeg(*IN);
20             close IN;
21            
22             # Create the thumbnail from it, where the biggest side is 50 px
23             my ($thumb,$x,$y) = Image::GD::Thumbnail::create($srcImage,50);
24            
25            
26             # Save your thumbnail
27             open OUT, ">E:/Images/thumb_test.jpg" or die "Could not save ";
28             binmode OUT;
29             print OUT $thumb->jpeg;
30             close OUT;
31            
32             __END__
33            
34             =head1 DESCRIPTION
35            
36             This module uses the GD library to create a thumbnail image with no side bigger than you specify.
37            
38             The subroutine C takes two arguments: the first is a GD image object,
39             the second is the size, in pixels, you wish the image's longest side to be.
40             It returns a new GD image object (the thumbnail), as well as the I and I
41             dimensions, as (integer) scalars.
42            
43             =head1 PREREQUISITES
44            
45             GD
46            
47             =cut
48            
49 0     0 0   sub create { my ($orig,$max) = (shift,shift);
50 0 0         confess "No image supplied" unless $orig;
51 0 0         confess "No scale factor or geometry" unless $max;
52            
53 0           my ($ox,$oy) = $orig->getBounds();
54 0           my ($maxx, $maxy);
55 0 0         if (($maxx, $maxy) = $max =~ /^(\d+)x(\d+)$/i){
56 0 0         $max = ($ox>$oy)? $maxx : $maxy;
57             } else {
58 0           $maxx = $maxy = $max;
59             }
60            
61             # my $r = ($ox>$oy) ? ($ox/$maxx) : ($oy/$maxy);
62 0 0         my $r = ($ox/$maxx) > ($oy/$maxy) ? ($ox/$maxx) : ($oy/$maxy);
63            
64 0           my $thumb = GD::Image->new($ox/$r,$oy/$r);
65 0           $thumb->copyResized($orig,0,0,0,0,$ox/$r,$oy/$r,$ox,$oy);
66 0           return $thumb, sprintf("%.0f",$ox/$r), sprintf("%.0f",$oy/$r);
67             }
68            
69             1;
70            
71             __END__