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