Handling of non-jp2 image formats using ImageMagick's convert
[bookreader.git] / BookReaderIA / datanode / BookReaderImages.php
index 8f4e7fe..fd06508 100644 (file)
@@ -1,9 +1,14 @@
 <?php
 
 /*
-Copyright(c)2008 Internet Archive. Software license AGPL version 3.
+Copyright(c) 2008-2010 Internet Archive. Software license AGPL version 3.
 
-This file is part of BookReader.
+This file is part of BookReader.  The full source code can be found at GitHub:
+http://github.com/openlibrary/bookreader
+
+The canonical short name of an image type is the same as in the MIME type.
+For example both .jpeg and .jpg are considered to have type "jpeg" since
+the MIME type is "image/jpeg".
 
     BookReader is free software: you can redistribute it and/or modify
     it under the terms of the GNU Affero General Public License as published by
@@ -19,170 +24,27 @@ This file is part of BookReader.
     along with BookReader.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-$MIMES = array('jpg' => 'image/jpeg',
-               'png' => 'image/png');
-
-$zipPath  = $_REQUEST['zip'];
-$file     = $_REQUEST['file'];
-
-// Unfortunately kakadu requires us to know a priori if the
-// output file should be .ppm or .pgm.  By decompressing to
-// .bmp kakadu will write a file we can consistently turn into
-// .pnm.  Really kakadu should support .pnm as the file output
-// extension and automatically write ppm or pgm format as
-// appropriate.
-$decompressToBmp = true;
-if ($decompressToBmp) {
-  $stdoutLink = '/tmp/stdout.bmp';
-} else {
-  $stdoutLink = '/tmp/stdout.ppm';
-}
-
-if (isset($_REQUEST['ext'])) {
-  $ext = $_REQUEST['ext'];
-} else {
-  // Default to jpg
-  $ext = 'jpg';
-}
-
-$fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
-
-// Rotate is currently only supported for jp2 since it does not add server load
-$allowedRotations = array("0", "90", "180", "270");
-$rotate = $_REQUEST['rotate'];
-if ( !in_array($rotate, $allowedRotations) ) {
-    $rotate = "0";
-}
-
-// Image conversion options
-$pngOptions = '';
-$jpegOptions = '-quality 75';
-
-// The pbmreduce reduction factor produces an image with dimension 1/n
-// The kakadu reduction factor produceds an image with dimension 1/(2^n)
-
-if (isset($_REQUEST['height'])) {
-    $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
-    if ($ratio <= 2) {
-        $scale = 2;
-        $powReduce = 1;    
-    } else if ($ratio <= 4) {
-        $scale = 4;
-        $powReduce = 2;
-    } else {
-        //$powReduce = 3; //too blurry!
-        $scale = 2;
-        $powReduce = 1;
-    }
-
-} else {
-    $scale = $_REQUEST['scale'];
-    if (1 >= $scale) {
-        $scale = 1;
-        $powReduce = 0;
-    } else if (2 == $scale) {
-        $powReduce = 1;
-    } else if (4 == $scale) {
-        $powReduce = 2;
-    } else if (8 == $scale) {
-        $powReduce = 3;
-    } else if (16 == $scale) {
-        $powReduce = 4;
-    } else if (32 == $scale) {
-        $powReduce = 5;
-    } else {
-        // $$$ Leaving this in as default though I'm not sure why it is...
-        $scale = 8;
-        $powReduce = 3;
-    }
-}
-
-if (!file_exists($stdoutLink)) 
-{  
-  system('ln -s /dev/stdout ' . $stdoutLink);  
-}
-
+// Include BookReaderImages class definition
+require_once('BookReaderImages.inc.php');
 
-putenv('LD_LIBRARY_PATH=/petabox/sw/lib/kakadu');
-
-$unzipCmd  = 'unzip -p ' . 
-        escapeshellarg($zipPath) .
-        ' ' . escapeshellarg($file);
-        
-if ('jp2' == $fileExt) {
-    $decompressCmd = 
-        " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $powReduce -rotate $rotate -i /dev/stdin -o " . $stdoutLink;
-    if ($decompressToBmp) {
-        $decompressCmd .= ' | bmptopnm ';
-    }
-    
-} else if ('tif' == $fileExt) {
-    // We need to create a temporary file for tifftopnm since it cannot
-    // work on a pipe (the file must be seekable).
-    // We use the BookReaderTiff prefix to give a hint in case things don't
-    // get cleaned up.
-    $tempFile = tempnam("/tmp", "BookReaderTiff");
-    
-    if (1 != $scale) {
-        if (onPowerNode()) {
-            $pbmReduce = ' | pnmscale -reduce ' . $scale;
-        } else {
-            $pbmReduce = ' | pnmscale -nomix -reduce ' . $scale;
-        }
+// Serve request
+$bri = new BookReaderImages();
+try {
+    if ($_REQUEST['page']) {
+        // Need to lookup metadata
+        $bri->serveLookupRequest($_REQUEST);
     } else {
-        $pbmReduce = '';
+        // Request should be fully qualified - no lookup needed
+        $bri->serveRequest($_REQUEST);
     }
+} catch (Exception $e) {
+    header("HTTP/1.0 404 Not Found");
+    header("Content-type: text/plain");
     
-    $decompressCmd = 
-        ' > ' . $tempFile . ' ; tifftopnm ' . $tempFile . ' 2>/dev/null' . $pbmReduce;
-
-} else {
-    BRfatal('Unknown source file extension: ' . $fileExt);
+    print "Error serving request:\n";
+    print "  " . $e->getMessage() . "\n\n";
+    print "Debugging information:\n";
+    echo $e->getTraceAsString();
 }
-       
-// Non-integer scaling is currently disabled on the cluster
-// if (isset($_REQUEST['height'])) {
-//     $cmd .= " | pnmscale -height {$_REQUEST['height']} ";
-// }
-
-if ('jpg' == $ext) {
-    $compressCmd = ' | pnmtojpeg ' . $jpegOptions;
-} else if ('png' == $ext) {
-    $compressCmd = ' | pnmtopng ' . $pngOptions;
-}
-
-$cmd = $unzipCmd . $decompressCmd . $compressCmd;
-
-//print $cmd;
-
-header('Content-type: ' . $MIMES[$ext]);
-header('Cache-Control: max-age=15552000');
-
-passthru ($cmd);
-
-if (isset($tempFile)) {
-  unlink($tempFile);
-}
-
-function BRFatal($string) {
-    echo "alert('$string')\n";
-    die(-1);
-}
-
-// Returns true if using a power node
-function onPowerNode() {
-    exec("lspci | fgrep -c Realtek", $output, $return);
-    if ("0" != $output[0]) {
-        return true;
-    } else {
-        exec("egrep -q AMD /proc/cpuinfo", $output, $return);
-        if ($return == 0) {
-            return true;
-        }
-    }
-    return false;
-}
-
-
-?>
 
+?>
\ No newline at end of file