2009-08-03 15:01:51 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2010-08-15 17:27:41 +00:00
|
|
|
* Handler for GIF images.
|
|
|
|
|
*
|
2012-05-03 20:13:10 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2009-08-03 15:01:51 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handler for GIF images.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
|
|
|
|
class GIFHandler extends BitmapHandler {
|
2011-04-11 20:43:04 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
|
|
|
|
|
|
2009-08-03 15:01:51 +00:00
|
|
|
function getMetadata( $image, $filename ) {
|
2011-04-16 01:23:15 +00:00
|
|
|
try {
|
|
|
|
|
$parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
|
|
|
|
|
} catch( Exception $e ) {
|
|
|
|
|
// Broken file?
|
|
|
|
|
wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
|
|
|
|
|
return self::BROKEN_FILE;
|
2009-08-23 23:27:32 +00:00
|
|
|
}
|
2009-08-03 15:01:51 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return serialize($parsedGIFMetadata);
|
2009-08-03 15:01:51 +00:00
|
|
|
}
|
2011-04-11 20:43:04 +00:00
|
|
|
|
2011-05-26 19:21:50 +00:00
|
|
|
/**
|
|
|
|
|
* @param $image File
|
|
|
|
|
* @return array|bool
|
|
|
|
|
*/
|
2009-08-03 15:01:51 +00:00
|
|
|
function formatMetadata( $image ) {
|
2011-04-16 01:23:15 +00:00
|
|
|
$meta = $image->getMetadata();
|
|
|
|
|
|
|
|
|
|
if ( !$meta ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$meta = unserialize( $meta );
|
|
|
|
|
if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $meta['metadata']['_MW_GIF_VERSION'] ) ) {
|
|
|
|
|
unset( $meta['metadata']['_MW_GIF_VERSION'] );
|
|
|
|
|
}
|
|
|
|
|
return $this->formatMetadataHelper( $meta['metadata'] );
|
2009-08-03 15:01:51 +00:00
|
|
|
}
|
2011-02-18 23:34:24 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $image File
|
2011-11-02 20:48:50 +00:00
|
|
|
* @todo unittests
|
|
|
|
|
* @return bool
|
2011-02-18 23:34:24 +00:00
|
|
|
*/
|
2011-11-02 20:48:50 +00:00
|
|
|
function getImageArea( $image ) {
|
2009-08-10 11:12:04 +00:00
|
|
|
$ser = $image->getMetadata();
|
2011-04-12 00:16:04 +00:00
|
|
|
if ( $ser ) {
|
|
|
|
|
$metadata = unserialize( $ser );
|
2011-11-02 20:48:50 +00:00
|
|
|
return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
|
2009-08-10 09:32:55 +00:00
|
|
|
} else {
|
2011-11-02 20:48:50 +00:00
|
|
|
return $image->getWidth() * $image->getHeight();
|
2009-08-10 09:32:55 +00:00
|
|
|
}
|
2009-08-03 15:01:51 +00:00
|
|
|
}
|
2010-05-05 22:37:27 +00:00
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
|
|
|
|
* @param $image File
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2010-05-05 22:37:27 +00:00
|
|
|
function isAnimatedImage( $image ) {
|
|
|
|
|
$ser = $image->getMetadata();
|
2011-04-12 00:16:04 +00:00
|
|
|
if ( $ser ) {
|
2010-05-05 22:37:27 +00:00
|
|
|
$metadata = unserialize($ser);
|
2011-04-11 20:43:04 +00:00
|
|
|
if( $metadata['frameCount'] > 1 ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-05-05 22:37:27 +00:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-04-11 20:43:04 +00:00
|
|
|
|
2009-08-03 15:01:51 +00:00
|
|
|
function getMetadataType( $image ) {
|
|
|
|
|
return 'parsed-gif';
|
|
|
|
|
}
|
2011-04-11 20:43:04 +00:00
|
|
|
|
2010-04-17 01:37:32 +00:00
|
|
|
function isMetadataValid( $image, $metadata ) {
|
2011-04-16 01:23:15 +00:00
|
|
|
if ( $metadata === self::BROKEN_FILE ) {
|
|
|
|
|
// Do not repetitivly regenerate metadata on broken file.
|
|
|
|
|
return self::METADATA_GOOD;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-09 10:15:19 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$data = unserialize( $metadata );
|
|
|
|
|
wfRestoreWarnings();
|
2011-04-16 01:23:15 +00:00
|
|
|
|
|
|
|
|
if ( !$data || !is_array( $data ) ) {
|
|
|
|
|
wfDebug(__METHOD__ . ' invalid GIF metadata' );
|
|
|
|
|
return self::METADATA_BAD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
|
|
|
|
|
|| $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION ) {
|
|
|
|
|
wfDebug(__METHOD__ . ' old but compatible GIF metadata' );
|
|
|
|
|
return self::METADATA_COMPATIBLE;
|
|
|
|
|
}
|
|
|
|
|
return self::METADATA_GOOD;
|
2010-04-17 01:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
|
|
|
|
* @param $image File
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-08-03 15:01:51 +00:00
|
|
|
function getLongDesc( $image ) {
|
2010-07-25 21:44:29 +00:00
|
|
|
global $wgLang;
|
|
|
|
|
|
2010-06-20 16:13:24 +00:00
|
|
|
$original = parent::getLongDesc( $image );
|
2010-06-09 10:15:19 +00:00
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$metadata = unserialize($image->getMetadata());
|
|
|
|
|
wfRestoreWarnings();
|
2009-08-06 22:55:44 +00:00
|
|
|
|
2011-04-11 20:43:04 +00:00
|
|
|
if (!$metadata || $metadata['frameCount'] <= 1) {
|
2010-06-20 16:13:24 +00:00
|
|
|
return $original;
|
2011-04-11 20:43:04 +00:00
|
|
|
}
|
2011-01-09 22:54:51 +00:00
|
|
|
|
|
|
|
|
/* Preserve original image info string, but strip the last char ')' so we can add even more */
|
2009-08-03 15:01:51 +00:00
|
|
|
$info = array();
|
2011-01-10 22:18:08 +00:00
|
|
|
$info[] = $original;
|
2009-08-03 15:01:51 +00:00
|
|
|
|
2011-04-11 20:43:04 +00:00
|
|
|
if ( $metadata['looped'] ) {
|
2009-08-03 15:01:51 +00:00
|
|
|
$info[] = wfMsgExt( 'file-info-gif-looped', 'parseinline' );
|
2011-04-11 20:43:04 +00:00
|
|
|
}
|
2009-08-03 15:01:51 +00:00
|
|
|
|
2011-04-11 20:43:04 +00:00
|
|
|
if ( $metadata['frameCount'] > 1 ) {
|
2009-08-03 15:01:51 +00:00
|
|
|
$info[] = wfMsgExt( 'file-info-gif-frames', 'parseinline', $metadata['frameCount'] );
|
2011-04-11 20:43:04 +00:00
|
|
|
}
|
2009-08-03 15:01:51 +00:00
|
|
|
|
2011-04-11 20:43:04 +00:00
|
|
|
if ( $metadata['duration'] ) {
|
2009-08-03 15:01:51 +00:00
|
|
|
$info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
|
2011-04-11 20:43:04 +00:00
|
|
|
}
|
2009-08-03 15:01:51 +00:00
|
|
|
|
2011-01-10 22:18:08 +00:00
|
|
|
return $wgLang->commaList( $info );
|
2009-08-03 15:01:51 +00:00
|
|
|
}
|
|
|
|
|
}
|