File Coverage

blib/lib/Blio/Image.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Blio::Image;
2              
3             # ABSTRACT: An image node
4              
5 4     4   411446 use 5.010;
  4         15  
  4         214  
6 4     4   4065 use Moose;
  0            
  0            
7             use namespace::autoclean;
8             use Digest::SHA1;
9             use Path::Class;
10             use File::Copy;
11             use Imager;
12              
13             has 'base_dir' => ( is => 'ro', isa => 'Path::Class::Dir', required => 1 );
14             has 'source_file' =>
15             ( is => 'ro', isa => 'Path::Class::File', required => 1 );
16             has 'url' =>
17             ( is => 'ro', isa => 'Path::Class::File', required => 1, lazy_build=>1 );
18             sub _build_url {
19             my $self = shift;
20             return $self->source_file->relative($self->base_dir);
21             }
22             has 'thumbnail' =>
23             ( is => 'ro', isa => 'Path::Class::File', required => 1, lazy_build=>1);
24             sub _build_thumbnail {
25             my $self = shift;
26             my $th = $self->source_file->relative($self->base_dir)->stringify;
27             $th=~s{/([^/]+)$}{/th_$1};
28             return file($th);
29             }
30              
31             sub publish {
32             my ($self, $blio) = @_;
33              
34             $blio->output_dir->file($self->url)->parent->mkpath;
35             my $from = $self->source_file->stringify;
36             my $to = $blio->output_dir->file($self->url)->stringify;
37             copy($from, $to) || die "Cannot copy $from to $to: $!";
38             }
39              
40             sub make_thumbnail {
41             my ($self, $blio, $width) = @_;
42              
43             $blio->output_dir->file($self->url)->parent->mkpath;
44             my $file = $self->source_file->stringify;
45             my $target = $blio->output_dir->file($self->thumbnail)->stringify;
46             my $image = Imager->new;
47             $image->read(file=>$file) || die "Cannot read image $file: ".$image->errstr;
48             $width ||= $blio->thumbnail;
49             my $thumbnail = $image->scale(xpixels => $width) || die "Cannot scale $file: ".$image->errstr;
50             $thumbnail->write( file => $target ) || die "Cannot write thumbnail $target" . $thumbnail->errstr;
51             }
52              
53             __PACKAGE__->meta->make_immutable;
54             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