line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Thumbnail; |
2
|
1
|
|
|
1
|
|
1026
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
202
|
use Imager; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has default_args => sub{+{ |
9
|
|
|
|
|
|
|
src => '', |
10
|
|
|
|
|
|
|
dst => '', |
11
|
|
|
|
|
|
|
width => 0, |
12
|
|
|
|
|
|
|
height => 0, |
13
|
|
|
|
|
|
|
}}; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub register { |
16
|
1
|
|
|
1
|
1
|
35
|
my ($self, $app) = @_; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$app->helper(thumbnail => sub { |
19
|
4
|
|
|
4
|
|
330368
|
my $app = shift; |
20
|
4
|
50
|
|
|
|
44
|
my $conf = @_ ? { @_ } : return; |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
|
|
10
|
$conf = {%{$self->default_args}, %$conf}; |
|
4
|
|
|
|
|
104
|
|
23
|
|
|
|
|
|
|
|
24
|
4
|
|
|
|
|
90
|
my $src_image = Imager->new(); |
25
|
4
|
|
|
|
|
94
|
my $dst_image; |
26
|
|
|
|
|
|
|
|
27
|
4
|
50
|
|
|
|
30
|
die 'Source file missing' unless $src_image->open(file => $conf->{src}); |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
my ($src_width, $src_height) = ($src_image->getwidth, $src_image->getheight); |
30
|
0
|
|
|
|
|
|
my ($dst_width, $dst_height) = (int $conf->{width}, int $conf->{height} ); |
31
|
|
|
|
|
|
|
|
32
|
0
|
0
|
0
|
|
|
|
if (!$dst_height || ($dst_width && $src_width < $src_height)) { |
|
|
|
0
|
|
|
|
|
33
|
0
|
|
|
|
|
|
$dst_height = $src_height * $dst_width /$src_width; |
34
|
|
|
|
|
|
|
} else { |
35
|
0
|
|
|
|
|
|
$dst_width = $src_width * $dst_height/$src_height; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
$dst_image = $src_image->scaleX(pixels => $dst_width)->scaleY(pixels => $dst_height); |
39
|
|
|
|
|
|
|
|
40
|
0
|
0
|
0
|
|
|
|
if ($conf->{width} && $conf->{height}) { |
41
|
0
|
|
|
|
|
|
$dst_image = $dst_image->crop(width => $conf->{width}, height => $conf->{height}); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
|
unless ($conf->{dst}) { |
45
|
0
|
|
|
|
|
|
$conf->{dst} = $conf->{src}; |
46
|
0
|
|
|
|
|
|
$conf->{dst} =~ s{(.*?)\.(.+)$}{${1}_thumb.$2}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
$dst_image->write(file => $conf->{dst}) or die $dst_image->errstr; |
50
|
1
|
|
|
|
|
12
|
}); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
1; |
54
|
|
|
|
|
|
|
__END__ |