Support named sizes in permalink image urls
[bookreader.git] / BookReaderIA / datanode / BookReaderImages.inc.php
index 4933999..1c9c44b 100644 (file)
@@ -28,7 +28,7 @@ require_once("BookReaderMeta.inc.php");
 
 class BookReaderImages
 {
-    public $MIMES = array('gif' => 'image/gif',
+    public static $MIMES = array('gif' => 'image/gif',
                    'jp2' => 'image/jp2',
                    'jpg' => 'image/jpeg',
                    'jpeg' => 'image/jpeg',
@@ -36,7 +36,7 @@ class BookReaderImages
                    'tif' => 'image/tiff',
                    'tiff' => 'image/tiff');
                    
-    public $EXTENSIONS = array('gif' => 'gif',
+    public static $EXTENSIONS = array('gif' => 'gif',
                         'jp2' => 'jp2',
                         'jpeg' => 'jpeg',
                         'jpg' => 'jpeg',
@@ -45,11 +45,22 @@ class BookReaderImages
                         'tiff' => 'tiff');
     
     // Width when generating thumbnails
-    public $imageSizes = array(
+    public static $imageSizes = array(
         'thumb' => 100,
-        'small' => 240,
-        'medium' => 500,
-        'large' => 1024,
+        'small' => 256,
+        'medium' => 512,
+        'large' => 2048,
+    );
+
+    // Keys in the image permalink urls, e.g. http://www.archive.org/download/itemid/page/cover_{keyval}_{keyval}.jpg
+    public static $imageUrlKeys = array(
+        //'r' => 'reduce',
+        's' => 'scale',
+        'region' => 'region',
+        'tile' => 'tile',
+        'w' => 'width',
+        'h' => 'height',
+        'rotate' => 'rotate'
     );
     
     // Paths to command-line tools
@@ -166,10 +177,12 @@ class BookReaderImages
             'ext' => 'jpg',
         );
         
-        if ($pageInfo['reduce']) {
-            $requestEnv['reduce'] = $pageInfo['reduce'];
-        }
-        // $$$ handle scale, other sizes, rotation, etc
+        // remove non-passthrough keys from pageInfo
+        unset($pageInfo['type']);
+        unset($pageInfo['value']);
+        
+        // add pageinfo to request
+        $requestEnv = array_merge($pageInfo, $requestEnv);
 
         // Return image data - will check privs        
         $this->serveRequest($requestEnv);
@@ -255,6 +268,8 @@ class BookReaderImages
         
         // The pbmreduce reduction factor produces an image with dimension 1/n
         // The kakadu reduction factor produceds an image with dimension 1/(2^n)
+        
+        // Set scale from height or width if set
         if (isset($requestEnv['height'])) {
             $powReduce = $this->nearestPow2Reduce($requestEnv['height'], $imageInfo['height']);
             $scale = pow(2, $powReduce);
@@ -265,11 +280,16 @@ class BookReaderImages
         } else {
             // $$$ could be cleaner
             // Provide next smaller power of two reduction
+            
+            // Set scale from 'scale' if set
             $scale = $requestEnv['scale'];
             if (!$scale) {
                 $scale = 1;
             }
-            if (array_key_exists($scale, $this->imageSizes)) {
+            
+            // Set scale from named size (e.g. 'large') if set
+            $size = $requestEnv['size'];
+            if ( $size && array_key_exists($size, self::$imageSizes)) {
                 $srcRatio = floatval($imageInfo['width']) / floatval($imageInfo['height']);
                 if ($srcRatio > 1) {
                     // wide
@@ -277,10 +297,13 @@ class BookReaderImages
                 } else {
                     $dimension = 'height';
                 }
-                $powReduce = $this->nearestPow2Reduce($this->imageSizes[$scale], $imageInfo[$dimension]);
+                $powReduce = $this->nearestPow2Reduce(self::$imageSizes[$size], $imageInfo[$dimension]);
             } else {
-                $powReduce = $this->nearestPow2ForScale($scale);
+                // No named size - update powReduce from scale
+                $powReduce = $this->nearestPow2ForScale($sale);
             }
+            
+            // Make sure scale matches powReduce
             $scale = pow(2, $powReduce);
         }
         
@@ -341,7 +364,7 @@ class BookReaderImages
         
         $filenameForClient = $this->filenameForClient($file, $ext);
         
-        $headers = array('Content-type: '. $MIMES[$ext], // XXX is nginx swallowing this?
+        $headers = array('Content-type: '. self::$MIMES[$ext],
                          'Cache-Control: max-age=15552000',
                          'Content-disposition: inline; filename=' . $filenameForClient);
                           
@@ -413,8 +436,8 @@ class BookReaderImages
     function imageExtensionToType($extension)
     {
         
-        if (array_key_exists($extension, $this->EXTENSIONS)) {
-            return $this->EXTENSIONS[$extension];
+        if (array_key_exists($extension, self::$EXTENSIONS)) {
+            return self::$EXTENSIONS[$extension];
         } else {
             $this->BRfatal('Unknown image extension');
         }            
@@ -757,19 +780,6 @@ class BookReaderImages
             'title' => 'single'
         );
         
-        $sizes = array(
-            'large', 'thumb', 'medium', 'small', 'orig'
-        );
-        
-        $keys = array(
-            'r' => 'reduce',
-            's' => 'scale',
-            'region' => 'region',
-            'tile' => 'tile',
-            'w' => 'width',
-            'h' => 'height'
-        );
-        
         // Look for known page types
         foreach ( $pageTypes as $pageName => $kind ) {
             if ( preg_match('#^(' . $pageName . ')(.*)#', $page, $matches) === 1 ) {
@@ -793,7 +803,7 @@ class BookReaderImages
         
         // Look for other known parts
         foreach ($parts as $part) {
-            if ( in_array($part, $sizes) ) {
+            if ( array_key_exists($part, self::$imageSizes) ) {
                 $pageInfo['size'] = $part;
                 continue;
             }
@@ -808,8 +818,8 @@ class BookReaderImages
             $key = $matches[1];
             $value = $matches[2];
             
-            if ( array_key_exists($key, $keys) ) {
-                $pageInfo[$keys[$key]] = $value;
+            if ( array_key_exists($key, self::$imageUrlKeys) ) {
+                $pageInfo[self::$imageUrlKeys[$key]] = $value;
                 continue;
             }