2 /* This file is deprecated -- image stack processing has been
3 * combined into GnuBookImages.php. This file will eventually
8 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
10 This file is part of GnuBook.
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.
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.
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/>.
26 $MIMES = array('jpg' => 'image/jpeg',
27 'png' => 'image/png');
29 $zipPath = $_REQUEST['zip'];
30 $file = $_REQUEST['file'];
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
38 $decompressToBmp = true;
39 if ($decompressToBmp) {
40 $stdoutLink = '/tmp/stdout.bmp';
42 $stdoutLink = '/tmp/stdout.ppm';
45 if (isset($_REQUEST['ext'])) {
46 $ext = $_REQUEST['ext'];
52 $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
54 // Png conversion options
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)
60 if (isset($_REQUEST['height'])) {
61 $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
65 } else if ($ratio <= 4) {
69 //$reduce = 3; //too blurry!
75 $scale = $_REQUEST['scale'];
79 } else if (2 == $scale) {
81 } else if (4 == $scale) {
84 // $$$ why do we default to such a small scale?
90 if (!file_exists($stdoutLink))
92 system('ln -s /dev/stdout ' . $stdoutLink);
96 putenv('LD_LIBRARY_PATH=/petabox/sw/lib/kakadu');
98 $unzipCmd = 'unzip -p ' .
99 escapeshellarg($zipPath) .
100 ' ' . escapeshellarg($file);
102 if ('jp2' == $fileExt) {
104 " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $reduce -i /dev/stdin -o " . $stdoutLink;
105 if ($decompressToBmp) {
106 $decompressCmd .= ' | bmptopnm ';
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
113 $tempFile = tempnam("/tmp", "GnuBookTiff");
116 $pbmReduce = ' | pbmreduce -threshold ' . $scale;
122 ' > ' . $tempFile . ' ; tifftopnm ' . $tempFile . ' 2>/dev/null' . $pbmReduce;
125 GBfatal('Unknown source file extension: ' . $fileExt);
128 // Non-integer scaling is currently disabled on the cluster
129 // if (isset($_REQUEST['height'])) {
130 // $cmd .= " | pnmscale -height {$_REQUEST['height']} ";
134 $compressCmd = ' | pnmtojpeg -quality 90';
135 } else if ('png' == $ext) {
136 $compressCmd = ' | pnmtopng $pngOptions';
139 $cmd = $unzipCmd . $decompressCmd . $compressCmd;
143 header('Content-type: ' . $MIMES[$ext]);
144 header('Cache-Control: max-age=15552000');
148 if (isset($tempFile)) {
152 function GBFatal($string) {
153 echo "alert('$string')\n";