From: Bob W. <bwi...@pl...> - 2008-03-31 15:26:11
|
I ran into a problem with book covers on my vuFind install on CentOS. Basically the fopen wrapper for URL's seems to be broken in the latest CentOS release. I thought others might run into the same problem because many admins disable fopen wrappers for URLs because of security concerns. The preferred way is to use curl. I rewrote the Amazon book cover code to use curl and while I was at it I implemented a poor mans caching scheme. An added benefit to using curl is the fact that we can control the timeout in the script so when things are bad we don't have to wait for an eternity for image retrieval to timeout. To make it work all you need is a cover_images folder in web with write access for apache. There are still a couple things to be done. I need to write some sort or cache management script to clean up and keep the cache current and I haven't touched the other two image sources yet. I'm not sure of the best way to post the source so I'll try including it in this message. <?php /** * * Copyright (C) Villanova University 2007. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /* * @todo Convert this to an AJAX approach to allow for client side access to * images. Also investigate local caching approach. What about using * Squid? */ // Retrieve values from configuration file $configArray = parse_ini_file('conf/config.ini', true); if (isset($configArray['BookCovers'])) { switch($configArray['BookCovers']['provider']) { case 'Google': if (is_readable('cover_images/' . $_GET['isn'] . '.jpg')) { // Load local cache if available header('Content-type: image/jpeg'); echo readfile('cover_images/' . $_GET['isn'] . '.jpg'); } else { // Fetch from provider google(); } break; case 'Syndetics': if (is_readable('cover_images/' . $_GET['isn'] . '.jpg')) { // Load local cache if available header('Content-type: image/jpeg'); echo readfile('cover_images/' . $_GET['isn'] . '.jpg'); } else { // Fetch from provider syndetics(); } break; case 'Amazon': default: if (is_readable('cover_images/amazon-'. $_GET['size'] .'-'. $_GET['isn'] .'.jpg')) { // Load local cache if available header('Content-type: image/jpeg'); echo readfile('cover_images/amazon-'. $_GET['size'] .'-'. $_GET['isn'] .'.jpg'); } else { // Fetch from provider amazon(); } break; } } else { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } function syndetics() { global $configArray; switch ($_GET['size']) { case 'small': header('Content-type: image/gif'); $size = 'SC.GIF'; break; case 'medium': header('Content-type: image/gif'); $size = 'MC.GIF'; break; case 'large': header('Content-type: image/jpeg'); $size = 'LC.JPG'; break; } $url = 'http://www.syndetics.com/index.aspx?type=xw12&isbn=' . $_GET['isn'] . '/' . $size . '&client=' . $configArray['BookCovers']['id']; if (!($binary = readfile($url))) { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } else { echo $binary; } } function google() { require_once 'HTTP/Request.php'; global $configArray; $url = 'http://books.google.com/books?jscmd=viewapi&' . 'bibkeys=ISBN:' . $_GET['isn'] . '&callback=addTheCover'; $client = new HTTP_Request(); $client->setMethod(HTTP_REQUEST_METHOD_GET); $client->setURL($url); $result = $client->sendRequest(); if (!PEAR::isError($result)) { $json = $client->getResponseBody(); $json = substr($json, strlen('addthecover(')); $json = substr($json, 0, -strlen(');')); if ($json = json_decode($json, true)) { if ($fp = fopen($json['ISBN:' . $_GET['isn']]['thumbnail_url'], 'r')) { header('Content-type: image/jpeg'); $image = $json['ISBN:' . $_GET['isn']]['thumbnail_url']; echo readfile($image); fclose($fp); } else { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } } else { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } } else { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } } function amazon() { require_once 'HTTP/Request.php'; require_once 'XML/Unserializer.php'; global $configArray; $url = 'http://webservices.amazon.com/onca/xml?' . 'Service=AWSECommerceService&' . 'SubscriptionId=' . $configArray['BookCovers']['id'] . '&' . 'Operation=ItemLookup&' . 'ResponseGroup=Images&' . 'ItemId=' . $_GET['isn']; $client = new HTTP_Request(); $client->setMethod(HTTP_REQUEST_METHOD_GET); $client->setURL($url); $result = $client->sendRequest(); if (!PEAR::isError($result)) { $unxml = new XML_Unserializer(); $unxml->unserialize($client->getResponseBody()); $data = $unxml->getUnserializedData(); if (isset($data['Items']['Item'])) { switch ($_GET['size']) { case 'small': $image = $data['Items']['Item']['SmallImage']['URL']; break; case 'medium': $image = $data['Items']['Item']['MediumImage']['URL']; break; case 'large': $image = $data['Items']['Item']['LargeImage']['URL']; break; } if ($image) { $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, $image); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $image_data = curl_exec($ch); $curl_error = curl_error($ch); curl_close($ch); if ($curl_error == '') { if ($cache_image = fopen('cover_images/amazon-'. $_GET['size'] .'-'. $_GET['isn'] .'.jpg', 'wb')) { fwrite($cache_image,$image_data); fclose($cache_image); } header('Content-type: image/jpeg'); echo $image_data; } else { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } } else { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } } else { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } } else { header('Content-type: image/gif'); echo readfile('images/noCover2.gif'); } } ?> Bob Wicksall Systems Administrator Pioneer Library System 2557 State Rt. 21 Canandaigua, New York 14424 Voice: (585) 394-8260 Fax: (585) 394-1935 |