a8c16ea6935a350d5d73f9f07483aeda676989ec
[bookreader.git] / GnuBookIA / GnuBookImages.php
1 <?php
2 /* This file is deprecated -- image stack processing has been 
3  * combined into GnuBookImages.php.  This file will eventually
4  * be removed.
5  */
6
7 /*
8 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
9
10 This file is part of GnuBook.
11
12     GnuBook is free software: you can redistribute it and/or modify
13     it under the terms of the GNU Affero General Public License as published by
14     the Free Software Foundation, either version 3 of the License, or
15     (at your option) any later version.
16
17     GnuBook is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU Affero General Public License for more details.
21
22     You should have received a copy of the GNU Affero General Public License
23     along with GnuBook.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 $MIMES = array('jpg' => 'image/jpeg',
27                'png' => 'image/png');
28
29 $zipPath  = $_REQUEST['zip'];
30 $file     = $_REQUEST['file'];
31
32 // Unfortunately kakadu requires us to know a priori if the
33 // output file should be .ppm or .pgm.  By decompressing to
34 // .bmp kakadu will write a file we can consistently turn into
35 // .pnm.  Really kakadu should support .pnm as the file output
36 // extension and automatically write ppm or pgm format as
37 // appropriate.
38 $decompressToBmp = true;
39 if ($decompressToBmp) {
40   $stdoutLink = '/tmp/stdout.bmp';
41 } else {
42   $stdoutLink = '/tmp/stdout.ppm';
43 }
44
45 if (isset($_REQUEST['ext'])) {
46   $ext = $_REQUEST['ext'];
47 } else {
48   // Default to jpg
49   $ext = 'jpg';
50 }
51
52 $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
53
54 // Png conversion options
55 $pngOptions = '';
56
57 // The pbmreduce reduction factor produces an image with dimension 1/n
58 // The kakadu reduction factor produceds an image with dimension 1/(2^n)
59
60 if (isset($_REQUEST['height'])) {
61     $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
62     if ($ratio <= 2) {
63         $scale = 2;
64         $reduce = 1;    
65     } else if ($ratio <= 4) {
66         $scale = 4;
67         $reduce = 2;
68     } else {
69         //$reduce = 3; //too blurry!
70         $scale = 2;
71         $reduce = 1;
72     }
73
74 } else {
75     $scale = $_REQUEST['scale'];
76     if (1 >= $scale) {
77         $scale = 1;
78         $reduce = 0;
79     } else if (2 == $scale) {
80         $reduce = 1;
81     } else if (4 == $scale) {
82         $reduce = 2;
83     } else {
84         // $$$ why do we default to such a small scale?
85         $scale = 8;
86         $reduce = 3;
87     }
88 }
89
90 if (!file_exists($stdoutLink)) 
91 {  
92   system('ln -s /dev/stdout ' . $stdoutLink);  
93 }
94
95
96 putenv('LD_LIBRARY_PATH=/petabox/sw/lib/kakadu');
97
98 $unzipCmd  = 'unzip -p ' . 
99         escapeshellarg($zipPath) .
100         ' ' . escapeshellarg($file);
101         
102 if ('jp2' == $fileExt) {
103     $decompressCmd = 
104         " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $reduce -i /dev/stdin -o " . $stdoutLink;
105     if ($decompressToBmp) {
106         $decompressCmd .= ' | bmptopnm ';
107     }
108 } else if ('tif' == $fileExt) {
109     // We need to create a temporary file for tifftopnm since it cannot
110     // work on a pipe (the file must be seekable).
111     // We use the GnuBookTiff prefix to give a hint in case things don't
112     // get cleaned up.
113     $tempFile = tempnam("/tmp", "GnuBookTiff");
114     
115     if (1 != $scale) {
116         $pbmReduce = ' | pbmreduce -threshold ' . $scale;
117     } else {
118         $pbmReduce = '';
119     }
120     
121     $decompressCmd = 
122         ' > ' . $tempFile . ' ; tifftopnm ' . $tempFile . ' 2>/dev/null' . $pbmReduce;
123
124 } else {
125     GBfatal('Unknown source file extension: ' . $fileExt);
126 }
127        
128 // Non-integer scaling is currently disabled on the cluster
129 // if (isset($_REQUEST['height'])) {
130 //     $cmd .= " | pnmscale -height {$_REQUEST['height']} ";
131 // }
132
133 if ('jpg' == $ext) {
134     $compressCmd = ' | pnmtojpeg -quality 90';
135 } else if ('png' == $ext) {
136     $compressCmd = ' | pnmtopng $pngOptions';
137 }
138
139 $cmd = $unzipCmd . $decompressCmd . $compressCmd;
140
141 //print $cmd;
142
143 header('Content-type: ' . $MIMES[$ext]);
144 header('Cache-Control: max-age=15552000');
145
146 passthru ($cmd);
147
148 if (isset($tempFile)) {
149   unlink($tempFile);
150 }
151
152 function GBFatal($string) {
153     echo "alert('$string')\n";
154     die(-1);
155 }
156
157 ?>
158