aboutsummaryrefslogtreecommitdiffstats
path: root/web/photo.cgi
blob: fc89fc0f747ab8e7aeae62f79e23f0356dc4a6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl -w -I../perllib

# photo.cgi:
# Display a photo for FixMyStreet
#
# Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
# $Id: photo.cgi,v 1.11 2008-10-09 14:20:54 matthew Exp $

use strict;
use Standard;
use Error qw(:try);
use CGI::Carp;

sub main {
    my $q = shift;
    print $q->header(-type => 'image/jpeg',
        -expires => '+1y' );
    my $id = $q->param('id');
    my $c = $q->param('c');
    return unless ($id || $c);
    my $photo;
    if ($c) {
        $photo = dbh()->selectrow_arrayref("select photo from comment where
            id=? and state = 'confirmed' and photo is not null", {}, $c);
    } else {
        $photo = dbh()->selectrow_arrayref( "select photo from problem where
            id=? and state in ('confirmed', 'fixed', 'partial') and photo is not
            null", {}, $id);
    }
    return unless $photo;
    $photo = $photo->[0];
    if ($q->param('tn')) {
        $photo = resize($photo, 'x100');
    } elsif ($q->{site} eq 'emptyhomes') {
        $photo = resize($photo, '195x');
    }

    print $photo;
}
Page::do_fastcgi(\&main);

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];
}