diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Photo.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Model/PhotoSet.pm | 18 | ||||
-rw-r--r-- | perllib/FixMyStreet/ImageMagick.pm | 42 | ||||
-rw-r--r-- | perllib/FixMyStreet/Roles/PhotoSet.pm | 6 | ||||
-rw-r--r-- | t/app/controller/report_display.t | 14 | ||||
-rw-r--r-- | templates/web/base/header_opengraph_image.html | 9 | ||||
-rw-r--r-- | templates/web/fixamingata/header_opengraph_image.html | 9 |
8 files changed, 89 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 25a3d126f..f135fb91f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Set report title autocomplete to off to prevent email autocompleting - Add map filter debouncing to reduce server requests. #2675 - Add XSL to RSS feeds so they look nicer in browsers. + - Add per-report OpenGraph images. #2394 - Display GPS marker on /around map. #2359 - Admin improvements: - Add new roles system, to group permissions and apply to users. #2483 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; diff --git a/t/app/controller/report_display.t b/t/app/controller/report_display.t index fbb9d7b75..48a827a63 100644 --- a/t/app/controller/report_display.t +++ b/t/app/controller/report_display.t @@ -441,6 +441,20 @@ for my $test ( }; } +subtest "Correct OpenGraph image is used when report has no photo" => sub { + $report->update({ photo => undef }); + $mech->get_ok("/report/$report_id"); + $mech->content_contains("/cobrands/fixmystreet/images/fms-og_image.jpg", "site image is used"); + $mech->content_lacks("/photo/$report_id.0.og", "report image is not present"); +}; + +subtest "Correct OpenGraph image is used when report has a photo" => sub { + $report->update({ photo => '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg' }); + $mech->get_ok("/report/$report_id"); + $mech->content_contains("/photo/$report_id.0.og.jpeg", "report opengraph image is present"); + $mech->content_lacks("/cobrands/fixmystreet/images/fms-og_image.jpg", "site image is not used"); +}; + my $body_westminster = $mech->create_body_ok(2504, 'Westminster City Council'); my $body_camden = $mech->create_body_ok(2505, 'Camden Borough Council'); diff --git a/templates/web/base/header_opengraph_image.html b/templates/web/base/header_opengraph_image.html index 7ec1aabb0..4cd106fc8 100644 --- a/templates/web/base/header_opengraph_image.html +++ b/templates/web/base/header_opengraph_image.html @@ -1,4 +1,13 @@ + [% IF problem.photo %] + [% photo = problem.photos.first %] + [% data = problem.get_first_image_og %] + <meta property="og:image" content="[% c.cobrand.base_url %][% photo.url_og %]"> + <meta property="og:image:type" content="[% data.content_type %]"> + <meta property="og:image:width" content="[% data.width %]"> + <meta property="og:image:height" content="[% data.height %]"> + [% ELSE %] <meta property="og:image" content="[% c.cobrand.base_url %]/cobrands/fixmystreet/images/fms-og_image.jpg"> <meta property="og:image:type" content="image/jpeg"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="630"> + [% END %] diff --git a/templates/web/fixamingata/header_opengraph_image.html b/templates/web/fixamingata/header_opengraph_image.html index 7a681484a..6bf3d5b01 100644 --- a/templates/web/fixamingata/header_opengraph_image.html +++ b/templates/web/fixamingata/header_opengraph_image.html @@ -1,4 +1,13 @@ + [% IF problem.photo %] + [% photo = problem.photos.first %] + [% data = problem.get_first_image_og %] + <meta property="og:image" content="[% c.cobrand.base_url %][% photo.url_og %]"> + <meta property="og:image:type" content="[% data.content_type %]"> + <meta property="og:image:width" content="[% data.width %]"> + <meta property="og:image:height" content="[% data.height %]"> + [% ELSE %] <meta property="og:image" content="[% c.cobrand.base_url %]/cobrands/fixamingata/images/fms-og_image.jpg"> <meta property="og:image:type" content="image/jpeg"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="630"> + [% END %] |