aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App/Controller/Photo.pm2
-rw-r--r--perllib/FixMyStreet/App/Model/PhotoSet.pm18
-rw-r--r--perllib/FixMyStreet/ImageMagick.pm42
-rw-r--r--perllib/FixMyStreet/Roles/PhotoSet.pm6
4 files changed, 56 insertions, 12 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm
index 5507e3a94..3408d5e35 100644
--- a/perllib/FixMyStreet/App/Controller/Photo.pm
+++ b/perllib/FixMyStreet/App/Controller/Photo.pm
@@ -43,7 +43,7 @@ sub during :LocalRegex('^(temp|fulltemp)\.([0-9a-f]{40}\.(?:jpeg|png|gif|tiff))$
$c->forward( 'output', [ $photo ] );
}
-sub index :LocalRegex('^(c/)?([1-9]\d*)(?:\.(\d+))?(?:\.(full|tn|fp))?\.(?:jpeg|png|gif|tiff)$') {
+sub index :LocalRegex('^(c/)?([1-9]\d*)(?:\.(\d+))?(?:\.(full|tn|fp|og))?\.(?:jpeg|png|gif|tiff)$') {
my ( $self, $c ) = @_;
my ( $is_update, $id, $photo_number, $size ) = @{ $c->req->captures };
diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm
index c9f1c48a1..76a287e71 100644
--- a/perllib/FixMyStreet/App/Model/PhotoSet.pm
+++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm
@@ -192,7 +192,6 @@ sub get_image_data {
my $image = $self->get_raw_image( $num )
or return;
- my $photo = $image->{data};
my $size = $args{size};
@@ -201,19 +200,24 @@ sub get_image_data {
return $image;
}
- my $im = FixMyStreet::ImageMagick->new(blob => $photo);
+ my $im = FixMyStreet::ImageMagick->new(blob => $image->{data});
+ my $photo;
if ( $size eq 'tn' ) {
- $photo = $im->shrink('x100')->as_blob;
+ $photo = $im->shrink('x100');
} elsif ( $size eq 'fp' ) {
- $photo = $im->crop->as_blob;
+ $photo = $im->crop;
+ } elsif ( $size eq 'og' ) {
+ $photo = $im->crop('1200x630');
} elsif ( $size eq 'full' ) {
- # do nothing
+ $photo = $im
} else {
- $photo = $im->shrink($args{default} || '250x250')->as_blob;
+ $photo = $im->shrink($args{default} || '250x250');
}
return {
- data => $photo,
+ data => $photo->as_blob,
+ width => $photo->width,
+ height => $photo->height,
content_type => $image->{content_type},
};
}
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;
diff --git a/perllib/FixMyStreet/Roles/PhotoSet.pm b/perllib/FixMyStreet/Roles/PhotoSet.pm
index 27a63bad5..3d0027f8c 100644
--- a/perllib/FixMyStreet/Roles/PhotoSet.pm
+++ b/perllib/FixMyStreet/Roles/PhotoSet.pm
@@ -31,6 +31,11 @@ sub get_first_image_fp {
return $self->get_photoset->get_image_data( num => 0, size => 'fp' );
}
+sub get_first_image_og {
+ my ($self) = @_;
+ return $self->get_photoset->get_image_data( num => 0, size => 'og' );
+}
+
sub photos {
my $self = shift;
my $photoset = $self->get_photoset;
@@ -62,6 +67,7 @@ sub photos {
url_full => "/photo/$typ$id.$i.full.$format?$cachebust",
url_tn => "/photo/$typ$id.$i.tn.$format?$cachebust",
url_fp => "/photo/$typ$id.$i.fp.$format?$cachebust",
+ url_og => "/photo/$typ$id.$i.og.$format?$cachebust",
idx => $i++,
}
} $photoset->all_ids;