File Coverage

blib/lib/Mojolicious/Plugin/AssetPack/Pipe/Png.pm
Criterion Covered Total %
statement 16 27 59.2
branch 2 10 20.0
condition 1 3 33.3
subroutine 4 6 66.6
pod 1 1 100.0
total 24 47 51.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Pipe::Png;
2 1     1   5 use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe';
  1         3  
  1         6  
3              
4 1     1   63 use Mojolicious::Plugin::AssetPack::Util qw(diag DEBUG);
  1         2  
  1         494  
5              
6             has app => 'pngquant';
7             has app_args => sub {
8             my $self = shift;
9             return [DEBUG ? () : ('-quiet'), qw(-clobber -preserve $input)] if $self->app eq 'optipng';
10             return [DEBUG ? ('-v') : (), qw(--speed 2 -)] if $self->app eq 'pngquant';
11             return [];
12             };
13              
14             sub process {
15 1     1 1 4 my ($self, $assets) = @_;
16 1         8 my $store = $self->assetpack->store;
17 1         8 my $file;
18              
19             return $assets->each(sub {
20 1     1   13 my ($asset, $index) = @_;
21 1         6 my $attrs = $asset->TO_JSON;
22 1         6 $attrs->{key} = sprintf '%s-min', $self->app;
23 1         12 $attrs->{minified} = 1;
24 1 50 33     4 return if $asset->format ne 'png' or $asset->minified;
25 1 50       16 return unless $self->assetpack->minify;
26 0 0         return $asset->content($file)->minified(1) if $file = $store->load($attrs);
27 0           diag 'Process "%s", with checksum %s.', $asset->url, $attrs->{checksum} if DEBUG;
28 0           $asset->content($store->save($self->_run_app($asset), $attrs))->FROM_JSON($attrs);
29 1         15 });
30             }
31              
32             sub _install_optipng {
33 0     0     my $self = shift;
34 0           my $class = ref $self;
35 0 0         my $app = $^O eq 'darwin' ? 'brew' : 'apt-get'; # not very nice
36 0           die "$class requires http://optipng.sourceforge.net/ '$app install optipng'";
37             }
38              
39             sub _install_pngquant {
40 0     0     my $self = shift;
41 0           my $class = ref $self;
42 0 0         my $app = $^O eq 'darwin' ? 'brew' : 'apt-get'; # not very nice
43 0           die "$class requires https://pngquant.org/ '$app install pngquant'";
44             }
45              
46             1;
47              
48             =encoding utf8
49              
50             =head1 NAME
51              
52             Mojolicious::Plugin::AssetPack::Pipe::Png - Crush PNG image files
53              
54             =head1 SYNOPSIS
55              
56             =head2 Application
57              
58             plugin AssetPack => {pipes => ["Png"]};
59              
60             # Forces the use of "optipng -clobber -preserve $input"
61             app->asset->pipe("Png")->app("optipng");
62              
63             # Forces the use of "pngquant --speed 2 -"
64             app->asset->pipe("Png")->app("pngquant");
65              
66             # Set custom application arguments:
67             app->asset->pipe("Png")->app("pngquant")->app_args([qw(--speed 10 --ordered -)]);
68              
69             =head1 DESCRIPTION
70              
71             L can be used to crush "png" image
72             files.
73              
74             This plugin has default settings for "pngquant" (default) and "optipng". Which
75             will be the default in the future is unknown, so force the one you want in case
76             that matters.
77              
78             This pipe is EXPERIMENTAL. Feedback wanted.
79              
80             TODO: Detect which application is installed and use the best available.
81              
82             TODO: Add support for pngcrush.
83              
84             =head1 ATTRIBUTES
85              
86             =head2 app
87              
88             $str = $self->app;
89             $self = $self->app("pngquant");
90              
91             Can be used to set a custom application.
92              
93             =head2 app_args
94              
95             $array = $self->app_args;
96             $self = $self->app_args([qw(-clobber $input)]);
97              
98             Can be used to set custom L arguments. The special C<$input> string in
99             the argument list will be replaced with the path to a temp file holding the
100             image data.
101              
102             If no C<$input> element is found in the L list, then STDIN and
103             STDOUT will be used instead.
104              
105             =head1 METHODS
106              
107             =head2 process
108              
109             See L.
110              
111             =head1 SEE ALSO
112              
113             L.
114              
115             =cut