aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Photo.pm
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2012-03-14 13:11:55 +0000
committerMatthew Somerville <matthew@mysociety.org>2012-03-14 13:11:55 +0000
commitf465e32109d6600887f1c5765b44347a4cfaa3d7 (patch)
tree3855e96d1d69683b388d82c27bad3f9e339f2792 /perllib/FixMyStreet/App/Controller/Photo.pm
parent1b15ca0aea334d20fb0f19fed36bc948668e2a14 (diff)
parentc9681f6bbc04659b9d6ed5eaa46b8c2edd704f9e (diff)
Merge branch 'redesign'
Conflicts: .gitignore notes/INSTALL perllib/FixMyStreet/App/Controller/Photo.pm perllib/FixMyStreet/Cobrand/FixMyStreet.pm
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Photo.pm')
-rw-r--r--perllib/FixMyStreet/App/Controller/Photo.pm68
1 files changed, 58 insertions, 10 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm
index 27dd6f184..8711b19e9 100644
--- a/perllib/FixMyStreet/App/Controller/Photo.pm
+++ b/perllib/FixMyStreet/App/Controller/Photo.pm
@@ -5,6 +5,7 @@ use namespace::autoclean;
BEGIN {extends 'Catalyst::Controller'; }
use DateTime::Format::HTTP;
+use Path::Class;
=head1 NAME
@@ -25,17 +26,30 @@ Display a photo
=cut
-sub index :Path :Args(0) {
+sub during :LocalRegex('^([0-9a-f]{40})\.temp\.jpeg$') {
my ( $self, $c ) = @_;
+ my ( $hash ) = @{ $c->req->captures };
- my $id = $c->req->param('id');
- my $comment = $c->req->param('c');
- $c->detach( 'no_photo' ) unless $id || $comment;
+ my $file = file( $c->config->{UPLOAD_DIR}, "$hash.jpeg" );
+ my $photo = $file->slurp;
+
+ if ( $c->cobrand->default_photo_resize ) {
+ $photo = _shrink( $photo, $c->cobrand->default_photo_resize );
+ } else {
+ $photo = _shrink( $photo, 'x250' );
+ }
+
+ $c->forward( 'output', [ $photo ] );
+}
+
+sub index :LocalRegex('^(c/)?(\d+)(?:\.(full|tn|fp))?\.jpeg$') {
+ my ( $self, $c ) = @_;
+ my ( $is_update, $id, $size ) = @{ $c->req->captures };
my @photo;
- if ( $comment ) {
+ if ( $is_update ) {
@photo = $c->model('DB::Comment')->search( {
- id => $comment,
+ id => $id,
state => 'confirmed',
photo => { '!=', undef },
} );
@@ -56,12 +70,30 @@ sub index :Path :Args(0) {
$c->detach( 'no_photo' ) unless @photo;
my $photo = $photo[0]->photo;
- if ( $c->req->param('tn' ) ) {
- $photo = _resize( $photo, 'x100' );
+
+ # If photo field contains a hash
+ if (length($photo) == 40) {
+ my $file = file( $c->config->{UPLOAD_DIR}, "$photo.jpeg" );
+ $photo = $file->slurp;
+ }
+
+ if ( $size eq 'tn' ) {
+ $photo = _shrink( $photo, 'x100' );
+ } elsif ( $size eq 'fp' ) {
+ $photo = _crop( $photo );
+ } elsif ( $size eq 'full' ) {
} elsif ( $c->cobrand->default_photo_resize ) {
- $photo = _resize( $photo, $c->cobrand->default_photo_resize );
+ $photo = _shrink( $photo, $c->cobrand->default_photo_resize );
+ } else {
+ $photo = _shrink( $photo, 'x250' );
}
+ $c->forward( 'output', [ $photo ] );
+}
+
+sub output : Private {
+ my ( $self, $c, $photo ) = @_;
+
my $dt = DateTime->now()->add( years => 1 );
$c->res->content_type( 'image/jpeg' );
@@ -74,7 +106,8 @@ sub no_photo : Private {
$c->detach( '/page_error_404_not_found', [ 'No photo' ] );
}
-sub _resize {
+# Shrinks a picture to the specified size, but keeping in proportion.
+sub _shrink {
my ($photo, $size) = @_;
use Image::Magick;
my $image = Image::Magick->new;
@@ -86,6 +119,21 @@ sub _resize {
return $blobs[0];
}
+# Shrinks a picture to 90x60, cropping so that it is exactly that.
+sub _crop {
+ my ($photo) = @_;
+ use Image::Magick;
+ my $image = Image::Magick->new;
+ $image->BlobToImage($photo);
+ my $err = $image->Resize( geometry => "90x60^" );
+ throw Error::Simple("resize failed: $err") if "$err";
+ $err = $image->Extent( geometry => '90x60', gravity => 'Center' );
+ throw Error::Simple("resize failed: $err") if "$err";
+ my @blobs = $image->ImageToBlob();
+ undef $image;
+ return $blobs[0];
+}
+
=head1 AUTHOR
Struan Donald