4 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
6 This file is part of GnuBook.
8 GnuBook 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 GnuBook 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 GnuBook. If not, see <http://www.gnu.org/licenses/>.
22 $MIMES = array('jpg' => 'image/jpeg',
23 'png' => 'image/png');
25 $zipPath = $_REQUEST['zip'];
26 $file = $_REQUEST['file'];
28 // Unfortunately kakadu requires us to know a priori if the
29 // output file should be .ppm or .pgm. By decompressing to
30 // .bmp kakadu will write a file we can consistently turn into
31 // .pnm. Really kakadu should support .pnm as the file output
32 // extension and automatically write ppm or pgm format as
34 $decompressToBmp = true;
35 if ($decompressToBmp) {
36 $stdoutLink = '/tmp/stdout.bmp';
38 $stdoutLink = '/tmp/stdout.ppm';
41 if (isset($_REQUEST['ext'])) {
42 $ext = $_REQUEST['ext'];
48 $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
50 // Rotate is currently only supported for jp2 since it does not add server load
51 $allowedRotations = array("0", "90", "180", "270");
52 $rotate = $_REQUEST['rotate'];
53 if ( !in_array($rotate, $allowedRotations) ) {
57 // Image conversion options
59 $jpegOptions = '-quality 75';
61 // The pbmreduce reduction factor produces an image with dimension 1/n
62 // The kakadu reduction factor produceds an image with dimension 1/(2^n)
64 if (isset($_REQUEST['height'])) {
65 $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
69 } else if ($ratio <= 4) {
73 //$powReduce = 3; //too blurry!
79 $scale = $_REQUEST['scale'];
83 } else if (2 == $scale) {
85 } else if (4 == $scale) {
87 } else if (8 == $scale) {
89 } else if (16 == $scale) {
91 } else if (32 == $scale) {
94 // $$$ Leaving this in as default though I'm not sure why it is...
100 if (!file_exists($stdoutLink))
102 system('ln -s /dev/stdout ' . $stdoutLink);
106 putenv('LD_LIBRARY_PATH=/petabox/sw/lib/kakadu');
108 $unzipCmd = 'unzip -p ' .
109 escapeshellarg($zipPath) .
110 ' ' . escapeshellarg($file);
112 if ('jp2' == $fileExt) {
114 " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $powReduce -rotate $rotate -i /dev/stdin -o " . $stdoutLink;
115 if ($decompressToBmp) {
116 $decompressCmd .= ' | bmptopnm ';
119 } else if ('tif' == $fileExt) {
120 // We need to create a temporary file for tifftopnm since it cannot
121 // work on a pipe (the file must be seekable).
122 // We use the GnuBookTiff prefix to give a hint in case things don't
124 $tempFile = tempnam("/tmp", "GnuBookTiff");
128 $pbmReduce = ' | pnmscale -reduce ' . $scale;
130 $pbmReduce = ' | pnmscale -nomix -reduce ' . $scale;
137 ' > ' . $tempFile . ' ; tifftopnm ' . $tempFile . ' 2>/dev/null' . $pbmReduce;
140 GBfatal('Unknown source file extension: ' . $fileExt);
143 // Non-integer scaling is currently disabled on the cluster
144 // if (isset($_REQUEST['height'])) {
145 // $cmd .= " | pnmscale -height {$_REQUEST['height']} ";
149 $compressCmd = ' | pnmtojpeg ' . $jpegOptions;
150 } else if ('png' == $ext) {
151 $compressCmd = ' | pnmtopng ' . $pngOptions;
154 $cmd = $unzipCmd . $decompressCmd . $compressCmd;
158 header('Content-type: ' . $MIMES[$ext]);
159 header('Cache-Control: max-age=15552000');
163 if (isset($tempFile)) {
167 function GBFatal($string) {
168 echo "alert('$string')\n";
172 // Returns true if using a power node
173 function onPowerNode() {
174 exec("lspci | fgrep -c Realtek", $output, $return);
175 if ("0" != $output[0]) {
178 exec("egrep -q AMD /proc/cpuinfo", $output, $return);