| 1 | package Mojolicious::Plugin::AssetPack::Pipe::Png; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Mojolicious::Plugin::AssetPack::Util qw(diag DEBUG); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | has app => 'pngquant'; | |||||||||||||||||||||||||||
| 7 | 0 | ▶ | has app_args => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 8 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 9 | 0 | ▶ | return [DEBUG ? () : ('-quiet'), qw(-clobber -preserve $input)] if $self->app eq 'optipng'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | return [DEBUG ? ('-v') : (), qw(--speed 2 -)] if $self->app eq 'pngquant'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 11 | 0 | return []; | |||||||||||||||||||||||||||
| 12 | 1 | }; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=1 SCAR=0.7
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 1 | my ($self, $assets) = @_; | |||||||||||||||||||||||||||
| 16 | 1 | my $store = $self->assetpack->store; | |||||||||||||||||||||||||||
| 17 | 1 | my $file; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | return $assets->each(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=20.9
| |||||||||||||||||||||||||||||
| 20 | 1 | my ($asset, $index) = @_; | |||||||||||||||||||||||||||
| 21 | 1 | my $attrs = $asset->TO_JSON; | |||||||||||||||||||||||||||
| 22 | 1 | $attrs->{key} = sprintf '%s-min', $self->app; | |||||||||||||||||||||||||||
| 23 | 1 | $attrs->{minified} = 1; | |||||||||||||||||||||||||||
| 24 | 1 | ▶ | return if $asset->format ne 'png' or $asset->minified; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$asset->format ne "png" or $asset->minified
MC/DC0%0/2
$asset->format ne "png" or $asset->minified
$asset->format ne "png"
$asset->minified
Truth table33%1/3
A or B
A= $asset->format ne "png"B= $asset->minified
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | return unless $self->assetpack->minify; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | return $asset->content($file)->minified(1) if $file = $store->load($attrs); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | }); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub _install_optipng { | ||||||||||||||||||||||||||
|
Subroutine
_install_optipng: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 0 | my $class = ref $self; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | my $app = $^O eq 'darwin' ? 'brew' : 'apt-get'; # not very nice | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | die "$class requires http://optipng.sourceforge.net/ '$app install optipng'"; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub _install_pngquant { | ||||||||||||||||||||||||||
|
Subroutine
_install_pngquant: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 41 | 0 | my $class = ref $self; | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $app = $^O eq 'darwin' ? 'brew' : 'apt-get'; # not very nice | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | die "$class requires https://pngquant.org/ '$app install pngquant'"; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | 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<Mojolicious::Plugin::AssetPack::Pipe::Png> 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</app> 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</app_args> list, then STDIN and | ||||||||||||||||||||||||||||
| 103 | STDOUT will be used instead. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 METHODS | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head2 process | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | See L<Mojolicious::Plugin::AssetPack::Pipe/process>. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | L<Mojolicious::Plugin::AssetPack>. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||