summaryrefslogtreecommitdiff
path: root/includes/media/GIFMetadataExtractor.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/media/GIFMetadataExtractor.php')
-rw-r--r--includes/media/GIFMetadataExtractor.php90
1 files changed, 54 insertions, 36 deletions
diff --git a/includes/media/GIFMetadataExtractor.php b/includes/media/GIFMetadataExtractor.php
index 5fc5c1a7..887afa3f 100644
--- a/includes/media/GIFMetadataExtractor.php
+++ b/includes/media/GIFMetadataExtractor.php
@@ -49,16 +49,16 @@ class GIFMetadataExtractor {
* @return array
*/
static function getMetadata( $filename ) {
- self::$gif_frame_sep = pack( "C", ord("," ) );
- self::$gif_extension_sep = pack( "C", ord("!" ) );
- self::$gif_term = pack( "C", ord(";" ) );
+ self::$gif_frame_sep = pack( "C", ord( "," ) );
+ self::$gif_extension_sep = pack( "C", ord( "!" ) );
+ self::$gif_term = pack( "C", ord( ";" ) );
$frameCount = 0;
$duration = 0.0;
$isLooped = false;
$xmp = "";
$comment = array();
-
+
if ( !$filename ) {
throw new Exception( "No file name specified" );
} elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
@@ -73,7 +73,7 @@ class GIFMetadataExtractor {
// Check for the GIF header
$buf = fread( $fh, 6 );
- if ( !($buf == 'GIF87a' || $buf == 'GIF89a') ) {
+ if ( !( $buf == 'GIF87a' || $buf == 'GIF89a' ) ) {
throw new Exception( "Not a valid GIF file; header: $buf" );
}
@@ -90,10 +90,10 @@ class GIFMetadataExtractor {
// Skip over the GCT
self::readGCT( $fh, $bpp );
- while( !feof( $fh ) ) {
+ while ( !feof( $fh ) ) {
$buf = fread( $fh, 1 );
- if ($buf == self::$gif_frame_sep) {
+ if ( $buf == self::$gif_frame_sep ) {
// Found a frame
$frameCount++;
@@ -107,21 +107,25 @@ class GIFMetadataExtractor {
## Read GCT
self::readGCT( $fh, $bpp );
fread( $fh, 1 );
- self::skipBlock( $fh );
+ self::skipBlock( $fh );
} elseif ( $buf == self::$gif_extension_sep ) {
$buf = fread( $fh, 1 );
- if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
+ if ( strlen( $buf ) < 1 ) {
+ throw new Exception( "Ran out of input" );
+ }
$extension_code = unpack( 'C', $buf );
$extension_code = $extension_code[1];
- if ($extension_code == 0xF9) {
+ if ( $extension_code == 0xF9 ) {
// Graphics Control Extension.
fread( $fh, 1 ); // Block size
fread( $fh, 1 ); // Transparency, disposal method, user input
$buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
- if ( strlen( $buf ) < 2 ) throw new Exception( "Ran out of input" );
+ if ( strlen( $buf ) < 2 ) {
+ throw new Exception( "Ran out of input" );
+ }
$delay = unpack( 'v', $buf );
$delay = $delay[1];
$duration += $delay * 0.01;
@@ -129,13 +133,15 @@ class GIFMetadataExtractor {
fread( $fh, 1 ); // Transparent colour index
$term = fread( $fh, 1 ); // Should be a terminator
- if ( strlen( $term ) < 1 ) throw new Exception( "Ran out of input" );
+ if ( strlen( $term ) < 1 ) {
+ throw new Exception( "Ran out of input" );
+ }
$term = unpack( 'C', $term );
$term = $term[1];
- if ($term != 0 ) {
+ if ( $term != 0 ) {
throw new Exception( "Malformed Graphics Control Extension block" );
}
- } elseif ($extension_code == 0xFE) {
+ } elseif ( $extension_code == 0xFE ) {
// Comment block(s).
$data = self::readBlock( $fh );
if ( $data === "" ) {
@@ -157,48 +163,51 @@ class GIFMetadataExtractor {
$commentCount = count( $comment );
if ( $commentCount === 0
- || $comment[$commentCount-1] !== $data )
+ || $comment[$commentCount - 1] !== $data )
{
// Some applications repeat the same comment on each
// frame of an animated GIF image, so if this comment
// is identical to the last, only extract once.
$comment[] = $data;
}
- } elseif ($extension_code == 0xFF) {
+ } elseif ( $extension_code == 0xFF ) {
// Application extension (Netscape info about the animated gif)
// or XMP (or theoretically any other type of extension block)
$blockLength = fread( $fh, 1 );
- if ( strlen( $blockLength ) < 1 ) throw new Exception( "Ran out of input" );
+ if ( strlen( $blockLength ) < 1 ) {
+ throw new Exception( "Ran out of input" );
+ }
$blockLength = unpack( 'C', $blockLength );
$blockLength = $blockLength[1];
$data = fread( $fh, $blockLength );
- if ($blockLength != 11 ) {
- wfDebug( __METHOD__ . ' GIF application block with wrong length' );
- fseek( $fh, -($blockLength + 1), SEEK_CUR );
+ if ( $blockLength != 11 ) {
+ wfDebug( __METHOD__ . " GIF application block with wrong length\n" );
+ fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
self::skipBlock( $fh );
continue;
}
// NETSCAPE2.0 (application name for animated gif)
if ( $data == 'NETSCAPE2.0' ) {
-
$data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
- if ($data != "\x03\x01") {
+ if ( $data != "\x03\x01" ) {
throw new Exception( "Expected \x03\x01, got $data" );
}
-
+
// Unsigned little-endian integer, loop count or zero for "forever"
$loopData = fread( $fh, 2 );
- if ( strlen( $loopData ) < 2 ) throw new Exception( "Ran out of input" );
+ if ( strlen( $loopData ) < 2 ) {
+ throw new Exception( "Ran out of input" );
+ }
$loopData = unpack( 'v', $loopData );
$loopCount = $loopData[1];
-
- if ($loopCount != 1) {
+
+ if ( $loopCount != 1 ) {
$isLooped = true;
}
-
+
// Read out terminator byte
fread( $fh, 1 );
} elseif ( $data == 'XMP DataXMP' ) {
@@ -219,7 +228,7 @@ class GIFMetadataExtractor {
} else {
// unrecognized extension block
- fseek( $fh, -($blockLength + 1), SEEK_CUR );
+ fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
self::skipBlock( $fh );
continue;
}
@@ -229,10 +238,12 @@ class GIFMetadataExtractor {
} elseif ( $buf == self::$gif_term ) {
break;
} else {
- if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
+ if ( strlen( $buf ) < 1 ) {
+ throw new Exception( "Ran out of input" );
+ }
$byte = unpack( 'C', $buf );
$byte = $byte[1];
- throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
+ throw new Exception( "At position: " . ftell( $fh ) . ", Unknown byte " . $byte );
}
}
@@ -252,7 +263,7 @@ class GIFMetadataExtractor {
*/
static function readGCT( $fh, $bpp ) {
if ( $bpp > 0 ) {
- for( $i=1; $i<=pow( 2, $bpp ); ++$i ) {
+ for ( $i = 1; $i <= pow( 2, $bpp ); ++$i ) {
fread( $fh, 3 );
}
}
@@ -260,10 +271,13 @@ class GIFMetadataExtractor {
/**
* @param $data
+ * @throws Exception
* @return int
*/
static function decodeBPP( $data ) {
- if ( strlen( $data ) < 1 ) throw new Exception( "Ran out of input" );
+ if ( strlen( $data ) < 1 ) {
+ throw new Exception( "Ran out of input" );
+ }
$buf = unpack( 'C', $data );
$buf = $buf[1];
$bpp = ( $buf & 7 ) + 1;
@@ -276,20 +290,23 @@ class GIFMetadataExtractor {
/**
* @param $fh
- * @return
+ * @throws Exception
*/
static function skipBlock( $fh ) {
while ( !feof( $fh ) ) {
$buf = fread( $fh, 1 );
- if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
+ if ( strlen( $buf ) < 1 ) {
+ throw new Exception( "Ran out of input" );
+ }
$block_len = unpack( 'C', $buf );
$block_len = $block_len[1];
- if ($block_len == 0) {
+ if ( $block_len == 0 ) {
return;
}
fread( $fh, $block_len );
}
}
+
/**
* Read a block. In the GIF format, a block is made up of
* several sub-blocks. Each sub block starts with one byte
@@ -301,6 +318,7 @@ class GIFMetadataExtractor {
* sub-blocks in the returned value. Normally this is false,
* except XMP is weird and does a hack where you need to keep
* these length bytes.
+ * @throws Exception
* @return string The data.
*/
static function readBlock( $fh, $includeLengths = false ) {
@@ -308,7 +326,7 @@ class GIFMetadataExtractor {
$subLength = fread( $fh, 1 );
$blocks = 0;
- while( $subLength !== "\0" ) {
+ while ( $subLength !== "\0" ) {
$blocks++;
if ( $blocks > self::MAX_SUBBLOCKS ) {
throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );