Index: branches/img_metadata/phase3/includes/media/PNGMetadataExtractor.php |
— | — | @@ -24,7 +24,7 @@ |
25 | 25 | static function getMetadata( $filename ) { |
26 | 26 | self::$png_sig = pack( "C8", 137, 80, 78, 71, 13, 10, 26, 10 ); |
27 | 27 | self::$CRC_size = 4; |
28 | | - /* based on list at http://owl.phy.queensu.ca/~phil/exiftool/TagNames/PNG.html#TextualData |
| 28 | + /* based on list at http://owl.phy.queensu.ca/~phil/exiftool/TagNames/PNG.html#TextualData |
29 | 29 | * and http://www.w3.org/TR/PNG/#11keywords |
30 | 30 | */ |
31 | 31 | self::$text_chunks = array( |
— | — | @@ -53,23 +53,24 @@ |
54 | 54 | ); |
55 | 55 | |
56 | 56 | $showXMP = function_exists( 'xml_parser_create_ns' ); |
57 | | - |
| 57 | + |
58 | 58 | $frameCount = 0; |
59 | 59 | $loopCount = 1; |
60 | 60 | $duration = 0.0; |
61 | 61 | $text = array(); |
62 | 62 | |
63 | | - if (!$filename) |
| 63 | + if ( !$filename ) { |
64 | 64 | throw new Exception( __METHOD__ . ": No file name specified" ); |
65 | | - elseif ( !file_exists($filename) || is_dir($filename) ) |
| 65 | + } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) { |
66 | 66 | throw new Exception( __METHOD__ . ": File $filename does not exist" ); |
67 | | - |
| 67 | + } |
| 68 | + |
68 | 69 | $fh = fopen( $filename, 'r' ); |
69 | | - |
70 | | - if (!$fh) { |
| 70 | + |
| 71 | + if ( !$fh ) { |
71 | 72 | throw new Exception( __METHOD__ . ": Unable to open file $filename" ); |
72 | 73 | } |
73 | | - |
| 74 | + |
74 | 75 | // Check for the PNG header |
75 | 76 | $buf = fread( $fh, 8 ); |
76 | 77 | if ( $buf != self::$png_sig ) { |
— | — | @@ -77,22 +78,22 @@ |
78 | 79 | } |
79 | 80 | |
80 | 81 | // Read chunks |
81 | | - while( !feof( $fh ) ) { |
| 82 | + while ( !feof( $fh ) ) { |
82 | 83 | $buf = fread( $fh, 4 ); |
83 | | - if( !$buf ) { |
| 84 | + if ( !$buf ) { |
84 | 85 | throw new Exception( __METHOD__ . ": Read error" ); |
85 | 86 | } |
86 | | - $chunk_size = unpack( "N", $buf); |
| 87 | + $chunk_size = unpack( "N", $buf ); |
87 | 88 | $chunk_size = $chunk_size[1]; |
88 | 89 | |
89 | 90 | $chunk_type = fread( $fh, 4 ); |
90 | | - if( !$chunk_type ) { |
| 91 | + if ( !$chunk_type ) { |
91 | 92 | throw new Exception( __METHOD__ . ": Read error" ); |
92 | 93 | } |
93 | 94 | |
94 | 95 | if ( $chunk_type == "acTL" ) { |
95 | 96 | $buf = fread( $fh, $chunk_size ); |
96 | | - if( !$buf ) { |
| 97 | + if ( !$buf ) { |
97 | 98 | throw new Exception( __METHOD__ . ": Read error" ); |
98 | 99 | } |
99 | 100 | |
— | — | @@ -101,21 +102,23 @@ |
102 | 103 | $loopCount = $actl['plays']; |
103 | 104 | } elseif ( $chunk_type == "fcTL" ) { |
104 | 105 | $buf = fread( $fh, $chunk_size ); |
105 | | - if( !$buf ) { |
| 106 | + if ( !$buf ) { |
106 | 107 | throw new Exception( __METHOD__ . ": Read error" ); |
107 | 108 | } |
108 | | - $buf = substr( $buf, 20 ); |
| 109 | + $buf = substr( $buf, 20 ); |
109 | 110 | |
110 | 111 | $fctldur = unpack( "ndelay_num/ndelay_den", $buf ); |
111 | | - if( $fctldur['delay_den'] == 0 ) $fctldur['delay_den'] = 100; |
112 | | - if( $fctldur['delay_num'] ) { |
| 112 | + if ( $fctldur['delay_den'] == 0 ) { |
| 113 | + $fctldur['delay_den'] = 100; |
| 114 | + } |
| 115 | + if ( $fctldur['delay_num'] ) { |
113 | 116 | $duration += $fctldur['delay_num'] / $fctldur['delay_den']; |
114 | 117 | } |
115 | 118 | } elseif ( $chunk_type == "iTXt" ) { |
116 | 119 | // Extracts iTXt chunks, uncompressing if necessary. |
117 | 120 | $buf = fread( $fh, $chunk_size ); |
118 | 121 | $items = array(); |
119 | | - if ( preg_match( |
| 122 | + if ( preg_match( |
120 | 123 | '/^([^\x00]{1,79})\x00(\x00|\x01)\x00([^\x00]*)(.)[^\x00]*\x00(.*)$/Ds', |
121 | 124 | $buf, $items ) |
122 | 125 | ) { |
— | — | @@ -137,7 +140,7 @@ |
138 | 141 | // if no lang specified use x-default like in xmp. |
139 | 142 | $items[3] = 'x-default'; |
140 | 143 | } |
141 | | - |
| 144 | + |
142 | 145 | // if compressed |
143 | 146 | if ( $items[2] == "\x01" ) { |
144 | 147 | if ( function_exists( 'gzuncompress' ) && $items[4] === "\x00" ) { |
— | — | @@ -146,7 +149,7 @@ |
147 | 150 | wfRestoreWarnings(); |
148 | 151 | |
149 | 152 | if ( $items[5] === false ) { |
150 | | - //decompression failed |
| 153 | + // decompression failed |
151 | 154 | wfDebug( __METHOD__ . ' Error decompressing iTxt chunk - ' . $items[1] ); |
152 | 155 | fseek( $fh, self::$CRC_size, SEEK_CUR ); |
153 | 156 | continue; |
— | — | @@ -164,9 +167,8 @@ |
165 | 168 | $text[ $finalKeyword ]['_type'] = 'lang'; |
166 | 169 | |
167 | 170 | } else { |
168 | | - //Error reading iTXt chunk |
| 171 | + // Error reading iTXt chunk |
169 | 172 | throw new Exception( __METHOD__ . ": Read error on iTXt chunk" ); |
170 | | - return; |
171 | 173 | } |
172 | 174 | |
173 | 175 | } elseif ( $chunk_type == 'tEXt' ) { |
— | — | @@ -177,7 +179,6 @@ |
178 | 180 | list( $keyword, $content ) = explode( "\x00", $buf, 2 ); |
179 | 181 | if ( $keyword === '' || $content === '' ) { |
180 | 182 | throw new Exception( __METHOD__ . ": Read error on tEXt chunk" ); |
181 | | - return; |
182 | 183 | } |
183 | 184 | |
184 | 185 | // Theoretically should be case-sensitive, but in practise... |
— | — | @@ -188,12 +189,11 @@ |
189 | 190 | continue; |
190 | 191 | } |
191 | 192 | wfSuppressWarnings(); |
192 | | - $content = iconv( 'ISO-8859-1', 'UTF-8', $content); |
| 193 | + $content = iconv( 'ISO-8859-1', 'UTF-8', $content ); |
193 | 194 | wfRestoreWarnings(); |
194 | 195 | |
195 | 196 | if ( $content === false ) { |
196 | 197 | throw new Exception( __METHOD__ . ": Read error (error with iconv)" ); |
197 | | - return; |
198 | 198 | } |
199 | 199 | |
200 | 200 | $finalKeyword = self::$text_chunks[ $keyword ]; |
— | — | @@ -209,7 +209,6 @@ |
210 | 210 | list( $keyword, $postKeyword ) = explode( "\x00", $buf, 2 ); |
211 | 211 | if ( $keyword === '' || $postKeyword === '' ) { |
212 | 212 | throw new Exception( __METHOD__ . ": Read error on zTXt chunk" ); |
213 | | - return; |
214 | 213 | } |
215 | 214 | // Theoretically should be case-sensitive, but in practise... |
216 | 215 | $keyword = strtolower( $keyword ); |
— | — | @@ -232,19 +231,18 @@ |
233 | 232 | wfRestoreWarnings(); |
234 | 233 | |
235 | 234 | if ( $content === false ) { |
236 | | - //decompression failed |
| 235 | + // decompression failed |
237 | 236 | wfDebug( __METHOD__ . ' Error decompressing zTXt chunk - ' . $keyword ); |
238 | 237 | fseek( $fh, self::$CRC_size, SEEK_CUR ); |
239 | 238 | continue; |
240 | 239 | } |
241 | 240 | |
242 | 241 | wfSuppressWarnings(); |
243 | | - $content = iconv( 'ISO-8859-1', 'UTF-8', $content); |
| 242 | + $content = iconv( 'ISO-8859-1', 'UTF-8', $content ); |
244 | 243 | wfRestoreWarnings(); |
245 | 244 | |
246 | 245 | if ( $content === false ) { |
247 | 246 | throw new Exception( __METHOD__ . ": Read error (error with iconv)" ); |
248 | | - return; |
249 | 247 | } |
250 | 248 | |
251 | 249 | $finalKeyword = self::$text_chunks[ $keyword ]; |
— | — | @@ -257,9 +255,13 @@ |
258 | 256 | } |
259 | 257 | } elseif ( $chunk_type == 'tIME' ) { |
260 | 258 | // last mod timestamp. |
261 | | - if( $chunk_size !== 7 ) { throw new Exception( __METHOD__ . ": tIME wrong size" ); return; } |
| 259 | + if ( $chunk_size !== 7 ) { |
| 260 | + throw new Exception( __METHOD__ . ": tIME wrong size" ); |
| 261 | + } |
262 | 262 | $buf = fread( $fh, $chunk_size ); |
263 | | - if( !$buf ) { throw new Exception( __METHOD__ . ": Read error" ); return; } |
| 263 | + if ( !$buf ) { |
| 264 | + throw new Exception( __METHOD__ . ": Read error" ); |
| 265 | + } |
264 | 266 | |
265 | 267 | // Note: spec says this should be UTC. |
266 | 268 | $t = unpack( "ny/Cm/Cd/Ch/Cmin/Cs", $buf ); |
— | — | @@ -275,9 +277,14 @@ |
276 | 278 | |
277 | 279 | } elseif ( $chunk_type == 'pHYs' ) { |
278 | 280 | // how big pixels are (dots per meter). |
279 | | - if( $chunk_size !== 9 ) { throw new Exception( __METHOD__ . ": pHYs wrong size" ); return; } |
| 281 | + if ( $chunk_size !== 9 ) { |
| 282 | + throw new Exception( __METHOD__ . ": pHYs wrong size" ); |
| 283 | + } |
| 284 | + |
280 | 285 | $buf = fread( $fh, $chunk_size ); |
281 | | - if( !$buf ) { throw new Exception( __METHOD__ . ": Read error" ); return; } |
| 286 | + if ( !$buf ) { |
| 287 | + throw new Exception( __METHOD__ . ": Read error" ); |
| 288 | + } |
282 | 289 | |
283 | 290 | $dim = unpack( "Nwidth/Nheight/Cunit", $buf ); |
284 | 291 | if ( $dim['unit'] == 1 ) { |
— | — | @@ -300,7 +307,7 @@ |
301 | 308 | } |
302 | 309 | fclose( $fh ); |
303 | 310 | |
304 | | - if( $loopCount > 1 ) { |
| 311 | + if ( $loopCount > 1 ) { |
305 | 312 | $duration *= $loopCount; |
306 | 313 | } |
307 | 314 | |
— | — | @@ -336,6 +343,6 @@ |
337 | 344 | 'duration' => $duration, |
338 | 345 | 'text' => $text, |
339 | 346 | ); |
340 | | - |
| 347 | + |
341 | 348 | } |
342 | 349 | } |