4 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
6 This file is part of BookReader.
8 BookReader is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 BookReader is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with BookReader. If not, see <http://www.gnu.org/licenses/>.
22 $MIMES = array('jpg' => 'image/jpeg',
23 'png' => 'image/png');
25 $exiftool = '/petabox/sw/books/exiftool/exiftool';
27 $zipPath = $_REQUEST['zip'];
28 $file = $_REQUEST['file'];
33 * Get info about requested image (input)
34 * Get info about requested output format
35 * Determine processing parameters
38 * Clean up temporary files
41 function getUnzipCommand($zipPath, $file)
44 escapeshellarg($zipPath) .
45 ' ' . escapeshellarg($file);
49 * Get the image width, height and depth from a jp2 file in zip.
51 function getImageSizeAndDepth($zipPath, $file)
55 # $$$ will exiftool work for *all* of our images?
56 $cmd = getUnzipCommand($zipPath, $file)
57 . ' | '. $exiftool . ' -s -s -s -ImageWidth -ImageHeight -BitsPerComponent -';
60 preg_match('/^(\d+)/', $output[2], $groups);
61 $bits = intval($groups[1]);
63 $retval = Array('width' => intval($output[0]), 'height' => intval($output[1]),
68 // Unfortunately kakadu requires us to know a priori if the
69 // output file should be .ppm or .pgm. By decompressing to
70 // .bmp kakadu will write a file we can consistently turn into
71 // .pnm. Really kakadu should support .pnm as the file output
72 // extension and automatically write ppm or pgm format as
74 $decompressToBmp = true;
75 if ($decompressToBmp) {
76 $stdoutLink = '/tmp/stdout.bmp';
78 $stdoutLink = '/tmp/stdout.ppm';
81 if (isset($_REQUEST['ext'])) {
82 $ext = $_REQUEST['ext'];
88 $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
90 // Rotate is currently only supported for jp2 since it does not add server load
91 $allowedRotations = array("0", "90", "180", "270");
92 $rotate = $_REQUEST['rotate'];
93 if ( !in_array($rotate, $allowedRotations) ) {
97 // Image conversion options
99 $jpegOptions = '-quality 75';
101 // The pbmreduce reduction factor produces an image with dimension 1/n
102 // The kakadu reduction factor produceds an image with dimension 1/(2^n)
104 if (isset($_REQUEST['height'])) {
105 $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
109 } else if ($ratio <= 4) {
113 //$powReduce = 3; //too blurry!
119 $scale = $_REQUEST['scale'];
123 } else if (2 == $scale) {
125 } else if (4 == $scale) {
127 } else if (8 == $scale) {
129 } else if (16 == $scale) {
131 } else if (32 == $scale) {
134 // $$$ Leaving this in as default though I'm not sure why it is...
140 if (!file_exists($stdoutLink))
142 system('ln -s /dev/stdout ' . $stdoutLink);
146 putenv('LD_LIBRARY_PATH=/petabox/sw/lib/kakadu');
148 $unzipCmd = getUnzipCommand($zipPath, $file);
150 if ('jp2' == $fileExt) {
152 " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $powReduce -rotate $rotate -i /dev/stdin -o " . $stdoutLink;
153 if ($decompressToBmp) {
154 $decompressCmd .= ' | bmptopnm ';
157 } else if ('tif' == $fileExt) {
158 // We need to create a temporary file for tifftopnm since it cannot
159 // work on a pipe (the file must be seekable).
160 // We use the BookReaderTiff prefix to give a hint in case things don't
162 $tempFile = tempnam("/tmp", "BookReaderTiff");
166 $pbmReduce = ' | pnmscale -reduce ' . $scale;
168 $pbmReduce = ' | pnmscale -nomix -reduce ' . $scale;
175 ' > ' . $tempFile . ' ; tifftopnm ' . $tempFile . ' 2>/dev/null' . $pbmReduce;
178 BRfatal('Unknown source file extension: ' . $fileExt);
181 // Non-integer scaling is currently disabled on the cluster
182 // if (isset($_REQUEST['height'])) {
183 // $cmd .= " | pnmscale -height {$_REQUEST['height']} ";
187 $compressCmd = ' | pnmtojpeg ' . $jpegOptions;
188 } else if ('png' == $ext) {
189 $compressCmd = ' | pnmtopng ' . $pngOptions;
192 $cmd = $unzipCmd . $decompressCmd . $compressCmd;
197 header('Content-type: ' . $MIMES[$ext]);
198 header('Cache-Control: max-age=15552000');
199 passthru ($cmd); # cmd returns image data
201 if (isset($tempFile)) {
205 function BRFatal($string) {
206 echo "alert('$string')\n";
210 // Returns true if using a power node
211 function onPowerNode() {
212 exec("lspci | fgrep -c Realtek", $output, $return);
213 if ("0" != $output[0]) {
216 exec("egrep -q AMD /proc/cpuinfo", $output, $return);