diff options
author | Graeme Porteous <graeme@rgbp.co.uk> | 2019-12-18 16:16:54 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2020-01-27 16:00:41 +0000 |
commit | 2d6af993630d9e43594975e093507ee62ed53b97 (patch) | |
tree | 514de24dd76344d88dcd5bf6494dbda519e90ebe /perllib/FixMyStreet/ImageMagick.pm | |
parent | 856ae6bed192345249df8569518a4ecd074b3dc0 (diff) |
Add per report OpenGraph images
Includes a bit of refactoring of PhotoSet::get_image_data to make
it easier to call subsequent methods on the photo object. We then
do this to get the width and height.
Also adds width/height attributes to FixMyStreet::ImageMagick
Attributes are updated every time the image is transformed and before
the as_blob data, which also undefs the image, is returned so it always
present for subsequent calls.
Fixes #2394.
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; |