Bug 1633: QA follow-up
[koha.git] / opac / opac-image.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (C) 2011 C & P Bibliography Services
4 # Jared Camins-Esakov <jcamins@cpbibliograpy.com>
5 #
6 # based on patronimage.pl
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #
23 #
24 #
25
26 use strict;
27 use warnings;
28
29 use CGI;
30 use C4::Context;
31 use C4::Images;
32
33 $| = 1;
34
35 my $DEBUG = 0;
36 my $data  = new CGI;
37 my $imagenumber;
38
39 =head1 NAME
40
41 opac-image.pl - Script for retrieving and formatting local cover images for display
42
43 =head1 SYNOPSIS
44
45 <img src="opac-image.pl?imagenumber=X" />
46 <img src="opac-image.pl?biblionumber=X" />
47 <img src="opac-image.pl?imagenumber=X&thumbnail=1" />
48 <img src="opac-image.pl?biblionumber=X&thumbnail=1" />
49
50 =head1 DESCRIPTION
51
52 This script, when called from within HTML and passed a valid imagenumber or
53 biblionumber, will retrieve the image data associated with that biblionumber
54 if one exists, format it in proper HTML format and pass it back to be displayed.
55 If the parameter thumbnail has been provided, a thumbnail will be returned
56 rather than the full-size image. When a biblionumber is provided rather than an
57 imagenumber, a random image is selected.
58
59 =cut
60
61 error() unless C4::Context->preference("OPACLocalCoverImages");
62
63 if ( defined $data->param('imagenumber') ) {
64     $imagenumber = $data->param('imagenumber');
65 }
66 elsif ( defined $data->param('biblionumber') ) {
67     my @imagenumbers = ListImagesForBiblio( $data->param('biblionumber') );
68     if (@imagenumbers) {
69         $imagenumber = $imagenumbers[0];
70     }
71     else {
72         warn "No images for this biblio" if $DEBUG;
73         error();
74     }
75 }
76 else {
77     $imagenumber = shift;
78 }
79
80 if ($imagenumber) {
81     warn "imagenumber passed in: $imagenumber" if $DEBUG;
82     my $imagedata = RetrieveImage($imagenumber);
83
84     error() unless $imagedata;
85
86     if ($imagedata) {
87         my $image;
88         if ( $data->param('thumbnail') ) {
89             $image = $imagedata->{'thumbnail'};
90         }
91         else {
92             $image = $imagedata->{'imagefile'};
93         }
94         print $data->header(
95             -type            => $imagedata->{'mimetype'},
96             -'Cache-Control' => 'no-store',
97             -expires         => 'now',
98             -Content_Length  => length($image)
99         ), $image;
100         exit;
101     }
102     else {
103         warn "No image exists for $imagenumber" if $DEBUG;
104         error();
105     }
106 }
107 else {
108     error();
109 }
110
111 error();
112
113 sub error {
114     print $data->header( -status => '404', -expires => 'now' );
115     exit;
116 }
117
118 =head1 AUTHOR
119
120 Chris Nighswonger cnighswonger <at> foundations <dot> edu
121
122 modified for local cover images by Koustubha Kale kmkale <at> anantcorp <dot> com
123
124 =cut