blib/lib/Blio/Image.pm

filter: Statementstmt 93.4% 43 / 46 Branchbran 33.3% 2 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 79.4% 58 / 73 CC 6 SCAR 14.3
CC
6
Cov
79%
make_thumbnail
10.7
publish
2.0
BEGIN
1.0
CRAP 4.2
« Build ↑ Summary blib/lib/Blio.pm »
1 package Blio::Image;
2  
3 # ABSTRACT: An image node
4  
5 16 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 has 'base_dir' => ( is => 'ro', isa => 'Path::Class::Dir', required => 1 );
14 16 has 'source_file' =>
15 ( is => 'ro', isa => 'Path::Class::File', required => 1 );
16 16 has 'url' =>
17 ( is => 'ro', isa => 'Path::Class::File', required => 1, lazy_build=>1 );
18 sub _build_url {
19 6 ▶ my $self = shift;
Subroutine _build_url: called CC=1 SCAR=0.0
20 6 return $self->source_file->relative($self->base_dir);
21 }
22 16 has 'thumbnail' =>
23 ( is => 'ro', isa => 'Path::Class::File', required => 1, lazy_build=>1);
24 sub _build_thumbnail {
25 3 ▶ my $self = shift;
Subroutine _build_thumbnail: called CC=1 SCAR=0.0
26 3 my $th = $self->source_file->relative($self->base_dir)->stringify;
27 3 $th=~s{/([^/]+)$}{/th_$1};
28 3 return file($th);
29 }
30  
31 sub publish {
32 1 ▶ my ($self, $blio) = @_;
Subroutine publish: called CC=2 SCAR=7.0
Pod documented
33  
34 1 $blio->output_dir->file($self->url)->parent->mkpath;
35 1 my $from = $self->source_file->stringify;
36 1 my $to = $blio->output_dir->file($self->url)->stringify;
37 1 ▶ copy($from, $to) || die "Cannot copy $from to $to: $!";
Branch: 1 branch
BranchTrueFalseTotal
copy($from, $to) or die "Cannot copy $from to ${to}: $!" 0 1 1
38 }
39  
40 sub make_thumbnail {
41 1 ▶ my ($self, $blio, $width) = @_;
Subroutine make_thumbnail: called CC=5 SCAR=23.7
Pod documented
42  
43 1 $blio->output_dir->file($self->url)->parent->mkpath;
44 1 my $file = $self->source_file->stringify;
45 1 my $target = $blio->output_dir->file($self->thumbnail)->stringify;
46 1 my $image = Imager->new;
47 1 ▶ $image->read(file=>$file) || die "Cannot read image $file: ".$image->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$image->read("file", $file) or die "Cannot read image ${file}: " . $image->errstr 1 0 1
48 0 ▶ $width ||= $blio->thumbnail;
Condition0%0/3
$width ||= $blio->thumbnail
l!l&&r!l&&!r
000
MC/DC0%0/2
$width ||= $blio->thumbnail
$width $blio->thumbnail
Truth table0%0/3
A ||= B
A= $widthB= $blio->thumbnail
ABresult
1X1
011
000
49 0 ▶ my $thumbnail = $image->scale(xpixels => $width) || die "Cannot scale $file: ".$image->errstr;
Condition0%0/2
$image->scale('xpixels', $width) || die("Cannot scale ${file}: " . $image->errstr)
l!l
00
MC/DC0%0/1
$image->scale('xpixels', $width) || die("Cannot scale ${file}: " . $image->errstr)
$image->scale('xpixels', $width)
Truth table0%0/2
A
A= $image->scale('xpixels', $width)
Aresult
11
00
50 0 ▶ $thumbnail->write( file => $target ) || die "Cannot write thumbnail $target" . $thumbnail->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$thumbnail->write("file", $target) or die "Cannot write thumbnail $target" . $thumbnail->errstr 0 0 0
51 }
52  
53 16 __PACKAGE__->meta->make_immutable;
54 16 1;
55  
56 __END__
57  
58 =pod
59  
60 =encoding UTF-8
61  
62 =head1 NAME
63  
64 Blio::Image - An image node
65  
66 =head1 VERSION
67  
68 version 2.003
69  
70 =head1 SYNOPSIS
71  
72 my $image_node = Blio::Image->new(
73 base_dir => $blio->source_dir,
74 source_file => 'relative/path/to/image.jpg',
75 );
76  
77 $image_node->make_thumbnail( $blio, 450 );
78 $image_node->publish( $blio );
79  
80 =head1 DESCRIPTION
81  
82 You probably won't need to use C<Blio::Image> directly.
83  
84 =head1 METHODS
85  
86 =head2 publish
87  
88 $image_node->publish( $blio );
89  
90 Write the image file into the L<output_dir>. Create all directories that are needed.
91  
92 =head2 make_thumbnail
93  
94 $image_node->make_thumbnail( $blio, $width );
95  
96 Generate a thumbnail image and store it in L<output_dir>.
97  
98 If C<$width> is not passed in, the default width from C<$blio> is used.
99  
100 =attribute base_dir
101  
102 <Path::Class::Dir> object pointing to the L<source_dir> of this C<Blio> instance.
103  
104 =attribute source_file
105  
106 <Path::Class::File> object pointing to the source image file. The file format has to be supported by C<Imager>. I would strongly suggest using jpeg or png.
107  
108 =attribute url
109  
110 Relative <Path::Class::File> object pointing to the non-scaled image. This can be used in templates to link to the img.
111  
112 =attribute thumbnail
113  
114 Relative <Path::Class::File> object pointing to the thumbnailed image. This can be used in templates to link to the thumbnail.
115  
116 =head1 AUTHOR
117  
118 Thomas Klausner <domm@cpan.org>
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 This software is copyright (c) 2013 by Thomas Klausner.
123  
124 This is free software; you can redistribute it and/or modify it under
125 the same terms as the Perl 5 programming language system itself.
126  
127 =cut
« Build ↑ Summary blib/lib/Blio.pm »