From d7fe2f24dbccce034d52b630c225c2f342860fa2 Mon Sep 17 00:00:00 2001 From: Jared Camins-Esakov Date: Wed, 7 Mar 2012 22:13:34 -0500 Subject: [PATCH] Bug 7600: Return a single-pixel GIF when no local cover image exists Signed-off-by: Kristina D.C. Hoeppner Signed-off-by: Paul Poulain --- C4/Images.pm | 31 +++++++++++++++---- catalogue/image.pl | 76 ++++++++++++++++++---------------------------- opac/opac-image.pl | 76 ++++++++++++++++++---------------------------- 3 files changed, 83 insertions(+), 100 deletions(-) diff --git a/C4/Images.pm b/C4/Images.pm index 209cde05c4..8afc8fa294 100644 --- a/C4/Images.pm +++ b/C4/Images.pm @@ -25,7 +25,7 @@ use 5.010; use C4::Context; use GD; -use vars qw($debug $VERSION @ISA @EXPORT); +use vars qw($debug $noimage $VERSION @ISA @EXPORT); BEGIN { @@ -40,6 +40,11 @@ BEGIN { &DelImage ); $debug = $ENV{KOHA_DEBUG} || $ENV{DEBUG} || 0; + + $noimage = pack( "H*", + '47494638396101000100800000FFFFFF' + . '00000021F90401000000002C00000000' + . '010001000002024401003B' ); } =head2 PutImage @@ -101,13 +106,14 @@ sub RetrieveImage { my $sth = $dbh->prepare($query); $sth->execute($imagenumber); my $imagedata = $sth->fetchrow_hashref; - if ( $sth->err ) { - warn "Database error!"; - return undef; + if ( !$imagedata ) { + $imagedata->{'thumbnail'} = $noimage; + $imagedata->{'imagefile'} = $noimage; } - else { - return $imagedata; + if ( $sth->err ) { + warn "Database error!" if $debug; } + return $imagedata; } =head2 ListImagesForBiblio @@ -192,4 +198,17 @@ sub _scale_image { } } +=head2 NoImage + + C4::Images->NoImage; + +Returns the gif to be used when there is no image matching the request, and +its mimetype (image/gif). + +=cut + +sub NoImage { + return $noimage, 'image/gif'; +} + 1; diff --git a/catalogue/image.pl b/catalogue/image.pl index 2d9a5e1935..4fa77e9056 100755 --- a/catalogue/image.pl +++ b/catalogue/image.pl @@ -58,62 +58,44 @@ imagenumber, a random image is selected. =cut -error() unless C4::Context->preference("OPACLocalCoverImages"); - -if ( defined $data->param('imagenumber') ) { - $imagenumber = $data->param('imagenumber'); -} -elsif ( defined $data->param('biblionumber') ) { - my @imagenumbers = ListImagesForBiblio( $data->param('biblionumber') ); - if (@imagenumbers) { - $imagenumber = $imagenumbers[0]; - } - else { - warn "No images for this biblio" if $DEBUG; - error(); +my ( $image, $mimetype ) = C4::Images->NoImage; +if ( C4::Context->preference("LocalCoverImages") ) { + if ( defined $data->param('imagenumber') ) { + $imagenumber = $data->param('imagenumber'); } -} -else { - $imagenumber = shift; -} - -if ($imagenumber) { - warn "imagenumber passed in: $imagenumber" if $DEBUG; - my $imagedata = RetrieveImage($imagenumber); - - error() unless $imagedata; - - if ($imagedata) { - my $image; - if ( $data->param('thumbnail') ) { - $image = $imagedata->{'thumbnail'}; + elsif ( defined $data->param('biblionumber') ) { + my @imagenumbers = ListImagesForBiblio( $data->param('biblionumber') ); + if (@imagenumbers) { + $imagenumber = $imagenumbers[0]; } else { - $image = $imagedata->{'imagefile'}; + warn "No images for this biblio" if $DEBUG; } - print $data->header( - -type => $imagedata->{'mimetype'}, - -'Cache-Control' => 'no-store', - -expires => 'now', - -Content_Length => length($image) - ), $image; - exit; } else { - warn "No image exists for $imagenumber" if $DEBUG; - error(); + $imagenumber = shift; } -} -else { - error(); -} -error(); - -sub error { - print $data->header( -status => '404', -expires => 'now' ); - exit; + if ($imagenumber) { + warn "imagenumber passed in: $imagenumber" if $DEBUG; + my $imagedata = RetrieveImage($imagenumber); + if ($imagedata) { + if ( $data->param('thumbnail') ) { + $image = $imagedata->{'thumbnail'}; + } + else { + $image = $imagedata->{'imagefile'}; + } + $mimetype = $imagedata->{'mimetype'}; + } + } } +print $data->header( + -type => $mimetype, + -'Cache-Control' => 'no-store', + -expires => 'now', + -Content_Length => length($image) +), $image; =head1 AUTHOR diff --git a/opac/opac-image.pl b/opac/opac-image.pl index 340a47cdfa..ba554c7bef 100755 --- a/opac/opac-image.pl +++ b/opac/opac-image.pl @@ -58,62 +58,44 @@ imagenumber, a random image is selected. =cut -error() unless C4::Context->preference("OPACLocalCoverImages"); - -if ( defined $data->param('imagenumber') ) { - $imagenumber = $data->param('imagenumber'); -} -elsif ( defined $data->param('biblionumber') ) { - my @imagenumbers = ListImagesForBiblio( $data->param('biblionumber') ); - if (@imagenumbers) { - $imagenumber = $imagenumbers[0]; - } - else { - warn "No images for this biblio" if $DEBUG; - error(); +my ( $image, $mimetype ) = C4::Images->NoImage; +if ( C4::Context->preference("OPACLocalCoverImages") ) { + if ( defined $data->param('imagenumber') ) { + $imagenumber = $data->param('imagenumber'); } -} -else { - $imagenumber = shift; -} - -if ($imagenumber) { - warn "imagenumber passed in: $imagenumber" if $DEBUG; - my $imagedata = RetrieveImage($imagenumber); - - error() unless $imagedata; - - if ($imagedata) { - my $image; - if ( $data->param('thumbnail') ) { - $image = $imagedata->{'thumbnail'}; + elsif ( defined $data->param('biblionumber') ) { + my @imagenumbers = ListImagesForBiblio( $data->param('biblionumber') ); + if (@imagenumbers) { + $imagenumber = $imagenumbers[0]; } else { - $image = $imagedata->{'imagefile'}; + warn "No images for this biblio" if $DEBUG; } - print $data->header( - -type => $imagedata->{'mimetype'}, - -'Cache-Control' => 'no-store', - -expires => 'now', - -Content_Length => length($image) - ), $image; - exit; } else { - warn "No image exists for $imagenumber" if $DEBUG; - error(); + $imagenumber = shift; } -} -else { - error(); -} -error(); - -sub error { - print $data->header( -status => '404', -expires => 'now' ); - exit; + if ($imagenumber) { + warn "imagenumber passed in: $imagenumber" if $DEBUG; + my $imagedata = RetrieveImage($imagenumber); + if ($imagedata) { + if ( $data->param('thumbnail') ) { + $image = $imagedata->{'thumbnail'}; + } + else { + $image = $imagedata->{'imagefile'}; + } + $mimetype = $imagedata->{'mimetype'}; + } + } } +print $data->header( + -type => $mimetype, + -'Cache-Control' => 'no-store', + -expires => 'now', + -Content_Length => length($image) +), $image; =head1 AUTHOR -- 2.39.5