[PATCH] Add image width to item type images

Michael Hafen mdhafen at tech.washk12.org
Thu Feb 12 21:42:05 CET 2009


This adds width and height to item type images, especially in the staff
search results page.  Also the width is used to make enough room for the
item type image on the search results screen, in the Location cells.
---
 C4/Koha.pm                                         |   53 ++++++++++++++++++++
 C4/Search.pm                                       |    9 +++-
 admin/itemtypes.pl                                 |   12 ++++-
 .../prog/en/modules/catalogue/results.tmpl         |   14 ++++-
 4 files changed, 82 insertions(+), 6 deletions(-)

diff --git a/C4/Koha.pm b/C4/Koha.pm
index b5e87f6..68cd2ac 100644
--- a/C4/Koha.pm
+++ b/C4/Koha.pm
@@ -45,6 +45,7 @@ BEGIN {
 		&getnbpages
 		&get_infos_of
 		&get_notforloan_label_of
+		&getitemtypeimagedimensions
 		&getitemtypeimagedir
 		&getitemtypeimagesrc
 		&getitemtypeimagelocation
@@ -441,11 +442,63 @@ sub getitemtypeinfo {
     $sth->execute($itemtype);
     my $res = $sth->fetchrow_hashref;
 
+    unless ( $res->{imagedimensions} ) {
+	my $dim = getitemtypeimagedimensions( $res->{imageurl} );
+	$sth = $dbh->prepare( "UPDATE itemtypes SET imagedimensions = ? WHERE itemtype = ?" );
+	$sth->execute( $dim, $itemtype );
+	$res->{imagedimensions} = $dim;
+    }
+
     $res->{imageurl} = getitemtypeimagelocation( 'intranet', $res->{imageurl} );
 
     return $res;
 }
 
+=head2 getitemtypeimagedimensions
+
+=over
+
+=item 4
+
+  my $dimensions = getitemtypeimagedimensions( 'set/image_path.ext' );
+
+Pass in the partial path to the image file.
+
+Returns the width and height of the image as a string, seperated by 'x'.
+
+=back
+
+=cut
+
+sub getitemtypeimagedimensions {
+    my $image = shift || return undef;
+
+    my $scheme = ( uri_split( $image ) )[0];
+    return undef if ( $scheme );
+
+    #  FIXME  I probably should care about where I'm pulling this from
+    $image = getitemtypeimagedir() . '/' . $image;
+    ( -r $image ) || return undef;
+
+    use GD;
+    my ( $img, $width, $height );
+
+    for ( $image ) {
+	if    ( $_ =~ /\.gif$/ ) {
+	    $img = GD::Image->newFromGif( $image );
+	}
+	elsif ( $_ =~ /\.jpe?g$/ ) {
+	    $img = GD::Image->newFromJpeg( $image );
+	}
+	elsif ( $_ =~ /\.png$/ ) {
+	    $img = GD::Image->newFromPng( $image );
+	}
+    }
+
+    ( $width, $height ) = $img->getBounds();
+    return $width ."x". $height;
+}
+
 =head2 getitemtypeimagedir
 
 =over
diff --git a/C4/Search.pm b/C4/Search.pm
index f950215..479cace 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -1220,9 +1220,13 @@ sub searchResults {
       );
     $bsth->execute();
     while ( my $bdata = $bsth->fetchrow_hashref ) {
-		foreach (qw(description imageurl summary notforloan)) {
+		foreach (qw(description imageurl imagedimensions summary notforloan)) {
         	$itemtypes{ $bdata->{'itemtype'} }->{$_} = $bdata->{$_};
 		}
+		unless ( $itemtypes{ $bdata->{'itemtype'} }->{'imagedimensions'} ) {
+		    $itemtypes{ $bdata->{'itemtype'} }->{'imagedimensions'} = 
+			getitemtypeimagedimensions( $bdata->{'imageurl'} );
+		}
     }
 
     #search item field code
@@ -1393,6 +1397,7 @@ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
 				$onloan_items->{$key}->{location} = $shelflocations->{ $item->{location} };
 				$onloan_items->{$key}->{itemcallnumber} = $item->{itemcallnumber};
 				$onloan_items->{$key}->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
+				$onloan_items->{$key}->{imagewidth} = [ split 'x', $itemtypes{ $item->{itype} }->{imagedimensions} ]->[1];
                 # if something's checked out and lost, mark it as 'long overdue'
                 if ( $item->{itemlost} ) {
                     $onloan_items->{$prefix}->{longoverdue}++;
@@ -1458,6 +1463,7 @@ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
 					$other_items->{$key}->{count}++ if $item->{$hbranch};
 					$other_items->{$key}->{location} = $shelflocations->{ $item->{location} };
 					$other_items->{$key}->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
+					$other_items->{$key}->{imagewidth} = [ split 'x', $itemtypes{ $item->{itype} }->{imagedimensions} ]->[1];
                 }
                 # item is available
                 else {
@@ -1469,6 +1475,7 @@ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
 					}
 					$available_items->{$prefix}->{location} = $shelflocations->{ $item->{location} };
 					$available_items->{$prefix}->{imageurl} = getitemtypeimagelocation( 'opac', $itemtypes{ $item->{itype} }->{imageurl} );
+					$available_items->{$prefix}->{imagewidth} = [ split 'x', $itemtypes{ $item->{itype} }->{imagedimensions} ]->[1];
                 }
             }
         }    # notforloan, item level and biblioitem level
diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl
index 345e172..73662ea 100755
--- a/admin/itemtypes.pl
+++ b/admin/itemtypes.pl
@@ -127,6 +127,11 @@ if ( $op eq 'add_form' ) {
     # called by add_form, used to insert/modify data in DB
 }
 elsif ( $op eq 'add_validate' ) {
+    my $imagedimensions;
+    if ( $input->param('image') ne 'remoteImage' ) {
+	$imagedimensions = getitemtypeimagedimensions( $input->param('image') );
+    }
+
     my $query = "
         SELECT itemtype
         FROM   itemtypes
@@ -142,6 +147,7 @@ elsif ( $op eq 'add_validate' ) {
                  , rentalcharge = ?
                  , notforloan = ?
                  , imageurl = ?
+                 , imagedimensions = ?
                  , summary = ?
             WHERE itemtype = ?
         ';
@@ -158,6 +164,7 @@ elsif ( $op eq 'add_validate' ) {
                     : $input->param('image') . ""
                 )
             ),
+            $imagedimensions,
             $input->param('summary'),
             $input->param('itemtype')
         );
@@ -165,9 +172,9 @@ elsif ( $op eq 'add_validate' ) {
     else {    # add a new itemtype & not modif an old
         my $query = "
             INSERT INTO itemtypes
-                (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,summary)
+                (itemtype,description,renewalsallowed,rentalcharge, notforloan, imageurl,imagedimensions,summary)
             VALUES
-                (?,?,?,?,?,?,?);
+                (?,?,?,?,?,?,?,?);
             ";
         my $sth = $dbh->prepare($query);
 		my $image = $input->param('image');
@@ -180,6 +187,7 @@ elsif ( $op eq 'add_validate' ) {
             $image eq 'removeImage' ?           ''                 :
             $image eq 'remoteImage' ? $input->param('remoteImage') :
             $image,
+            $imagedimensions,
             $input->param('summary'),
         );
     }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl
index 3609e26..4dfcfe1 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl
@@ -330,7 +330,7 @@ $(window).load(function() {
                                     <ul>
                                     <!-- TMPL_LOOP NAME="available_items_loop" -->
                                         
-                                        <!-- TMPL_IF NAME="noItemTypeImages" --><li><!-- TMPL_ELSE --><!-- TMPL_IF NAME="item-level_itypes" --><!-- TMPL_IF name="imageurl" --><li style=" list-style: none; list-style-type: none;  background-image: url(<!-- TMPL_VAR name="imageurl" -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px 30px; margin: .4em 0; " title="<!-- TMPL_VAR name="description" -->"><!-- TMPL_ELSE --><li><!-- /TMPL_IF --><!-- TMPL_ELSE --><li><!-- /TMPL_IF --><!-- /TMPL_IF -->
+                                        <!-- TMPL_IF NAME="noItemTypeImages" --><li><!-- TMPL_ELSE --><!-- TMPL_IF NAME="item-level_itypes" --><!-- TMPL_IF name="imageurl" --><!-- TMPL_IF NAME="imagewidth" --><li style=" list-style: none; list-style-type: none;  background-image: url(<!-- TMPL_VAR name="imageurl" -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px <!-- TMPL_VAR NAME="imagewidth" -->px; margin: .4em 0; " title="<!-- TMPL_VAR name="description" -->"><!-- TMPL_ELSE --><li style=" list-style: none; list-style-type: none;  background-image: url(<!-- TMPL_VAR name="imageurl" -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px 40px; margin: .4em 0; " title="<!-- TMPL_VAR name="description" -->"><!-- /TMPL_IF --><!-- TMPL_ELSE --><li><!-- /TMPL_IF --><!-- TMPL_ELSE --><li><!-- /TMPL_IF --><!-- /TMPL_IF -->
                                         <!-- TMPL_IF NAME="branchname" --><!-- TMPL_VAR NAME="branchname" --><!-- /TMPL_IF -->
                                         <!-- TMPL_IF NAME="location" --><!-- TMPL_VAR NAME="location" --><!-- /TMPL_IF -->
                                         <!-- TMPL_IF NAME="itemcallnumber" -->[<a href="/cgi-bin/koha/catalogue/search.pl?q=callnum:<!-- TMPL_VAR NAME="itemcallnumber" ESCAPE="URL" -->"><!-- TMPL_VAR NAME="itemcallnumber" --></a>]<!-- /TMPL_IF -->
@@ -346,7 +346,11 @@ $(window).load(function() {
                                     <!-- TMPL_LOOP NAME="onloan_items_loop" -->
                                        <!-- TMPL_IF NAME="noItemTypeImages" --><li><!-- TMPL_ELSE --><!-- TMPL_IF NAME="item-level_itypes" -->
                                         <!-- TMPL_IF name="imageurl" -->
-                                        <li style=" list-style: none; list-style-type: none;  background-image: url(<!-- TMPL_VAR name=imageurl -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px 30px; margin: .4em 0;" title="<!-- TMPL_VAR name="description" -->">
+					<!-- TMPL_IF NAME="imagewidth" -->
+                                        <li style=" list-style: none; list-style-type: none;  background-image: url(<!-- TMPL_VAR name=imageurl -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px <!-- TMPL_VAR NAME="imagewidth" -->px; margin: .4em 0;" title="<!-- TMPL_VAR name="description" -->">
+					<!-- TMPL_ELSE -->
+                                        <li style=" list-style: none; list-style-type: none;  background-image: url(<!-- TMPL_VAR name=imageurl -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px 40px; margin: .4em 0;" title="<!-- TMPL_VAR name="description" -->">
+					<!-- /TMPL_IF -->
                                         <!-- TMPL_ELSE --><li><!-- /TMPL_IF -->
                                         <!-- TMPL_ELSE --><li><!-- /TMPL_IF --><!-- /TMPL_IF -->                                        
 
@@ -363,7 +367,11 @@ $(window).load(function() {
                                     <!-- TMPL_LOOP NAME="other_items_loop" -->
                                         <!-- TMPL_IF NAME="noItemTypeImages" --><li><!-- TMPL_ELSE --><!-- TMPL_IF NAME="item-level_itypes" -->
                                         <!-- TMPL_IF name="imageurl" -->
-                                        <li style="list-style: none; list-style-type: none; background-image: url(<!-- TMPL_VAR name="imageurl" -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px 30px; margin: .4em 0;" title="<!-- TMPL_VAR name="description" -->">
+					<!-- TMPL_IF NAME="imagewidth" -->
+                                        <li style="list-style: none; list-style-type: none; background-image: url(<!-- TMPL_VAR name="imageurl" -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px <!-- TMPL_VAR NAME="imagewidth" -->px; margin: .4em 0;" title="<!-- TMPL_VAR name="description" -->">
+					<!-- TMPL_ELSE -->
+                                        <li style="list-style: none; list-style-type: none; background-image: url(<!-- TMPL_VAR name="imageurl" -->); background-repeat: no-repeat; background-position: 0 50%; padding: 3px 0 3px 40px; margin: .4em 0;" title="<!-- TMPL_VAR name="description" -->">
+					<!-- /TMPL_IF -->
                                         <!-- TMPL_ELSE --><li><!-- /TMPL_IF -->
                                         <!-- TMPL_ELSE --><li><!-- /TMPL_IF --><!-- /TMPL_IF -->
 
-- 
1.5.6.3


--=-nevF6ihQJTc+hrbkrZo/--



More information about the Koha-devel mailing list