* e.g.
*
*
* size must be one of: small, medium, large
* isbn must be either 10 or 13 characters long and can contain only the digits and the letter X (or x)
*
* Installation:
* 1) Copy this file to your web server
* 2) Create a directory where all of the book covers will be stored. Enter the directory name in the TOP
* definition below.
* 3) Make sure that the directory permissions allow PHP scripts to create new files.
* 4) Enter your own LibraryThing developer key in the KEY definition below. If you do not have your own
* developer get, get one at http://www.librarything.com/developers.php ; it's free.
* 5) Pick a default image to be served up if LibraryThing does not have the cover you need (or if your
* developer key has already been used 1000 times today). Put that image on your server and put its
* local file name in the DEFAULT_IMAGE definition below.
*/
define('TOP', 'covers');
define('KEY', 'PUT-YOUR-DEVELOPER-KEY-HERE');
define('DEFAULT_IMAGE', 'covers/transparent.gif');
$debugging = false; // change to true to print diagnostic information
/*
* ================= change nothing below this line =================
*/
if ($debugging)
header("Content-type: text/html");
$size = strtolower($_REQUEST['size']);
if ('small' != $size && 'medium' != $size && 'large' != $size)
{
print "
Illegal size
"; exit; } $isbn = strtoupper(preg_replace('/[^0-9Xx]/', '', $_REQUEST['isbn'])); // remove everything except digits and "X" if (10 != strlen($isbn) && 13 != strlen($isbn)) { print "Illegal ISBN
"; exit; } $directory = makeDirectory(TOP, $isbn, $debugging); $imageFile = "${directory}/${isbn}_${size}.jpg"; if ($debugging) print "creating/serving local file $imageFile
\n"; if ( ! file_exists($imageFile) ) { $ltUrl = 'http://covers.librarything.com/devkey/' . KEY . "/$size/isbn/$isbn"; if ($debugging) print "we don't have a local copy. trying to fetch the image from $ltUrl
\n"; if ($remote = fopen($ltUrl, 'r') ) { $image = ''; while ( ! feof($remote) ) $image .= fread($remote, 8192); fclose($remote); if (strlen($image) >= 100 && $local = fopen($imageFile, 'w')) // do not cache the transparent GIF image { fwrite($local, $image); fclose($local); if ($debugging) print "we have created a local copy of an LT image
\n"; } } } else { if ($debugging) print "we have a locally cached copy
\n"; } $imageToServe = file_exists($imageFile) ? $imageFile : DEFAULT_IMAGE; if ($debugging) { print "Now serving $imageToServe
\n"; print "next you should see some gobbledegook, the image itself
\n"; } else { header("Content-Type: image/jpeg"); header("Content-Length: " . filesize($imageToServe)); header("X-Pad: avoid browser bug"); } readfile($imageToServe); function makeDirectory($top, $isbn, $debugging) { $parts = Array($top, substr($isbn, 0, 3), substr($isbn, 3, 3), substr($isbn, 6, 3), substr($isbn, 9, 3)); if (13 == strlen($isbn)) array_push($parts, substr($isbn, 12, 3)); $dirName = join('/', $parts); if (file_exists($dirName)) return $dirName; $path = $top; for ($i = 1; $i < count($parts); $i++) { $path .= '/' . $parts[$i]; if ($debugging) print "creating directory $path (it might already exist and that's OK)
\n"; @mkdir($path); } return $dirName; } ?>