| 1 | package Dist::Zilla::Plugin::JSAN::OptimizePNG; | ||
| 2 | 2 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | $Dist::Zilla::Plugin::JSAN::OptimizePNG::VERSION = '0.06'; | |
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: a plugin for Dist::Zilla which optimize the PNG images | ||
| 7 | |||
| 8 | 2 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Moose::Autobox; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | with 'Dist::Zilla::Role::AfterBuild'; | ||
| 12 | |||
| 13 | use Deployer::Image::PNG; | ||
| 14 | |||
| 15 | use Path::Class; | ||
| 16 | use File::Find::Rule; | ||
| 17 | |||
| 18 | |||
| 19 | #================================================================================================================================================================================================================================================ | ||
| 20 | sub mvp_multivalue_args { qw( dirs ) } | ||
| 21 | sub mvp_aliases { return { dir => 'dirs' } } | ||
| 22 | |||
| 23 | |||
| 24 | has dirs => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'ArrayRef', | ||
| 27 | default => sub { [ 'static/images' ] }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | |||
| 31 | has only_for_release => ( | ||
| 32 | is => 'rw', | ||
| 33 | default => 1 | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | has 'use_lossless' => ( | ||
| 38 | is => 'rw', | ||
| 39 | |||
| 40 | default => 1 | ||
| 41 | ); | ||
| 42 | |||
| 43 | |||
| 44 | has 'use_quantization' => ( | ||
| 45 | is => 'rw', | ||
| 46 | |||
| 47 | default => 1 | ||
| 48 | ); | ||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | has 'use_optipng' => ( | ||
| 53 | is => 'rw', | ||
| 54 | |||
| 55 | default => 1 | ||
| 56 | ); | ||
| 57 | |||
| 58 | |||
| 59 | has 'use_pngout' => ( | ||
| 60 | is => 'rw', | ||
| 61 | |||
| 62 | default => 0 | ||
| 63 | ); | ||
| 64 | |||
| 65 | |||
| 66 | has 'png_out_binary' => ( | ||
| 67 | is => 'rw' | ||
| 68 | ); | ||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | #================================================================================================================================================================================================================================================ | ||
| 76 | sub after_build { | ||
| 77 | my ($self, $param) = @_; | ||
| 78 | |||
| 79 | return if ($self->only_for_release && !$ENV{ DZIL_RELEASING }); | ||
| 80 | |||
| 81 | my $build_root = dir($param->{ build_root }); | ||
| 82 | |||
| 83 | my @png_files; | ||
| 84 | |||
| 85 | foreach my $dir ($self->dirs->flatten) { | ||
| 86 | push @png_files, File::Find::Rule->or( | ||
| 87 | File::Find::Rule->file->name('*.png') | ||
| 88 | )->in($build_root->subdir($dir)); | ||
| 89 | } | ||
| 90 | |||
| 91 | my @log; | ||
| 92 | |||
| 93 | my $overall_before = 0; | ||
| 94 | my $overall_after = 0; | ||
| 95 | |||
| 96 | foreach my $file (@png_files) { | ||
| 97 | my $image = Deployer::Image::PNG->new({ | ||
| 98 | filename => $file, | ||
| 99 | |||
| 100 | use_lossless => $self->use_lossless, | ||
| 101 | use_quantization => $self->use_quantization, | ||
| 102 | |||
| 103 | use_pngout => $self->use_pngout, | ||
| 104 | use_optipng => $self->use_optipng, | ||
| 105 | png_out_binary => $self->png_out_binary | ||
| 106 | }); | ||
| 107 | |||
| 108 | my $before = $image->get_size; | ||
| 109 | $overall_before += $before; | ||
| 110 | |||
| 111 | $image->optimize(); | ||
| 112 | |||
| 113 | my $after = $image->get_size; | ||
| 114 | $overall_after += $after; | ||
| 115 | |||
| 116 | $file =~ /(.{0,30})$/; | ||
| 117 | |||
| 118 | $self->log(sprintf("File %30.30s: before =%7d, after =%7d, optimization = %.3f%%", $1, $before, $after, 100 * ($after - $before) / $before)); | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->log('Overall:'); | ||
| 122 | $self->log(sprintf(" %30s before =%7d, after =%7d, optimization = %.3f%%", '', $overall_before, $overall_after, 100 * ($overall_after - $overall_before) / $overall_before)); | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | no Moose; | ||
| 128 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); | ||
| 129 | |||
| 130 | |||
| 131 | 1; | ||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | __END__ | ||