Index: trunk/extensions/PagedTiffHandler/TiffReader.php |
— | — | @@ -7,341 +7,341 @@ |
8 | 8 | |
9 | 9 | // This is still experimental |
10 | 10 | class TiffReader { |
11 | | - protected $time = NULL; |
12 | | - protected $file = NULL; |
13 | | - protected $file_handle = NULL; |
14 | | - protected $order = NULL; |
15 | | - protected $the_answer = NULL; |
16 | | - protected $embed_files = 0; |
17 | | - protected $ifd_offsets = array(); |
18 | | - protected $real_eof = 0; |
19 | | - protected $highest_addr = 0; |
| 11 | + protected $time = NULL; |
| 12 | + protected $file = NULL; |
| 13 | + protected $file_handle = NULL; |
| 14 | + protected $order = NULL; |
| 15 | + protected $the_answer = NULL; |
| 16 | + protected $embed_files = 0; |
| 17 | + protected $ifd_offsets = array(); |
| 18 | + protected $real_eof = 0; |
| 19 | + protected $highest_addr = 0; |
20 | 20 | |
21 | | - protected $unknown_fields = false; |
| 21 | + protected $unknown_fields = false; |
22 | 22 | |
23 | | - protected $hex = NULL; |
24 | | - protected $short = NULL; |
25 | | - protected $long = NULL; |
| 23 | + protected $hex = NULL; |
| 24 | + protected $short = NULL; |
| 25 | + protected $long = NULL; |
26 | 26 | |
27 | | - public function __construct($file) { |
28 | | - $this->time = microtime(true); |
29 | | - $this->file = $file; |
30 | | - // set file-pointer |
31 | | - $this->file_handle = fopen($this->file, 'rb'); |
32 | | - // read the tiff-header |
33 | | - $this->order = (fread($this->file_handle, 2) == 'II'); |
34 | | - if($this->order) { |
35 | | - $this->hex = 'h*'; |
36 | | - $this->short = 'v*'; |
37 | | - $this->long = 'V*'; |
38 | | - } |
39 | | - else { |
40 | | - $this->hex = 'H*'; |
41 | | - $this->short = 'n*'; |
42 | | - $this->long = 'N*'; |
43 | | - } |
44 | | - $this->the_answer = unpack($this->short, fread($this->file_handle, 2)); |
45 | | - // set the offset of the first ifd |
46 | | - $offset = unpack($this->long, fread($this->file_handle, 4)); |
47 | | - $this->ifd_offsets[]['offset'] = $offset[1]; |
48 | | - fseek($this->file_handle, 0, SEEK_END); |
49 | | - $this->real_eof = ftell($this->file_handle); |
50 | | - } |
| 27 | + public function __construct( $file ) { |
| 28 | + $this->time = microtime( true ); |
| 29 | + $this->file = $file; |
| 30 | + // set file-pointer |
| 31 | + $this->file_handle = fopen( $this->file, 'rb' ); |
| 32 | + // read the tiff-header |
| 33 | + $this->order = ( fread( $this->file_handle, 2 ) == 'II' ); |
| 34 | + if ( $this->order ) { |
| 35 | + $this->hex = 'h*'; |
| 36 | + $this->short = 'v*'; |
| 37 | + $this->long = 'V*'; |
| 38 | + } |
| 39 | + else { |
| 40 | + $this->hex = 'H*'; |
| 41 | + $this->short = 'n*'; |
| 42 | + $this->long = 'N*'; |
| 43 | + } |
| 44 | + $this->the_answer = unpack( $this->short, fread( $this->file_handle, 2 ) ); |
| 45 | + // set the offset of the first ifd |
| 46 | + $offset = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 47 | + $this->ifd_offsets[]['offset'] = $offset[1]; |
| 48 | + fseek( $this->file_handle, 0, SEEK_END ); |
| 49 | + $this->real_eof = ftell( $this->file_handle ); |
| 50 | + } |
51 | 51 | |
52 | | - public function checkScriptAtEnd($size = 1) { |
53 | | - $size = $size * 1024 * 1024; |
54 | | - if($size > $this->real_eof) { |
55 | | - fseek($this->file_handle, 0, SEEK_SET); |
56 | | - $chunk = fread($this->file_handle, $this->real_eof); |
57 | | - } |
58 | | - else { |
59 | | - fseek($this->file_handle, ($this->real_eof - $size), SEEK_SET); |
60 | | - $chunk = fread($this->file_handle, $size); |
61 | | - } |
62 | | - #check for HTML doctype |
63 | | - if (preg_match('/<!DOCTYPE *X?HTML/', $chunk)) return true; |
| 52 | + public function checkScriptAtEnd( $size = 1 ) { |
| 53 | + $size = $size * 1024 * 1024; |
| 54 | + if ( $size > $this->real_eof ) { |
| 55 | + fseek( $this->file_handle, 0, SEEK_SET ); |
| 56 | + $chunk = fread( $this->file_handle, $this->real_eof ); |
| 57 | + } |
| 58 | + else { |
| 59 | + fseek( $this->file_handle, ( $this->real_eof - $size ), SEEK_SET ); |
| 60 | + $chunk = fread( $this->file_handle, $size ); |
| 61 | + } |
| 62 | + # check for HTML doctype |
| 63 | + if ( preg_match( '/<!DOCTYPE *X?HTML/', $chunk ) ) return true; |
64 | 64 | |
65 | | - $tags = array('<a href', |
66 | | - '<body', |
67 | | - '<head', |
68 | | - '<html', |
69 | | - '<img', |
70 | | - '<pre', |
71 | | - '<script', |
72 | | - '<table', |
73 | | - '<title'); |
74 | | - foreach( $tags as $tag ) { |
75 | | - if( false !== strpos( $chunk, $tag ) ) { |
76 | | - return true; |
77 | | - } |
78 | | - } |
79 | | - #look for script-types |
80 | | - if (preg_match('!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim',$chunk)) return true; |
| 65 | + $tags = array( '<a href', |
| 66 | + '<body', |
| 67 | + '<head', |
| 68 | + '<html', |
| 69 | + '<img', |
| 70 | + '<pre', |
| 71 | + '<script', |
| 72 | + '<table', |
| 73 | + '<title' ); |
| 74 | + foreach ( $tags as $tag ) { |
| 75 | + if ( false !== strpos( $chunk, $tag ) ) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + } |
| 79 | + # look for script-types |
| 80 | + if ( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) ) return true; |
81 | 81 | |
82 | | - #look for html-style script-urls |
83 | | - if (preg_match('!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim',$chunk)) return true; |
| 82 | + # look for html-style script-urls |
| 83 | + if ( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) return true; |
84 | 84 | |
85 | | - #look for css-style script-urls |
86 | | - if (preg_match('!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim',$chunk)) return true; |
87 | | - } |
| 85 | + # look for css-style script-urls |
| 86 | + if ( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) return true; |
| 87 | + } |
88 | 88 | |
89 | | - public function checkSize() { |
90 | | - $diff = $this->real_eof - $this->highest_addr; |
91 | | - if($diff) { |
92 | | - if($diff < 0) { |
93 | | - return true; |
94 | | - } |
95 | | - fseek($this->file_handle, $this->highest_addr, SEEK_SET); |
96 | | - $diffstr = fread($this->file_handle, $diff); |
97 | | - if(preg_match('/^\0+$/', $diffstr)) { |
98 | | - return true; |
99 | | - } |
100 | | - return false; |
101 | | - } |
102 | | - return true; |
103 | | - } |
| 89 | + public function checkSize() { |
| 90 | + $diff = $this->real_eof - $this->highest_addr; |
| 91 | + if ( $diff ) { |
| 92 | + if ( $diff < 0 ) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + fseek( $this->file_handle, $this->highest_addr, SEEK_SET ); |
| 96 | + $diffstr = fread( $this->file_handle, $diff ); |
| 97 | + if ( preg_match( '/^\0+$/', $diffstr ) ) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + return false; |
| 101 | + } |
| 102 | + return true; |
| 103 | + } |
104 | 104 | |
105 | | - public function isValidTiff() { |
106 | | - return $this->the_answer[1] == 42; |
107 | | - } |
| 105 | + public function isValidTiff() { |
| 106 | + return $this->the_answer[1] == 42; |
| 107 | + } |
108 | 108 | |
109 | | - public function check($debug = false) { |
110 | | - $offset = $this->ifd_offsets[$this->embed_files]['offset']; |
111 | | - $rounds = 0; |
112 | | - // loop over all ifd |
113 | | - while($offset && $offset <= $this->real_eof) { |
114 | | - // save the offset if it is the highest one |
115 | | - if($offset > $this->highest_addr) { |
116 | | - $this->highest_addr = $offset; |
117 | | - } |
118 | | - // set file-pointer to address with given offset |
119 | | - fseek($this->file_handle, $offset, SEEK_SET); |
120 | | - // read amount of ifd-entries |
121 | | - $entries = unpack($this->short, fread($this->file_handle, 2)); |
122 | | - if(!is_array($entries) || !isset($entries[1])) { |
| 109 | + public function check( $debug = false ) { |
| 110 | + $offset = $this->ifd_offsets[$this->embed_files]['offset']; |
| 111 | + $rounds = 0; |
| 112 | + // loop over all ifd |
| 113 | + while ( $offset && $offset <= $this->real_eof ) { |
| 114 | + // save the offset if it is the highest one |
| 115 | + if ( $offset > $this->highest_addr ) { |
| 116 | + $this->highest_addr = $offset; |
| 117 | + } |
| 118 | + // set file-pointer to address with given offset |
| 119 | + fseek( $this->file_handle, $offset, SEEK_SET ); |
| 120 | + // read amount of ifd-entries |
| 121 | + $entries = unpack( $this->short, fread( $this->file_handle, 2 ) ); |
| 122 | + if ( !is_array( $entries ) || !isset( $entries[1] ) ) { |
123 | 123 | $this->the_answer = 0; |
124 | 124 | return false; |
125 | 125 | } |
126 | | - $entries = $entries[1]; |
| 126 | + $entries = $entries[1]; |
127 | 127 | |
128 | | - $address = $offset + 2 + ($entries * 12); |
129 | | - if($address > $this->highest_addr) { |
130 | | - $this->highest_addr = $address; |
131 | | - } |
| 128 | + $address = $offset + 2 + ( $entries * 12 ); |
| 129 | + if ( $address > $this->highest_addr ) { |
| 130 | + $this->highest_addr = $address; |
| 131 | + } |
132 | 132 | |
133 | | - // run through all entries of this ifd an read them out |
134 | | - for($i = 0; $i < $entries; $i++) { |
135 | | - $tmp = $this->readIFDEntry(); |
136 | | - if(!$tmp) { return false; } |
137 | | - $this->ifd_offsets[$this->embed_files]['data'][$tmp['tag']] = $tmp; |
138 | | - } |
| 133 | + // run through all entries of this ifd an read them out |
| 134 | + for ( $i = 0; $i < $entries; $i++ ) { |
| 135 | + $tmp = $this->readIFDEntry(); |
| 136 | + if ( !$tmp ) { return false; } |
| 137 | + $this->ifd_offsets[$this->embed_files]['data'][$tmp['tag']] = $tmp; |
| 138 | + } |
139 | 139 | |
140 | | - // set the offset of the next ifd or null if this is the last one |
141 | | - $offset = unpack($this->long, fread($this->file_handle, 4)); |
142 | | - if(!is_array($offset) || !isset($offset[1])) { |
| 140 | + // set the offset of the next ifd or null if this is the last one |
| 141 | + $offset = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 142 | + if ( !is_array( $offset ) || !isset( $offset[1] ) ) { |
143 | 143 | $this->the_answer = 0; |
144 | 144 | return false; |
145 | 145 | } |
146 | | - $offset = $offset[1]; |
147 | | - if($offset) { |
148 | | - $this->ifd_offsets[]['offset'] = $offset; |
149 | | - } |
150 | | - $this->embed_files++; |
151 | | - } |
152 | | - $this->calculateDataRange(); |
153 | | - |
154 | | - if($debug) { |
155 | | - echo "<h2>TiffReader-Debug:</h2>\n"; |
156 | | - echo "<b>File: </b>".$this->file."<br />\n"; |
157 | | - if($this->order) { |
158 | | - echo "<b>Byte-Order: </b>little Endian<br />\n"; |
159 | | - } |
160 | | - else { |
161 | | - echo "<b>Byte-Order: </b>big Endian<br />\n"; |
162 | | - } |
163 | | - echo "<b>Valid Tiff: </b>"; |
164 | | - if($this->the_answer[1] == 42) { |
165 | | - echo "yes<br />\n"; |
166 | | - } |
167 | | - else { |
168 | | - echo "no<br />\n"; |
169 | | - } |
170 | | - echo "<b>Physicaly Size: </b>".$this->real_eof." bytes<br />\n"; |
171 | | - echo "<b>Calculated Size: </b>".$this->highest_addr." bytes<br />\n"; |
172 | | - echo "<b>Difference in Size: </b>".($this->real_eof - $this->highest_addr)." bytes<br />\n"; |
173 | | - echo "<b>Unknown Fields: </b>"; |
174 | | - if($this->unknown_fields) { |
175 | | - echo "yes<br />\n"; |
176 | | - } |
177 | | - else { |
178 | | - echo "no<br />\n"; |
179 | | - } |
180 | | - echo "<b>Embed Files: </b>".$this->embed_files."<br />\n"; |
181 | | - echo "<b>Runtime: </b>".round((microtime(true) - $this->time), 6)." seconds<br />\n"; |
182 | | - } |
183 | | - } |
| 146 | + $offset = $offset[1]; |
| 147 | + if ( $offset ) { |
| 148 | + $this->ifd_offsets[]['offset'] = $offset; |
| 149 | + } |
| 150 | + $this->embed_files++; |
| 151 | + } |
| 152 | + $this->calculateDataRange(); |
| 153 | + |
| 154 | + if ( $debug ) { |
| 155 | + echo "<h2>TiffReader-Debug:</h2>\n"; |
| 156 | + echo "<b>File: </b>" . $this->file . "<br />\n"; |
| 157 | + if ( $this->order ) { |
| 158 | + echo "<b>Byte-Order: </b>little Endian<br />\n"; |
| 159 | + } |
| 160 | + else { |
| 161 | + echo "<b>Byte-Order: </b>big Endian<br />\n"; |
| 162 | + } |
| 163 | + echo "<b>Valid Tiff: </b>"; |
| 164 | + if ( $this->the_answer[1] == 42 ) { |
| 165 | + echo "yes<br />\n"; |
| 166 | + } |
| 167 | + else { |
| 168 | + echo "no<br />\n"; |
| 169 | + } |
| 170 | + echo "<b>Physicaly Size: </b>" . $this->real_eof . " bytes<br />\n"; |
| 171 | + echo "<b>Calculated Size: </b>" . $this->highest_addr . " bytes<br />\n"; |
| 172 | + echo "<b>Difference in Size: </b>" . ( $this->real_eof - $this->highest_addr ) . " bytes<br />\n"; |
| 173 | + echo "<b>Unknown Fields: </b>"; |
| 174 | + if ( $this->unknown_fields ) { |
| 175 | + echo "yes<br />\n"; |
| 176 | + } |
| 177 | + else { |
| 178 | + echo "no<br />\n"; |
| 179 | + } |
| 180 | + echo "<b>Embed Files: </b>" . $this->embed_files . "<br />\n"; |
| 181 | + echo "<b>Runtime: </b>" . round( ( microtime( true ) - $this->time ), 6 ) . " seconds<br />\n"; |
| 182 | + } |
| 183 | + } |
184 | 184 | |
185 | | - protected function readIFDEntry() { |
186 | | - $tag = unpack($this->short, fread($this->file_handle, 2)); |
187 | | - $type = unpack($this->short, fread($this->file_handle, 2)); |
188 | | - $count = unpack($this->long, fread($this->file_handle, 4)); |
189 | | - $value = unpack($this->long, fread($this->file_handle, 4)); |
190 | | - if(!is_array($tag) || !is_array($type) || !is_array($count) || !is_array($value) || |
191 | | - !isset($tag[1]) || !isset($type[1]) || !isset($count[1]) || !isset($value[1])) { |
| 185 | + protected function readIFDEntry() { |
| 186 | + $tag = unpack( $this->short, fread( $this->file_handle, 2 ) ); |
| 187 | + $type = unpack( $this->short, fread( $this->file_handle, 2 ) ); |
| 188 | + $count = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 189 | + $value = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 190 | + if ( !is_array( $tag ) || !is_array( $type ) || !is_array( $count ) || !is_array( $value ) || |
| 191 | + !isset( $tag[1] ) || !isset( $type[1] ) || !isset( $count[1] ) || !isset( $value[1] ) ) { |
192 | 192 | $this->the_answer = 0; |
193 | 193 | return false; |
194 | 194 | } |
195 | | - return array('tag' => $tag[1], |
196 | | - 'type' => $type[1], |
197 | | - 'count' => $count[1], |
198 | | - 'value' => $value[1]); |
199 | | - } |
| 195 | + return array( 'tag' => $tag[1], |
| 196 | + 'type' => $type[1], |
| 197 | + 'count' => $count[1], |
| 198 | + 'value' => $value[1] ); |
| 199 | + } |
200 | 200 | |
201 | | - protected function calculateDataRange() { |
202 | | - foreach($this->ifd_offsets as $number => $ifd) { |
203 | | - foreach($ifd['data'] as $tag => $data) { |
204 | | - // ignore all entries with local values |
205 | | - if(($data['type'] == 1 && $data['count'] <= 4) || |
206 | | - ($data['type'] == 2 && $data['count'] <= 4) || |
207 | | - ($data['type'] == 3 && $data['count'] <= 2) || |
208 | | - ($data['type'] == 4 && $data['count'] <= 1) || |
209 | | - ($data['type'] == 6 && $data['count'] <= 4) || |
210 | | - ($data['type'] == 7 && $data['count'] <= 4) || |
211 | | - ($data['type'] == 8 && $data['count'] <= 2) || |
212 | | - ($data['type'] == 9 && $data['count'] <= 1)) { |
213 | | - continue; |
214 | | - } |
215 | | - // set value size |
216 | | - switch($data['type']) { |
217 | | - case 1: |
218 | | - case 2: |
219 | | - case 6: |
220 | | - case 7: |
221 | | - $size = 1; |
222 | | - break; |
223 | | - case 3: |
224 | | - case 8: |
225 | | - $size = 2; |
226 | | - break; |
227 | | - case 4: |
228 | | - case 9: |
229 | | - case 11: |
230 | | - $size = 4; |
231 | | - break; |
232 | | - case 5: |
233 | | - case 10; |
234 | | - case 12: |
235 | | - $size = 8; |
236 | | - break; |
237 | | - default: |
238 | | - $size = 4; |
239 | | - $this->unknown_fields = true; |
240 | | - break; |
241 | | - } |
242 | | - // calculate the range of memory, the data need |
243 | | - $size = $data['value'] + ($size * $data['count']); |
244 | | - if($size > $this->highest_addr) { |
245 | | - $this->highest_addr = $size; |
246 | | - } |
247 | | - } |
248 | | - // check if more calculations needed |
249 | | - if($this->highest_addr == $this->real_eof) { |
250 | | - break; |
251 | | - } |
252 | | - // check if image data have to calculate |
253 | | - if(isset($ifd['data'][273]) && isset($ifd['data'][279])) { |
254 | | - // set file pointer to the offset for values from field 273 |
255 | | - fseek($this->file_handle, $ifd['data'][273]['value'], SEEK_SET); |
256 | | - // get all offsets of the ImageStripes |
257 | | - $stripes = array(); |
258 | | - if($ifd['data'][273]['type'] == 3) { |
259 | | - for($i = 0; $i < $ifd['data'][273]['count']; $i++) { |
260 | | - $stripes[] = unpack($this->short, fread($this->file_handle, 2)); |
261 | | - } |
262 | | - } |
263 | | - else { |
264 | | - for($i = 0; $i < $ifd['data'][273]['count']; $i++) { |
265 | | - $stripes[] = unpack($this->long, fread($this->file_handle, 4)); |
266 | | - } |
267 | | - } |
| 201 | + protected function calculateDataRange() { |
| 202 | + foreach ( $this->ifd_offsets as $number => $ifd ) { |
| 203 | + foreach ( $ifd['data'] as $tag => $data ) { |
| 204 | + // ignore all entries with local values |
| 205 | + if ( ( $data['type'] == 1 && $data['count'] <= 4 ) || |
| 206 | + ( $data['type'] == 2 && $data['count'] <= 4 ) || |
| 207 | + ( $data['type'] == 3 && $data['count'] <= 2 ) || |
| 208 | + ( $data['type'] == 4 && $data['count'] <= 1 ) || |
| 209 | + ( $data['type'] == 6 && $data['count'] <= 4 ) || |
| 210 | + ( $data['type'] == 7 && $data['count'] <= 4 ) || |
| 211 | + ( $data['type'] == 8 && $data['count'] <= 2 ) || |
| 212 | + ( $data['type'] == 9 && $data['count'] <= 1 ) ) { |
| 213 | + continue; |
| 214 | + } |
| 215 | + // set value size |
| 216 | + switch( $data['type'] ) { |
| 217 | + case 1: |
| 218 | + case 2: |
| 219 | + case 6: |
| 220 | + case 7: |
| 221 | + $size = 1; |
| 222 | + break; |
| 223 | + case 3: |
| 224 | + case 8: |
| 225 | + $size = 2; |
| 226 | + break; |
| 227 | + case 4: |
| 228 | + case 9: |
| 229 | + case 11: |
| 230 | + $size = 4; |
| 231 | + break; |
| 232 | + case 5: |
| 233 | + case 10; |
| 234 | + case 12: |
| 235 | + $size = 8; |
| 236 | + break; |
| 237 | + default: |
| 238 | + $size = 4; |
| 239 | + $this->unknown_fields = true; |
| 240 | + break; |
| 241 | + } |
| 242 | + // calculate the range of memory, the data need |
| 243 | + $size = $data['value'] + ( $size * $data['count'] ); |
| 244 | + if ( $size > $this->highest_addr ) { |
| 245 | + $this->highest_addr = $size; |
| 246 | + } |
| 247 | + } |
| 248 | + // check if more calculations needed |
| 249 | + if ( $this->highest_addr == $this->real_eof ) { |
| 250 | + break; |
| 251 | + } |
| 252 | + // check if image data have to calculate |
| 253 | + if ( isset( $ifd['data'][273] ) && isset( $ifd['data'][279] ) ) { |
| 254 | + // set file pointer to the offset for values from field 273 |
| 255 | + fseek( $this->file_handle, $ifd['data'][273]['value'], SEEK_SET ); |
| 256 | + // get all offsets of the ImageStripes |
| 257 | + $stripes = array(); |
| 258 | + if ( $ifd['data'][273]['type'] == 3 ) { |
| 259 | + for ( $i = 0; $i < $ifd['data'][273]['count']; $i++ ) { |
| 260 | + $stripes[] = unpack( $this->short, fread( $this->file_handle, 2 ) ); |
| 261 | + } |
| 262 | + } |
| 263 | + else { |
| 264 | + for ( $i = 0; $i < $ifd['data'][273]['count']; $i++ ) { |
| 265 | + $stripes[] = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 266 | + } |
| 267 | + } |
268 | 268 | |
269 | | - // set file pointer to the offset for values from field 279 |
270 | | - fseek($this->file_handle, $ifd['data'][279]['value'], SEEK_SET); |
271 | | - // get all offsets of the StripeByteCounts |
272 | | - $stripebytes = array(); |
273 | | - if($ifd['data'][279]['type'] == 3) { |
274 | | - for($i = 0; $i < $ifd['data'][279]['count']; $i++) { |
275 | | - $stripebytes[] = unpack($this->short, fread($this->file_handle, 2)); |
276 | | - } |
277 | | - } |
278 | | - else { |
279 | | - for($i = 0; $i < $ifd['data'][279]['count']; $i++) { |
280 | | - $stripebytes[] = unpack($this->long, fread($this->file_handle, 4)); |
281 | | - } |
282 | | - } |
283 | | - // calculate the memory range of the image stripes |
284 | | - for($i = 0; $i < count($stripes); $i++) { |
285 | | - $size = $stripes[$i][1] + $stripebytes[$i][1]; |
286 | | - if($size > $this->highest_addr) { |
287 | | - $this->highest_addr = $size; |
288 | | - } |
289 | | - } |
290 | | - } |
291 | | - if(isset($ifd['data'][324]) && isset($ifd['data'][325])) { |
292 | | - // set file pointer to the offset for values from field 324 |
293 | | - fseek($this->file_handle, $ifd['data'][324]['value'], SEEK_SET); |
294 | | - // get all offsets of the ImageTiles |
295 | | - $tiles = array(); |
296 | | - for($i = 0; $i < $ifd['data'][324]['count']; $i++) { |
297 | | - $tiles[] = unpack($this->long, fread($this->file_handle, 4)); |
298 | | - } |
| 269 | + // set file pointer to the offset for values from field 279 |
| 270 | + fseek( $this->file_handle, $ifd['data'][279]['value'], SEEK_SET ); |
| 271 | + // get all offsets of the StripeByteCounts |
| 272 | + $stripebytes = array(); |
| 273 | + if ( $ifd['data'][279]['type'] == 3 ) { |
| 274 | + for ( $i = 0; $i < $ifd['data'][279]['count']; $i++ ) { |
| 275 | + $stripebytes[] = unpack( $this->short, fread( $this->file_handle, 2 ) ); |
| 276 | + } |
| 277 | + } |
| 278 | + else { |
| 279 | + for ( $i = 0; $i < $ifd['data'][279]['count']; $i++ ) { |
| 280 | + $stripebytes[] = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 281 | + } |
| 282 | + } |
| 283 | + // calculate the memory range of the image stripes |
| 284 | + for ( $i = 0; $i < count( $stripes ); $i++ ) { |
| 285 | + $size = $stripes[$i][1] + $stripebytes[$i][1]; |
| 286 | + if ( $size > $this->highest_addr ) { |
| 287 | + $this->highest_addr = $size; |
| 288 | + } |
| 289 | + } |
| 290 | + } |
| 291 | + if ( isset( $ifd['data'][324] ) && isset( $ifd['data'][325] ) ) { |
| 292 | + // set file pointer to the offset for values from field 324 |
| 293 | + fseek( $this->file_handle, $ifd['data'][324]['value'], SEEK_SET ); |
| 294 | + // get all offsets of the ImageTiles |
| 295 | + $tiles = array(); |
| 296 | + for ( $i = 0; $i < $ifd['data'][324]['count']; $i++ ) { |
| 297 | + $tiles[] = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 298 | + } |
299 | 299 | |
300 | | - // set file pointer to the offset for values from field 325 |
301 | | - fseek($this->file_handle, $ifd['data'][325]['value'], SEEK_SET); |
302 | | - // get all offsets of the TileByteCounts |
303 | | - $tilebytes = array(); |
304 | | - if($ifd['data'][325]['type'] == 3) { |
305 | | - for($i = 0; $i < $ifd['data'][325]['count']; $i++) { |
306 | | - $tilebytes[] = unpack($this->short, fread($this->file_handle, 2)); |
307 | | - } |
308 | | - } |
309 | | - else { |
310 | | - for($i = 0; $i < $ifd['data'][325]['count']; $i++) { |
311 | | - $tilebytes[] = unpack($this->long, fread($this->file_handle, 4)); |
312 | | - } |
313 | | - } |
314 | | - // calculate the memory range of the image tiles |
315 | | - for($i = 0; $i < count($tiles); $i++) { |
316 | | - $size = $tiles[$i][1] + $tilebytes[$i][1]; |
317 | | - if($size > $this->highest_addr) { |
318 | | - $this->highest_addr = $size; |
319 | | - } |
320 | | - } |
321 | | - } |
322 | | - if(isset($ifd['data'][288]) && isset($ifd['data'][289])) { |
323 | | - // set file pointer to the offset for values from field 288 |
324 | | - fseek($this->file_handle, $ifd['data'][288]['value'], SEEK_SET); |
325 | | - // get all offsets of the ImageTiles |
326 | | - $free = array(); |
327 | | - for($i = 0; $i < $ifd['data'][288]['count']; $i++) { |
328 | | - $free[] = unpack($this->long, fread($this->file_handle, 4)); |
329 | | - } |
| 300 | + // set file pointer to the offset for values from field 325 |
| 301 | + fseek( $this->file_handle, $ifd['data'][325]['value'], SEEK_SET ); |
| 302 | + // get all offsets of the TileByteCounts |
| 303 | + $tilebytes = array(); |
| 304 | + if ( $ifd['data'][325]['type'] == 3 ) { |
| 305 | + for ( $i = 0; $i < $ifd['data'][325]['count']; $i++ ) { |
| 306 | + $tilebytes[] = unpack( $this->short, fread( $this->file_handle, 2 ) ); |
| 307 | + } |
| 308 | + } |
| 309 | + else { |
| 310 | + for ( $i = 0; $i < $ifd['data'][325]['count']; $i++ ) { |
| 311 | + $tilebytes[] = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 312 | + } |
| 313 | + } |
| 314 | + // calculate the memory range of the image tiles |
| 315 | + for ( $i = 0; $i < count( $tiles ); $i++ ) { |
| 316 | + $size = $tiles[$i][1] + $tilebytes[$i][1]; |
| 317 | + if ( $size > $this->highest_addr ) { |
| 318 | + $this->highest_addr = $size; |
| 319 | + } |
| 320 | + } |
| 321 | + } |
| 322 | + if ( isset( $ifd['data'][288] ) && isset( $ifd['data'][289] ) ) { |
| 323 | + // set file pointer to the offset for values from field 288 |
| 324 | + fseek( $this->file_handle, $ifd['data'][288]['value'], SEEK_SET ); |
| 325 | + // get all offsets of the ImageTiles |
| 326 | + $free = array(); |
| 327 | + for ( $i = 0; $i < $ifd['data'][288]['count']; $i++ ) { |
| 328 | + $free[] = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 329 | + } |
330 | 330 | |
331 | | - // set file pointer to the offset for values from field 289 |
332 | | - fseek($this->file_handle, $ifd['data'][289]['value'], SEEK_SET); |
333 | | - // get all offsets of the TileByteCounts |
334 | | - $freebytes = array(); |
335 | | - for($i = 0; $i < $ifd['data'][289]['count']; $i++) { |
336 | | - $freebytes[] = unpack($this->long, fread($this->file_handle, 4)); |
337 | | - } |
338 | | - // calculate the memory range of the image tiles |
339 | | - for($i = 0; $i < count($tiles); $i++) { |
340 | | - $size = $free[$i][1] + $freebytes[$i][1]; |
341 | | - if($size > $this->highest_addr) { |
342 | | - $this->highest_addr = $size; |
343 | | - } |
344 | | - } |
345 | | - } |
346 | | - } |
347 | | - } |
| 331 | + // set file pointer to the offset for values from field 289 |
| 332 | + fseek( $this->file_handle, $ifd['data'][289]['value'], SEEK_SET ); |
| 333 | + // get all offsets of the TileByteCounts |
| 334 | + $freebytes = array(); |
| 335 | + for ( $i = 0; $i < $ifd['data'][289]['count']; $i++ ) { |
| 336 | + $freebytes[] = unpack( $this->long, fread( $this->file_handle, 4 ) ); |
| 337 | + } |
| 338 | + // calculate the memory range of the image tiles |
| 339 | + for ( $i = 0; $i < count( $tiles ); $i++ ) { |
| 340 | + $size = $free[$i][1] + $freebytes[$i][1]; |
| 341 | + if ( $size > $this->highest_addr ) { |
| 342 | + $this->highest_addr = $size; |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + } |
| 347 | + } |
348 | 348 | } |
\ No newline at end of file |
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php |
— | — | @@ -113,7 +113,7 @@ |
114 | 114 | |
115 | 115 | // NOTE: in future, it will become possible to pass parameters |
116 | 116 | // $error = array( 'tiff_bad_file' , join('<br />', $meta['errors']) ); |
117 | | - // does that work now? ^DK |
| 117 | + // does that work now? ^DK |
118 | 118 | |
119 | 119 | wfDebug( __METHOD__ . ": $error ($saveName) " . join( '; ', $meta['errors'] ) . "\n" ); |
120 | 120 | return false; |
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * |
5 | | - * Copyright (C) Wikimedia Deuschland, 2009 |
| 5 | + * Copyright (C) Wikimedia Deutschland, 2009 |
6 | 6 | * Authors Hallo Welt! Medienwerkstatt GmbH |
7 | 7 | * Authors Sebastian Ulbricht, Daniel Lynge, Marc Reymann, Markus Glaser |
8 | 8 | * |
— | — | @@ -47,7 +47,7 @@ |
48 | 48 | * Called by MimeMagick functions. |
49 | 49 | */ |
50 | 50 | public function isValid() { |
51 | | - return count($this->retrieveMetaData()); |
| 51 | + return count( $this->retrieveMetaData() ); |
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
— | — | @@ -57,7 +57,7 @@ |
58 | 58 | $data = $this->retrieveMetaData(); |
59 | 59 | $size = $this->getPageSize( $data, 1 ); |
60 | 60 | |
61 | | - if( $size ) { |
| 61 | + if ( $size ) { |
62 | 62 | $width = $size['width']; |
63 | 63 | $height = $size['height']; |
64 | 64 | return array( $width, $height, 'Tiff', |
— | — | @@ -70,9 +70,9 @@ |
71 | 71 | * Returns an array with width and height of the tiff page. |
72 | 72 | */ |
73 | 73 | public static function getPageSize( $data, $page ) { |
74 | | - if( isset( $data['page_data'][$page] ) ) { |
75 | | - return array('width' => $data['page_data'][$page]['width'], |
76 | | - 'height' => $data['page_data'][$page]['height']); |
| 74 | + if ( isset( $data['page_data'][$page] ) ) { |
| 75 | + return array( 'width' => $data['page_data'][$page]['width'], |
| 76 | + 'height' => $data['page_data'][$page]['height'] ); |
77 | 77 | } |
78 | 78 | return false; |
79 | 79 | } |
— | — | @@ -89,14 +89,14 @@ |
90 | 90 | public function retrieveMetaData() { |
91 | 91 | global $wgImageMagickIdentifyCommand, $wgTiffExivCommand, $wgTiffUseExiv, $wgMemc, $wgTiffErrorCacheTTL; |
92 | 92 | |
93 | | - $imgKey = wfMemcKey('PagedTiffHandler-ThumbnailGeneration', $this->mFilename); |
94 | | - $isCached = $wgMemc->get($imgKey); |
95 | | - if($isCached) { |
96 | | - return -1; |
| 93 | + $imgKey = wfMemcKey( 'PagedTiffHandler-ThumbnailGeneration', $this->mFilename ); |
| 94 | + $isCached = $wgMemc->get( $imgKey ); |
| 95 | + if ( $isCached ) { |
| 96 | + return - 1; |
97 | 97 | } |
98 | | - $wgMemc->add($imgKey, 1, $wgTiffErrorCacheTTL); |
| 98 | + $wgMemc->add( $imgKey, 1, $wgTiffErrorCacheTTL ); |
99 | 99 | |
100 | | - if($this->_meta === NULL) { |
| 100 | + if ( $this->_meta === NULL ) { |
101 | 101 | if ( $wgImageMagickIdentifyCommand ) { |
102 | 102 | |
103 | 103 | wfProfileIn( 'PagedTiffImage::retrieveMetaData' ); |
— | — | @@ -109,52 +109,52 @@ |
110 | 110 | wfEscapeShellArg( $this->mFilename ) . ' 2>&1'; |
111 | 111 | |
112 | 112 | wfProfileIn( 'identify' ); |
113 | | - wfDebug( __METHOD__.": $cmd\n" ); |
| 113 | + wfDebug( __METHOD__ . ": $cmd\n" ); |
114 | 114 | $dump = wfShellExec( $cmd, $retval ); |
115 | 115 | wfProfileOut( 'identify' ); |
116 | | - if($retval) { |
| 116 | + if ( $retval ) { |
117 | 117 | return false; |
118 | 118 | } |
119 | 119 | $this->_meta = $this->convertDumpToArray( $dump ); |
120 | 120 | $this->_meta['exif'] = array(); |
121 | 121 | |
122 | | - if($wgTiffUseExiv) { |
| 122 | + if ( $wgTiffUseExiv ) { |
123 | 123 | $cmd = wfEscapeShellArg( $wgTiffExivCommand ) . |
124 | 124 | ' -u -psix -Pnt ' . // read EXIF, XMP, IPTC as name-tag => interpreted data -ignore unknown fields |
125 | 125 | // exiv2-doc @link http://www.exiv2.org/sample.html |
126 | | - ## In der Linux-Version von exiv2 gibt es einen Bug, sodass diese Parameter dort nicht funktionieren. ^SU |
| 126 | + # # In der Linux-Version von exiv2 gibt es einen Bug, sodass diese Parameter dort nicht funktionieren. ^SU |
127 | 127 | wfEscapeShellArg( $this->mFilename ); |
128 | 128 | |
129 | 129 | wfRunHooks( "PagedTiffHandlerExivCommand", array( &$cmd, $this->mFilename ) ); |
130 | 130 | |
131 | 131 | wfProfileIn( 'exiv2' ); |
132 | | - wfDebug( __METHOD__.": $cmd\n" ); |
| 132 | + wfDebug( __METHOD__ . ": $cmd\n" ); |
133 | 133 | $dump = wfShellExec( $cmd, $retval ); |
134 | 134 | wfProfileOut( 'exiv2' ); |
135 | 135 | $result = array(); |
136 | | - preg_match_all('/(\w+)\s+(.+)/', $dump, $result, PREG_SET_ORDER); |
| 136 | + preg_match_all( '/(\w+)\s+(.+)/', $dump, $result, PREG_SET_ORDER ); |
137 | 137 | |
138 | | - foreach($result as $data) { |
| 138 | + foreach ( $result as $data ) { |
139 | 139 | $this->_meta['exif'][$data[1]] = $data[2]; |
140 | 140 | } |
141 | 141 | } |
142 | 142 | else { |
143 | 143 | $cmd = wfEscapeShellArg( $wgImageMagickIdentifyCommand ) . |
144 | 144 | ' -verbose ' . |
145 | | - wfEscapeShellArg( $this->mFilename )."[0]"; |
| 145 | + wfEscapeShellArg( $this->mFilename ) . "[0]"; |
146 | 146 | |
147 | 147 | wfProfileIn( 'identify -verbose' ); |
148 | | - wfDebug( __METHOD__.": $cmd\n" ); |
| 148 | + wfDebug( __METHOD__ . ": $cmd\n" ); |
149 | 149 | $dump = wfShellExec( $cmd, $retval ); |
150 | 150 | wfProfileOut( 'identify -verbose' ); |
151 | | - $this->_meta['exif'] = $this->parseVerbose($dump); |
| 151 | + $this->_meta['exif'] = $this->parseVerbose( $dump ); |
152 | 152 | } |
153 | 153 | wfProfileOut( 'PagedTiffImage::retrieveMetaData' ); |
154 | 154 | } |
155 | 155 | } |
156 | | - unset($this->_meta['exif']['Image']); |
157 | | - unset($this->_meta['exif']['filename']); |
158 | | - unset($this->_meta['exif']['Base filename']); |
| 156 | + unset( $this->_meta['exif']['Image'] ); |
| 157 | + unset( $this->_meta['exif']['filename'] ); |
| 158 | + unset( $this->_meta['exif']['Base filename'] ); |
159 | 159 | return $this->_meta; |
160 | 160 | } |
161 | 161 | |
— | — | @@ -166,23 +166,23 @@ |
167 | 167 | global $wgTiffIdentifyRejectMessages, $wgTiffIdentifyBypassMessages; |
168 | 168 | if ( strval( $dump ) == '' ) return false; |
169 | 169 | $infos = NULL; |
170 | | - preg_match_all('/\[BEGIN\](.+?)\[END\]/si', $dump, $infos, PREG_SET_ORDER); |
| 170 | + preg_match_all( '/\[BEGIN\](.+?)\[END\]/si', $dump, $infos, PREG_SET_ORDER ); |
171 | 171 | $data = array(); |
172 | | - $data['page_amount'] = count($infos); |
| 172 | + $data['page_amount'] = count( $infos ); |
173 | 173 | $data['page_data'] = array(); |
174 | | - foreach($infos as $info) { |
| 174 | + foreach ( $infos as $info ) { |
175 | 175 | $entry = array(); |
176 | | - $lines = explode("\n", $info[1]); |
177 | | - foreach($lines as $line) { |
178 | | - if(trim($line) == '') { |
| 176 | + $lines = explode( "\n", $info[1] ); |
| 177 | + foreach ( $lines as $line ) { |
| 178 | + if ( trim( $line ) == '' ) { |
179 | 179 | continue; |
180 | 180 | } |
181 | | - $parts = explode('=', $line); |
182 | | - if(trim($parts[0]) == 'alpha' && trim($parts[1]) == '%A') { |
| 181 | + $parts = explode( '=', $line ); |
| 182 | + if ( trim( $parts[0] ) == 'alpha' && trim( $parts[1] ) == '%A' ) { |
183 | 183 | continue; |
184 | 184 | } |
185 | | - if(trim($parts[0]) == 'alpha2' && !isset($entry['alpha'])) { |
186 | | - switch(trim($parts[1])) { |
| 185 | + if ( trim( $parts[0] ) == 'alpha2' && !isset( $entry['alpha'] ) ) { |
| 186 | + switch( trim( $parts[1] ) ) { |
187 | 187 | case 'DirectClassRGBMatte': |
188 | 188 | case 'DirectClassRGBA': |
189 | 189 | $entry['alpha'] = 'true'; |
— | — | @@ -193,35 +193,35 @@ |
194 | 194 | } |
195 | 195 | continue; |
196 | 196 | } |
197 | | - $entry[trim($parts[0])] = trim($parts[1]); |
| 197 | + $entry[trim( $parts[0] )] = trim( $parts[1] ); |
198 | 198 | } |
199 | 199 | $entry['pixels'] = $entry['height'] * $entry['width']; |
200 | 200 | $data['page_data'][$entry['page']] = $entry; |
201 | 201 | } |
202 | 202 | |
203 | | - $dump = preg_replace('/\[BEGIN\](.+?)\[END\]/si', '', $dump); |
204 | | - if(strlen($dump)) { |
205 | | - $errors = explode("\n", $dump); |
206 | | - foreach($errors as $error) { |
| 203 | + $dump = preg_replace( '/\[BEGIN\](.+?)\[END\]/si', '', $dump ); |
| 204 | + if ( strlen( $dump ) ) { |
| 205 | + $errors = explode( "\n", $dump ); |
| 206 | + foreach ( $errors as $error ) { |
207 | 207 | $knownError = false; |
208 | | - foreach($wgTiffIdentifyRejectMessages as $msg) { |
209 | | - if (preg_match($msg, trim($error))) { |
| 208 | + foreach ( $wgTiffIdentifyRejectMessages as $msg ) { |
| 209 | + if ( preg_match( $msg, trim( $error ) ) ) { |
210 | 210 | $data['errors'][] = $error; |
211 | 211 | $knownError = true; |
212 | 212 | break; |
213 | 213 | } |
214 | 214 | } |
215 | | - if(!$knownError) { |
216 | | - ## BypassMessages werden nicht gespeichert ^SU |
217 | | - foreach($wgTiffIdentifyBypassMessages as $msg) { |
218 | | - if (preg_match($msg, trim($error))) { |
219 | | - //$data['warnings'][] = $error; |
| 215 | + if ( !$knownError ) { |
| 216 | + # # BypassMessages werden nicht gespeichert ^SU |
| 217 | + foreach ( $wgTiffIdentifyBypassMessages as $msg ) { |
| 218 | + if ( preg_match( $msg, trim( $error ) ) ) { |
| 219 | + // $data['warnings'][] = $error; |
220 | 220 | $knownError = true; |
221 | 221 | break; |
222 | 222 | } |
223 | 223 | } |
224 | 224 | } |
225 | | - if(!$knownError) { |
| 225 | + if ( !$knownError ) { |
226 | 226 | $data['warning'][] = $error; |
227 | 227 | } |
228 | 228 | } |
— | — | @@ -233,25 +233,25 @@ |
234 | 234 | * helper function of retrieveMetaData(). |
235 | 235 | * parses shell return from identify-verbose-command into an array. |
236 | 236 | */ |
237 | | - protected function parseVerbose($dump) { |
| 237 | + protected function parseVerbose( $dump ) { |
238 | 238 | $data = array(); |
239 | | - $dump = explode("\n", $dump); |
| 239 | + $dump = explode( "\n", $dump ); |
240 | 240 | $lastwhite = 0; |
241 | 241 | $lastkey = false; |
242 | | - foreach($dump as $line) { |
243 | | - if(preg_match('/^(\s*?)(\w([\w\s]+?)?):(.*?)$/sim', $line, $res)) { |
244 | | - if($lastwhite == 0 || strlen($res[1]) == $lastwhite) { |
245 | | - if(strlen(trim($res[4]))) { |
246 | | - $data[trim($res[2])] = trim($res[4]); |
| 242 | + foreach ( $dump as $line ) { |
| 243 | + if ( preg_match( '/^(\s*?)(\w([\w\s]+?)?):(.*?)$/sim', $line, $res ) ) { |
| 244 | + if ( $lastwhite == 0 || strlen( $res[1] ) == $lastwhite ) { |
| 245 | + if ( strlen( trim( $res[4] ) ) ) { |
| 246 | + $data[trim( $res[2] )] = trim( $res[4] ); |
247 | 247 | } |
248 | 248 | else { |
249 | | - $data[trim($res[2])] = " Data:\n"; |
| 249 | + $data[trim( $res[2] )] = " Data:\n"; |
250 | 250 | } |
251 | | - $lastkey = trim($res[2]); |
252 | | - $lastwhite = strlen($res[1]); |
| 251 | + $lastkey = trim( $res[2] ); |
| 252 | + $lastwhite = strlen( $res[1] ); |
253 | 253 | } |
254 | 254 | else { |
255 | | - $data[$lastkey] .= $line."\n"; |
| 255 | + $data[$lastkey] .= $line . "\n"; |
256 | 256 | } |
257 | 257 | } |
258 | 258 | } |