| 1 | package Mojolicious::Plugin::Thumbnail; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Imager; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | has default_args => sub{+{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | src => '', | |||||||||||||||||||||||||||||||||||||||||
| 10 | dst => '', | ||||||||||||||||||||||||||||||||||||||||||
| 11 | width => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 12 | height => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | }}; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=3.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | my ($self, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | $app->helper(thumbnail => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=40.4
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | my $app = shift; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | my $conf = @_ ? { @_ } : return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | $conf = {%{$self->default_args}, %$conf}; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | my $src_image = Imager->new(); | |||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | my $dst_image; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | die 'Source file missing' unless $src_image->open(file => $conf->{src}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | if (!$dst_height || ($dst_width && $src_width < $src_height)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $dst_height or $dst_width and $src_width < $src_height
$dst_width and $src_width < $src_height
MC/DC0%0/3
not $dst_height or $dst_width and $src_width < $src_height
not $dst_height
$dst_width
$src_width < $src_height
Truth table0%0/4
not(A) or B and C
A= not $dst_heightB= $dst_widthC= $src_width < $src_height
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | if ($conf->{width} && $conf->{height}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$conf->{"width"} and $conf->{"height"}
MC/DC0%0/2
$conf->{"width"} and $conf->{"height"}
$conf->{"width"}
$conf->{"height"}
Truth table0%0/3
A and B
A= $conf->{"width"}B= $conf->{"height"}
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | $dst_image = $dst_image->crop(width => $conf->{width}, height => $conf->{height}); | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | unless ($conf->{dst}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $conf->{dst} = $conf->{src}; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $conf->{dst} =~ s{(.*?)\.(.+)$}{${1}_thumb.$2}; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $dst_image->write(file => $conf->{dst}) or die $dst_image->errstr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 54 | __END__ | ||||||||||||||||||||||||||||||||||||||||||