| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Image::Imlib2::Thumbnail; |
|
2
|
1
|
|
|
1
|
|
693
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
36
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
34
|
|
|
4
|
1
|
|
|
1
|
|
6
|
use File::Basename qw(fileparse); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
78
|
|
|
5
|
1
|
|
|
1
|
|
401
|
use Image::Imlib2; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use MIME::Types; |
|
7
|
|
|
|
|
|
|
use Path::Class; |
|
8
|
|
|
|
|
|
|
use base qw(Class::Accessor::Fast); |
|
9
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(sizes)); |
|
10
|
|
|
|
|
|
|
our $VERSION = '0.40'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
|
13
|
|
|
|
|
|
|
my $class = shift; |
|
14
|
|
|
|
|
|
|
my $self = $class->SUPER::new(@_); |
|
15
|
|
|
|
|
|
|
$self->sizes( |
|
16
|
|
|
|
|
|
|
[ { type => 'landscape', |
|
17
|
|
|
|
|
|
|
name => 'square', |
|
18
|
|
|
|
|
|
|
width => 75, |
|
19
|
|
|
|
|
|
|
height => 75 |
|
20
|
|
|
|
|
|
|
}, |
|
21
|
|
|
|
|
|
|
{ type => 'landscape', |
|
22
|
|
|
|
|
|
|
name => 'thumbnail', |
|
23
|
|
|
|
|
|
|
width => 100, |
|
24
|
|
|
|
|
|
|
height => 75 |
|
25
|
|
|
|
|
|
|
}, |
|
26
|
|
|
|
|
|
|
{ type => 'landscape', |
|
27
|
|
|
|
|
|
|
name => 'small', |
|
28
|
|
|
|
|
|
|
width => 240, |
|
29
|
|
|
|
|
|
|
height => 180 |
|
30
|
|
|
|
|
|
|
}, |
|
31
|
|
|
|
|
|
|
{ type => 'landscape', |
|
32
|
|
|
|
|
|
|
name => 'medium', |
|
33
|
|
|
|
|
|
|
width => 500, |
|
34
|
|
|
|
|
|
|
height => 375 |
|
35
|
|
|
|
|
|
|
}, |
|
36
|
|
|
|
|
|
|
{ type => 'landscape', |
|
37
|
|
|
|
|
|
|
name => 'large', |
|
38
|
|
|
|
|
|
|
width => 1024, |
|
39
|
|
|
|
|
|
|
height => 768 |
|
40
|
|
|
|
|
|
|
}, |
|
41
|
|
|
|
|
|
|
{ type => 'portrait', |
|
42
|
|
|
|
|
|
|
name => 'square', |
|
43
|
|
|
|
|
|
|
width => 75, |
|
44
|
|
|
|
|
|
|
height => 75 |
|
45
|
|
|
|
|
|
|
}, |
|
46
|
|
|
|
|
|
|
{ type => 'portrait', |
|
47
|
|
|
|
|
|
|
name => 'thumbnail', |
|
48
|
|
|
|
|
|
|
width => 75, |
|
49
|
|
|
|
|
|
|
height => 100 |
|
50
|
|
|
|
|
|
|
}, |
|
51
|
|
|
|
|
|
|
{ type => 'portrait', |
|
52
|
|
|
|
|
|
|
name => 'small', |
|
53
|
|
|
|
|
|
|
width => 180, |
|
54
|
|
|
|
|
|
|
height => 240 |
|
55
|
|
|
|
|
|
|
}, |
|
56
|
|
|
|
|
|
|
{ type => 'portrait', |
|
57
|
|
|
|
|
|
|
name => 'medium', |
|
58
|
|
|
|
|
|
|
width => 375, |
|
59
|
|
|
|
|
|
|
height => 500 |
|
60
|
|
|
|
|
|
|
}, |
|
61
|
|
|
|
|
|
|
{ type => 'portrait', |
|
62
|
|
|
|
|
|
|
name => 'large', |
|
63
|
|
|
|
|
|
|
width => 768, |
|
64
|
|
|
|
|
|
|
height => 1024 |
|
65
|
|
|
|
|
|
|
}, |
|
66
|
|
|
|
|
|
|
] |
|
67
|
|
|
|
|
|
|
) unless $self->sizes; |
|
68
|
|
|
|
|
|
|
return $self; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub add_size { |
|
72
|
|
|
|
|
|
|
my ( $self, $size ) = @_; |
|
73
|
|
|
|
|
|
|
push @{ $self->sizes }, $size; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub generate { |
|
77
|
|
|
|
|
|
|
my ( $self, $filename, $directory ) = @_; |
|
78
|
|
|
|
|
|
|
my $image = Image::Imlib2->load($filename); |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my ( $original_width, $original_height ) |
|
81
|
|
|
|
|
|
|
= ( $image->width, $image->height ); |
|
82
|
|
|
|
|
|
|
my $original_type |
|
83
|
|
|
|
|
|
|
= $original_width > $original_height ? 'landscape' : 'portrait'; |
|
84
|
|
|
|
|
|
|
my $original_extension = [ fileparse( $filename, qr/\.[^.]*?$/ ) ]->[2] |
|
85
|
|
|
|
|
|
|
|| '.jpg'; |
|
86
|
|
|
|
|
|
|
$original_extension =~ s/^\.//; |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my $mime_type = MIME::Types->new->mimeTypeOf($original_extension); |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my @thumbnails = ( |
|
91
|
|
|
|
|
|
|
{ filename => $filename, |
|
92
|
|
|
|
|
|
|
name => 'original', |
|
93
|
|
|
|
|
|
|
width => $original_width, |
|
94
|
|
|
|
|
|
|
height => $original_height, |
|
95
|
|
|
|
|
|
|
type => $original_type, |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
); |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
foreach my $size ( @{ $self->sizes } ) { |
|
100
|
|
|
|
|
|
|
my ( $name, $width, $height, $type ) |
|
101
|
|
|
|
|
|
|
= ( $size->{name}, $size->{width}, $size->{height}, |
|
102
|
|
|
|
|
|
|
$size->{type} ); |
|
103
|
|
|
|
|
|
|
next unless $type eq $original_type; |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# add quality from the size definition if provided |
|
106
|
|
|
|
|
|
|
my $quality = $size->{quality} || 75; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $scaled_image; |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
if ( $width && $height ) { |
|
111
|
|
|
|
|
|
|
my ( $new_width, $new_height ); |
|
112
|
|
|
|
|
|
|
my $aspect_ratio = $height / $width; |
|
113
|
|
|
|
|
|
|
$new_width = $original_width; |
|
114
|
|
|
|
|
|
|
$new_height = $original_width * $aspect_ratio; |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
if ( $new_height > $original_height ) { |
|
117
|
|
|
|
|
|
|
$new_width = $original_height / $aspect_ratio; |
|
118
|
|
|
|
|
|
|
$new_height = $original_height; |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
my $x = int( ( $original_width - $new_width ) / 2 ); |
|
121
|
|
|
|
|
|
|
my $y = int( ( $original_height - $new_height ) / 2 ); |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $cropped_image |
|
124
|
|
|
|
|
|
|
= $image->crop( $x, $y, $new_width, $new_height ); |
|
125
|
|
|
|
|
|
|
$scaled_image |
|
126
|
|
|
|
|
|
|
= $cropped_image->create_scaled_image( $width, $height ); |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} elsif ($width) { |
|
129
|
|
|
|
|
|
|
$scaled_image = $image->create_scaled_image( $width, 0 ); |
|
130
|
|
|
|
|
|
|
$height = $scaled_image->height; |
|
131
|
|
|
|
|
|
|
} else { |
|
132
|
|
|
|
|
|
|
$scaled_image = $image->create_scaled_image( 0, $height ); |
|
133
|
|
|
|
|
|
|
$width = $scaled_image->width; |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $destination |
|
137
|
|
|
|
|
|
|
= file( $directory, $name . '.' . $original_extension ) |
|
138
|
|
|
|
|
|
|
->stringify; |
|
139
|
|
|
|
|
|
|
$scaled_image->set_quality($quality); |
|
140
|
|
|
|
|
|
|
$scaled_image->save($destination); |
|
141
|
|
|
|
|
|
|
push @thumbnails, |
|
142
|
|
|
|
|
|
|
{ |
|
143
|
|
|
|
|
|
|
filename => $destination, |
|
144
|
|
|
|
|
|
|
name => $name, |
|
145
|
|
|
|
|
|
|
width => $width, |
|
146
|
|
|
|
|
|
|
height => $height, |
|
147
|
|
|
|
|
|
|
type => $type, |
|
148
|
|
|
|
|
|
|
mime_type => $mime_type, |
|
149
|
|
|
|
|
|
|
}; |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
return @thumbnails; |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
__END__ |