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