aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Photo.pm
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2011-06-10 14:56:00 +0100
committerMatthew Somerville <matthew@mysociety.org>2011-06-10 14:56:00 +0100
commit391ca1c469d93bb2c4798cc15e56fc495b5e80dd (patch)
tree6bc90fae589de824095e668fbf510ef259935729 /perllib/FixMyStreet/App/Controller/Photo.pm
parent7c96f8ec61d6eddc211f3f0e71cdb276c6a5f773 (diff)
parent860383f0de3287b0666d64a3ffff3db3a0f087ae (diff)
Merge branch 'migrate_to_catalyst' into reportemptyhomes
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Photo.pm')
-rw-r--r--perllib/FixMyStreet/App/Controller/Photo.pm93
1 files changed, 93 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm
new file mode 100644
index 000000000..3c6255f3a
--- /dev/null
+++ b/perllib/FixMyStreet/App/Controller/Photo.pm
@@ -0,0 +1,93 @@
+package FixMyStreet::App::Controller::Photo;
+use Moose;
+use namespace::autoclean;
+
+BEGIN {extends 'Catalyst::Controller'; }
+
+use DateTime::Format::HTTP;
+
+=head1 NAME
+
+FixMyStreet::App::Controller::Photo - Catalyst Controller
+
+=head1 DESCRIPTION
+
+Catalyst Controller.
+
+=head1 METHODS
+
+=cut
+
+
+=head2 index
+
+Display a photo
+
+=cut
+
+sub index :Path :Args(0) {
+ my ( $self, $c ) = @_;
+
+ my $id = $c->req->param('id');
+ my $comment = $c->req->param('c');
+ return unless ( $id || $comment );
+
+ my @photo;
+ if ( $comment ) {
+ @photo = $c->model('DB::Comment')->search( {
+ id => $comment,
+ state => 'confirmed',
+ photo => { '!=', undef },
+ } );
+ } else {
+ @photo = $c->cobrand->problems->search( {
+ id => $id,
+ state => [ 'confirmed', 'fixed', 'partial' ],
+ photo => { '!=', undef },
+ } );
+ }
+
+ $c->detach( '/page_error_404_not_found', [ 'No photo' ] )
+ unless @photo;
+
+ my $photo = $photo[0]->photo;
+ if ( $c->req->param('tn' ) ) {
+ $photo = _resize( $photo, 'x100' );
+ } elsif ( $c->cobrand->default_photo_resize ) {
+ $photo = _resize( $photo, $c->cobrand->default_photo_resize );
+ }
+
+ my $dt = DateTime->now();
+ $dt->set_year( $dt->year + 1 );
+
+ $c->res->content_type( 'image/jpeg' );
+ $c->res->header( 'expires', DateTime::Format::HTTP->format_datetime( $dt ) );
+ $c->res->body( $photo );
+}
+
+sub _resize {
+ my ($photo, $size) = @_;
+ use Image::Magick;
+ my $image = Image::Magick->new;
+ $image->BlobToImage($photo);
+ my $err = $image->Scale(geometry => "$size>");
+ throw Error::Simple("resize failed: $err") if "$err";
+ my @blobs = $image->ImageToBlob();
+ undef $image;
+ return $blobs[0];
+}
+
+=head1 AUTHOR
+
+Struan Donald
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+
+1;