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 // Image conversion options
52 $jpegOptions = '-quality 75';
54 // The pbmreduce reduction factor produces an image with dimension 1/n
55 // The kakadu reduction factor produceds an image with dimension 1/(2^n)
57 if (isset($_REQUEST['height'])) {
58 $ratio = floatval($_REQUEST['origHeight']) / floatval($_REQUEST['height']);
62 } else if ($ratio <= 4) {
66 //$powReduce = 3; //too blurry!
72 $scale = $_REQUEST['scale'];
76 } else if (2 == $scale) {
78 } else if (4 == $scale) {
80 } else if (8 == $scale) {
82 } else if (16 == $scale) {
84 } else if (32 == $scale) {
87 // $$$ Leaving this in as default though I'm not sure why it is...
93 if (!file_exists($stdoutLink))
95 system('ln -s /dev/stdout ' . $stdoutLink);
99 putenv('LD_LIBRARY_PATH=/petabox/sw/lib/kakadu');
101 $unzipCmd = 'unzip -p ' .
102 escapeshellarg($zipPath) .
103 ' ' . escapeshellarg($file);
105 if ('jp2' == $fileExt) {
107 " | /petabox/sw/bin/kdu_expand -no_seek -quiet -reduce $powReduce -i /dev/stdin -o " . $stdoutLink;
108 if ($decompressToBmp) {
109 $decompressCmd .= ' | bmptopnm ';
111 } else if ('tif' == $fileExt) {
112 // We need to create a temporary file for tifftopnm since it cannot
113 // work on a pipe (the file must be seekable).
114 // We use the GnuBookTiff prefix to give a hint in case things don't
116 $tempFile = tempnam("/tmp", "GnuBookTiff");
120 $pbmReduce = ' | pnmscale -reduce ' . $scale;
122 $pbmReduce = ' | pnmscale -nomix -reduce ' . $scale;
129 ' > ' . $tempFile . ' ; tifftopnm ' . $tempFile . ' 2>/dev/null' . $pbmReduce;
132 GBfatal('Unknown source file extension: ' . $fileExt);
135 // Non-integer scaling is currently disabled on the cluster
136 // if (isset($_REQUEST['height'])) {
137 // $cmd .= " | pnmscale -height {$_REQUEST['height']} ";
141 $compressCmd = ' | pnmtojpeg ' . $jpegOptions;
142 } else if ('png' == $ext) {
143 $compressCmd = ' | pnmtopng ' . $pngOptions;
146 $cmd = $unzipCmd . $decompressCmd . $compressCmd;
150 header('Content-type: ' . $MIMES[$ext]);
151 header('Cache-Control: max-age=15552000');
155 if (isset($tempFile)) {
159 function GBFatal($string) {
160 echo "alert('$string')\n";
164 // Returns true if using a power node
165 function onPowerNode() {
166 exec("lspci | fgrep -c Realtek", $output, $return);
167 if ("0" != $output[0]) {
170 exec("egrep -q AMD /proc/cpuinfo", $output, $return);