58358a78c42627b4c7d6bdfd163a10197b922755
[koha.git] / catalogue / 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
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 #
23 #
24 #
25
26 use Modern::Perl;
27
28 use CGI qw ( -utf8 );    #qw(:standard escapeHTML);
29 use C4::Context;
30 use Koha::CoverImages;
31 use Koha::Biblios;
32 use Koha::Exceptions;
33
34 $| = 1;
35
36 my $DEBUG = 0;
37 my $data  = CGI->new;
38 my $imagenumber;
39
40 =head1 NAME
41
42 image.pl - Script for retrieving and formatting local cover images for display
43
44 =head1 SYNOPSIS
45
46 <img src="image.pl?imagenumber=X" />
47 <img src="image.pl?biblionumber=X" />
48 <img src="image.pl?imagenumber=X&thumbnail=1" />
49 <img src="image.pl?biblionumber=X&thumbnail=1" />
50
51 =head1 DESCRIPTION
52
53 This script, when called from within HTML and passed a valid imagenumber or
54 biblionumber, will retrieve the image data associated with that biblionumber
55 if one exists, format it in proper HTML format and pass it back to be displayed.
56 If the parameter thumbnail has been provided, a thumbnail will be returned
57 rather than the full-size image. When a biblionumber is provided rather than an
58 imagenumber, a random image is selected.
59
60 =cut
61
62 my ( $image );
63 if ( C4::Context->preference("LocalCoverImages") ) {
64     my $imagenumber = $data->param('imagenumber');
65     my $biblionumber = $data->param('biblionumber');
66     if ( defined $imagenumber ) {
67         $imagenumber = $data->param('imagenumber');
68         $image = Koha::CoverImages->find($imagenumber);
69     }
70     elsif ( defined $biblionumber ) {
71         my $biblio = Koha::Biblios->find($biblionumber);
72         Koha::Exceptions::ObjectNotFound->throw( 'No bibliographic record for biblionumber ' . $biblionumber ) unless $biblio;
73         my $cover_images = $biblio->cover_images;
74         if ( $cover_images->count ) {
75             $image = $cover_images->next;
76         } else {
77             warn "No images for this biblio" if $DEBUG;
78         }
79     }
80 }
81
82 $image ||= Koha::CoverImages->no_image;
83
84 my $image_data =
85     $data->param('thumbnail')
86   ? $image->thumbnail
87   : $image->imagefile;
88
89 print $data->header(
90     -type            => $image->mimetype,
91     -expires         => '+30m',
92     -Content_Length  => length($image_data)
93 ), $image_data;
94
95 =head1 AUTHOR
96
97 Chris Nighswonger cnighswonger <at> foundations <dot> edu
98
99 modified for local cover images by Koustubha Kale kmkale <at> anantcorp <dot> com
100
101 =cut