| 1 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | package Plack::Middleware::Image::Scale; | ||
| 3 | our $AUTHORITY = 'cpan:PNU'; | ||
| 4 | # ABSTRACT: Resize jpeg and png images on the fly | ||
| 5 | |||
| 6 | 3 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use Class::Load; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use Plack::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use Plack::MIME; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 3 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use Image::Scale; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use List::Util qw( min max ); | ||
| 13 | use Carp; | ||
| 14 | |||
| 15 | extends 'Plack::Middleware'; | ||
| 16 | |||
| 17 | our $VERSION = '0.011'; # VERSION | ||
| 18 | |||
| 19 | |||
| 20 | has path => ( | ||
| 21 | is => 'rw', lazy => 1, isa => 'RegexpRef|CodeRef|Str|Undef', | ||
| 22 | default => undef | ||
| 23 | ); | ||
| 24 | |||
| 25 | |||
| 26 | has match => ( | ||
| 27 | is => 'rw', lazy => 1, isa => 'RegexpRef|CodeRef', | ||
| 28 | default => sub { qr{^(.+?)(?:_([^_]+?))?(?:\.(jpe?g|png|image))$} } | ||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | has size => ( | ||
| 33 | is => 'rw', lazy => 1, isa => 'RegexpRef|CodeRef|HashRef|Undef', | ||
| 34 | default => sub { qr{^(\d+)?x(\d+)?(?:-(.+))?$} } | ||
| 35 | ); | ||
| 36 | |||
| 37 | |||
| 38 | has any_ext => ( | ||
| 39 | is => 'rw', lazy => 1, isa => 'Str|Undef', | ||
| 40 | default => 'image' | ||
| 41 | ); | ||
| 42 | |||
| 43 | |||
| 44 | has orig_ext => ( | ||
| 45 | is => 'rw', lazy => 1, isa => 'ArrayRef', | ||
| 46 | default => sub { [qw( jpg png gif jpeg )] } | ||
| 47 | ); | ||
| 48 | |||
| 49 | |||
| 50 | has memory_limit => ( | ||
| 51 | is => 'rw', lazy => 1, isa => 'Int|Undef', | ||
| 52 | default => 10_000_000 # bytes | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | has jpeg_quality => ( | ||
| 57 | is => 'rw', lazy => 1, isa => 'Int|Undef', | ||
| 58 | default => undef | ||
| 59 | ); | ||
| 60 | |||
| 61 | |||
| 62 | has width => ( | ||
| 63 | is => 'rw', lazy => 1, isa => 'Int|Undef', | ||
| 64 | default => undef | ||
| 65 | ); | ||
| 66 | |||
| 67 | |||
| 68 | has height => ( | ||
| 69 | is => 'rw', lazy => 1, isa => 'Int|Undef', | ||
| 70 | default => undef | ||
| 71 | ); | ||
| 72 | |||
| 73 | |||
| 74 | has flags => ( | ||
| 75 | is => 'rw', lazy => 1, isa => 'HashRef|Undef', | ||
| 76 | default => undef | ||
| 77 | ); | ||
| 78 | |||
| 79 | sub call { | ||
| 80 | my ($self,$env) = @_; | ||
| 81 | my $path = $env->{PATH_INFO}; | ||
| 82 | my @param; | ||
| 83 | |||
| 84 | if ( defined $self->path ) { | ||
| 85 | my ($m) = _match($path,$self->path); | ||
| 86 | return $self->app->($env) unless $m; | ||
| 87 | } | ||
| 88 | |||
| 89 | my @m = _match($path,$self->match); | ||
| 90 | return $self->app->($env) unless @m; | ||
| 91 | ($path, my $size, my $ext) = @m; | ||
| 92 | return $self->app->($env) unless $path and $ext; | ||
| 93 | |||
| 94 | if ( defined $size ) { | ||
| 95 | @param = _unroll(_match($size,$self->size)); | ||
| 96 | return $self->app->($env) unless @param; | ||
| 97 | } | ||
| 98 | |||
| 99 | my $res = $self->fetch_orig($env,$path); | ||
| 100 | return $self->app->($env) unless $res; | ||
| 101 | |||
| 102 | ## Post-process the response with a body filter | ||
| 103 | $self->response_cb( $res, sub { | ||
| 104 | my $res = shift; | ||
| 105 | my $orig_ct = Plack::Util::header_get( $res->[1], 'Content-Type' ); | ||
| 106 | my $ct; | ||
| 107 | if ( defined $self->any_ext and $ext eq $self->any_ext ) { | ||
| 108 | $ct = Plack::Util::header_get( $res->[1], 'Content-Type' ); | ||
| 109 | } else { | ||
| 110 | $ct = Plack::MIME->mime_type(".$ext"); | ||
| 111 | Plack::Util::header_set( $res->[1], 'Content-Type', $ct ); | ||
| 112 | } | ||
| 113 | return $self->body_scaler( $ct, $orig_ct, @param ); | ||
| 114 | }); | ||
| 115 | } | ||
| 116 | |||
| 117 | ## Helper for matching a Scalar value against CodeRef, HashRef, | ||
| 118 | ## RegexpRef or Str. The first argument may be modified during match. | ||
| 119 | sub _match { | ||
| 120 | my @match; | ||
| 121 | for ( $_[0] ) { | ||
| 122 | my $match = $_[1]; | ||
| 123 | @match = | ||
| 124 | 'CODE' eq ref $match ? $match->($_) : | ||
| 125 | 'HASH' eq ref $match ? $match->{$_} : | ||
| 126 | 'Regexp' eq ref $match ? $_ =~ $match : | ||
| 127 | defined $match ? (substr($_,0,length $match) eq $match ? ($match) : ()) : | ||
| 128 | undef; | ||
| 129 | } | ||
| 130 | return @match; | ||
| 131 | } | ||
| 132 | |||
| 133 | ## Helper for extracting (width,height,flags) from | ||
| 134 | ## HashRef or ArrayRef. | ||
| 135 | sub _unroll { | ||
| 136 | return unless @_; | ||
| 137 | for ( $_[0] ) { | ||
| 138 | ## Config::General style hash of hashrefs. | ||
| 139 | if ( ref eq 'HASH' ) { | ||
| 140 | my %e = %{$_}; | ||
| 141 | return (delete @e{'width','height'}, \%e); | ||
| 142 | ## Manual config friendly hash of arraysrefs. | ||
| 143 | } elsif ( ref eq 'ARRAY' ) { | ||
| 144 | return @{$_}; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | return @_; | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | sub fetch_orig { | ||
| 152 | my ($self,$env,$basename) = @_; | ||
| 153 | |||
| 154 | for my $ext ( @{$self->orig_ext} ) { | ||
| 155 | local $env->{PATH_INFO} = "$basename.$ext"; | ||
| 156 | my $r = $self->app->($env); | ||
| 157 | return $r unless ref $r eq 'ARRAY' and $r->[0] == 404; | ||
| 158 | } | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | sub body_scaler { | ||
| 164 | my $self = shift; | ||
| 165 | my @args = @_; | ||
| 166 | |||
| 167 | my $buffer = q{}; | ||
| 168 | my $filter_cb = sub { | ||
| 169 | my $chunk = shift; | ||
| 170 | |||
| 171 | ## Buffer until we get EOF | ||
| 172 | if ( defined $chunk ) { | ||
| 173 | $buffer .= $chunk; | ||
| 174 | return q{}; #empty | ||
| 175 | } | ||
| 176 | |||
| 177 | ## Return EOF when done | ||
| 178 | return if not defined $buffer; | ||
| 179 | |||
| 180 | ## Process the buffer | ||
| 181 | my $img = $buffer ? $self->image_scale(\$buffer,@args) : ''; | ||
| 182 | undef $buffer; | ||
| 183 | return $img; | ||
| 184 | }; | ||
| 185 | |||
| 186 | return $filter_cb; | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | sub image_scale { | ||
| 191 | my ($self, $bufref, $ct, $orig_ct, $width, $height, $flags) = @_; | ||
| 192 | |||
| 193 | ## $flags can be a HashRef, or it's parsed as a string | ||
| 194 | my %flag = 'HASH' eq ref $flags ? %{ $flags } : | ||
| 195 | map { (split /(?<=\w)(?=\d)/, $_, 2)[0,1]; } split '-', $flags || ''; | ||
| 196 | |||
| 197 | $width = $self->width if defined $self->width; | ||
| 198 | $height = $self->height if defined $self->height; | ||
| 199 | %flag = %{ $self->flags } if defined $self->flags; | ||
| 200 | |||
| 201 | my $owidth = $width; | ||
| 202 | my $oheight = $height; | ||
| 203 | |||
| 204 | if ( defined $flag{z} and $flag{z} > 0 ) { | ||
| 205 | $width *= 1 + $flag{z} / 100 if $width; | ||
| 206 | $height *= 1 + $flag{z} / 100 if $height; | ||
| 207 | } | ||
| 208 | |||
| 209 | my $output; | ||
| 210 | if (defined $orig_ct and $orig_ct eq 'application/pdf') { | ||
| 211 | try { | ||
| 212 | Class::Load::load_class('Image::Magick::Thumbnail::PDF'); | ||
| 213 | Class::Load::load_class('File::Temp'); | ||
| 214 | my $in = File::Temp->new( SUFFIX => '.pdf' ); | ||
| 215 | my $out = File::Temp->new( SUFFIX => '.png' ); | ||
| 216 | $in->write( $$bufref ); $in->close; | ||
| 217 | Image::Magick::Thumbnail::PDF::create_thumbnail( | ||
| 218 | $in->filename, $out->filename, $flag{p}||1, { | ||
| 219 | frame => 0, normalize => 0, | ||
| 220 | restriction => max($width, $height), | ||
| 221 | } | ||
| 222 | ); | ||
| 223 | my $pdfdata; | ||
| 224 | $out->seek( 0, 0 ); | ||
| 225 | $out->read( $pdfdata, 9999999 ); | ||
| 226 | $bufref = \$pdfdata; | ||
| 227 | } catch { | ||
| 228 | carp $_; | ||
| 229 | $output = $$bufref; | ||
| 230 | }; | ||
| 231 | } | ||
| 232 | try { | ||
| 233 | my $img = Image::Scale->new($bufref) | ||
| 234 | or die 'Invalid data / image format not recognized'; | ||
| 235 | |||
| 236 | if ( exists $flag{crop} and defined $width and defined $height ) { | ||
| 237 | my $ratio = $img->width / $img->height; | ||
| 238 | $width = max $width , $height * $ratio; | ||
| 239 | $height = max $height, $width / $ratio; | ||
| 240 | } elsif ( exists $flag{fit} and defined $width and defined $height ) { | ||
| 241 | my $ratio = $img->width / $img->height; | ||
| 242 | $width = min $width , $height * $ratio; | ||
| 243 | $height = min $height, $width / $ratio; | ||
| 244 | } | ||
| 245 | |||
| 246 | unless ( defined $width or defined $height ) { | ||
| 247 | ## We want to keep the size, but Image::Scale | ||
| 248 | ## doesn't return data unless we call resize. | ||
| 249 | $width = $img->width; $height = $img->height; | ||
| 250 | } | ||
| 251 | $img->resize({ | ||
| 252 | defined $width ? (width => $width) : (), | ||
| 253 | defined $height ? (height => $height) : (), | ||
| 254 | exists $flag{fill} ? (keep_aspect => 1) : (), | ||
| 255 | defined $flag{fill} ? (bgcolor => hex $flag{fill}) : (), | ||
| 256 | defined $self->memory_limit ? | ||
| 257 | (memory_limit => $self->memory_limit) : (), | ||
| 258 | }); | ||
| 259 | |||
| 260 | $output = $ct eq 'image/jpeg' ? $img->as_jpeg($self->jpeg_quality || ()) : | ||
| 261 | $ct eq 'image/png' ? $img->as_png : | ||
| 262 | die "Conversion to '$ct' is not implemented"; | ||
| 263 | } catch { | ||
| 264 | carp $_; | ||
| 265 | $output = $$bufref; | ||
| 266 | }; | ||
| 267 | |||
| 268 | if ( defined $owidth and $width > $owidth or | ||
| 269 | defined $oheight and $height > $oheight ) { | ||
| 270 | try { | ||
| 271 | Class::Load::load_class('Imager'); | ||
| 272 | my $img = Imager->new; | ||
| 273 | $img->read( data => $output ) || die; | ||
| 274 | my $crop = $img->crop( | ||
| 275 | defined $owidth ? (width => $owidth) : (), | ||
| 276 | defined $oheight ? (height => $oheight) : (), | ||
| 277 | ); | ||
| 278 | $crop->write( data => \$output, type => (split '/', $ct)[1] ); | ||
| 279 | } catch { | ||
| 280 | carp $_; | ||
| 281 | }; | ||
| 282 | } | ||
| 283 | |||
| 284 | return $output; | ||
| 285 | } | ||
| 286 | |||
| 287 | 1; | ||
| 288 | |||
| 289 | __END__ | ||