diff options
Diffstat (limited to 'perllib/FixMyStreet/ImageMagick.pm')
-rw-r--r-- | perllib/FixMyStreet/ImageMagick.pm | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/perllib/FixMyStreet/ImageMagick.pm b/perllib/FixMyStreet/ImageMagick.pm index af9f56478..d9f643801 100644 --- a/perllib/FixMyStreet/ImageMagick.pm +++ b/perllib/FixMyStreet/ImageMagick.pm @@ -23,6 +23,26 @@ has image => ( }, ); +has width => ( + is => 'rwp', + lazy => 1, + default => sub { + my $self = shift; + return unless $self->image; + return $self->image->Get('width'); + } +); + +has height => ( + is => 'rwp', + lazy => 1, + default => sub { + my $self = shift; + return unless $self->image; + return $self->image->Get('height'); + } +); + sub strip { my $self = shift; return $self unless $self->image; @@ -35,6 +55,7 @@ sub rotate { return $self unless $self->image; my $err = $self->image->Rotate($direction); return 0 if $err; + $self->_set_width_and_height(); return $self; } @@ -44,17 +65,21 @@ sub shrink { return $self unless $self->image; my $err = $self->image->Scale(geometry => "$size>"); throw Error::Simple("resize failed: $err") if "$err"; + $self->_set_width_and_height(); return $self->strip; } -# Shrinks a picture to 90x60, cropping so that it is exactly that. +# Shrinks a picture to a given dimension (defaults to 90x60(, cropping so that +# it is exactly that. sub crop { - my $self = shift; + my ($self, $size) = @_; + $size //= '90x60'; return $self unless $self->image; - my $err = $self->image->Resize( geometry => "90x60^" ); + my $err = $self->image->Resize( geometry => "$size^" ); throw Error::Simple("resize failed: $err") if "$err"; - $err = $self->image->Extent( geometry => '90x60', gravity => 'Center' ); + $err = $self->image->Extent( geometry => $size, gravity => 'Center' ); throw Error::Simple("resize failed: $err") if "$err"; + $self->_set_width_and_height(); return $self->strip; } @@ -62,8 +87,17 @@ sub as_blob { my $self = shift; return $self->blob unless $self->image; my @blobs = $self->image->ImageToBlob(); + $self->_set_width_and_height(); $self->_set_image(undef); return $blobs[0]; } +sub _set_width_and_height { + my $self = shift; + return unless $self->image; + my ($width, $height) = $self->image->Get('width', 'height'); + $self->_set_width($width); + $self->_set_height($height); +} + 1; |