aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/ImageMagick.pm
diff options
context:
space:
mode:
authorMarius Halden <marius.h@lden.org>2019-10-30 19:28:55 +0100
committerMarius Halden <marius.h@lden.org>2019-10-30 19:28:55 +0100
commit377bd96aab7cad3434185c30eb908c9da447fe40 (patch)
tree7ec5527e205d5b62caaa862a7de8cd25199c8bf0 /perllib/FixMyStreet/ImageMagick.pm
parent56f61b1441070aa0b9ddcfc74aca46c20313609f (diff)
parent92b253904062edd533e55c22824de6fd01e2f7c1 (diff)
Merge tag 'v2.6' into fiksgatami-dev
Diffstat (limited to 'perllib/FixMyStreet/ImageMagick.pm')
-rw-r--r--perllib/FixMyStreet/ImageMagick.pm69
1 files changed, 69 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/ImageMagick.pm b/perllib/FixMyStreet/ImageMagick.pm
new file mode 100644
index 000000000..af9f56478
--- /dev/null
+++ b/perllib/FixMyStreet/ImageMagick.pm
@@ -0,0 +1,69 @@
+package FixMyStreet::ImageMagick;
+
+use Moo;
+
+my $IM = eval {
+ return 0 if FixMyStreet->test_mode;
+ require Image::Magick;
+ Image::Magick->import;
+ 1;
+};
+
+has blob => ( is => 'ro' );
+
+has image => (
+ is => 'rwp',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ return unless $IM;
+ my $image = Image::Magick->new;
+ $image->BlobToImage($self->blob);
+ return $image;
+ },
+);
+
+sub strip {
+ my $self = shift;
+ return $self unless $self->image;
+ $self->image->Strip();
+ return $self;
+}
+
+sub rotate {
+ my ($self, $direction) = @_;
+ return $self unless $self->image;
+ my $err = $self->image->Rotate($direction);
+ return 0 if $err;
+ return $self;
+}
+
+# Shrinks a picture to the specified size, but keeping in proportion.
+sub shrink {
+ my ($self, $size) = @_;
+ return $self unless $self->image;
+ my $err = $self->image->Scale(geometry => "$size>");
+ throw Error::Simple("resize failed: $err") if "$err";
+ return $self->strip;
+}
+
+# Shrinks a picture to 90x60, cropping so that it is exactly that.
+sub crop {
+ my $self = shift;
+ return $self unless $self->image;
+ my $err = $self->image->Resize( geometry => "90x60^" );
+ throw Error::Simple("resize failed: $err") if "$err";
+ $err = $self->image->Extent( geometry => '90x60', gravity => 'Center' );
+ throw Error::Simple("resize failed: $err") if "$err";
+ return $self->strip;
+}
+
+sub as_blob {
+ my $self = shift;
+ return $self->blob unless $self->image;
+ my @blobs = $self->image->ImageToBlob();
+ $self->_set_image(undef);
+ return $blobs[0];
+}
+
+1;