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-16 01:23:15 +00:00
|
|
|
const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
|
2012-10-19 20:10:42 +00:00
|
|
|
|
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 );
|
2013-04-27 12:02:08 +00:00
|
|
|
} catch ( Exception $e ) {
|
2011-04-16 01:23:15 +00:00
|
|
|
// Broken file?
|
|
|
|
|
wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return self::BROKEN_FILE;
|
2009-08-23 23:27:32 +00:00
|
|
|
}
|
2009-08-03 15:01:51 +00:00
|
|
|
|
2013-02-03 19:42:08 +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
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image
|
2014-12-28 20:31:34 +00:00
|
|
|
* @param bool|IContextSource $context Context to use (optional)
|
2011-05-26 19:21:50 +00:00
|
|
|
* @return array|bool
|
|
|
|
|
*/
|
2014-12-28 20:31:34 +00:00
|
|
|
function formatMetadata( $image, $context = false ) {
|
2013-08-28 23:09:07 +00:00
|
|
|
$meta = $this->getCommonMetaArray( $image );
|
|
|
|
|
if ( count( $meta ) === 0 ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-28 20:31:34 +00:00
|
|
|
return $this->formatMetadataHelper( $meta, $context );
|
2013-08-28 23:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the standard metadata elements for #filemetadata parser func.
|
|
|
|
|
* @param File $image
|
|
|
|
|
* @return array|bool
|
|
|
|
|
*/
|
|
|
|
|
public function getCommonMetaArray( File $image ) {
|
2011-04-16 01:23:15 +00:00
|
|
|
$meta = $image->getMetadata();
|
|
|
|
|
|
|
|
|
|
if ( !$meta ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|
|
|
|
|
$meta = unserialize( $meta );
|
2013-08-28 23:09:07 +00:00
|
|
|
if ( !isset( $meta['metadata'] ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2011-04-16 01:23:15 +00:00
|
|
|
}
|
2013-08-28 23:09:07 +00:00
|
|
|
unset( $meta['metadata']['_MW_GIF_VERSION'] );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2013-08-28 23:09:07 +00:00
|
|
|
return $meta['metadata'];
|
2009-08-03 15:01:51 +00:00
|
|
|
}
|
2011-02-18 23:34:24 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @todo Add unit tests
|
|
|
|
|
*
|
2014-04-06 18:11:30 +00:00
|
|
|
* @param File $image
|
2011-11-02 20:48:50 +00:00
|
|
|
* @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 );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
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
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image
|
2011-02-18 23:34:24 +00:00
|
|
|
* @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 ) {
|
2013-02-03 19:42:08 +00:00
|
|
|
$metadata = unserialize( $ser );
|
2013-04-20 21:11:46 +00:00
|
|
|
if ( $metadata['frameCount'] > 1 ) {
|
2011-04-11 20:43:04 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2010-05-05 22:37:27 +00:00
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2010-05-05 22:37:27 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2011-04-11 20:43:04 +00:00
|
|
|
|
2012-08-19 01:19:53 +00:00
|
|
|
/**
|
|
|
|
|
* We cannot animate thumbnails that are bigger than a particular size
|
|
|
|
|
* @param File $file
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function canAnimateThumbnail( $file ) {
|
|
|
|
|
global $wgMaxAnimatedGifArea;
|
|
|
|
|
$answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2012-08-19 01:19:53 +00:00
|
|
|
return $answer;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2010-06-09 10:15:19 +00:00
|
|
|
$data = unserialize( $metadata );
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2011-04-16 01:23:15 +00:00
|
|
|
|
|
|
|
|
if ( !$data || !is_array( $data ) ) {
|
2013-05-20 18:57:43 +00:00
|
|
|
wfDebug( __METHOD__ . " invalid GIF metadata\n" );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return self::METADATA_BAD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
|
2013-12-05 10:05:05 +00:00
|
|
|
|| $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
|
|
|
|
|
) {
|
2013-05-20 18:57:43 +00:00
|
|
|
wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return self::METADATA_COMPATIBLE;
|
|
|
|
|
}
|
2013-12-05 10:05:05 +00:00
|
|
|
|
2011-04-16 01:23:15 +00:00
|
|
|
return self::METADATA_GOOD;
|
2010-04-17 01:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-18 23:34:24 +00:00
|
|
|
/**
|
2013-12-05 19:27:27 +00:00
|
|
|
* @param File $image
|
2011-02-18 23:34:24 +00:00
|
|
|
* @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
|
|
|
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2013-02-03 19:42:08 +00:00
|
|
|
$metadata = unserialize( $image->getMetadata() );
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2012-10-19 20:10:42 +00:00
|
|
|
|
2013-02-03 19:42:08 +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 */
|
2016-02-17 09:09:32 +00:00
|
|
|
$info = [];
|
2011-01-10 22:18:08 +00:00
|
|
|
$info[] = $original;
|
2012-10-19 20:10:42 +00:00
|
|
|
|
2011-04-11 20:43:04 +00:00
|
|
|
if ( $metadata['looped'] ) {
|
2012-07-24 01:04:15 +00:00
|
|
|
$info[] = wfMessage( 'file-info-gif-looped' )->parse();
|
2011-04-11 20:43:04 +00:00
|
|
|
}
|
2012-10-19 20:10:42 +00:00
|
|
|
|
2011-04-11 20:43:04 +00:00
|
|
|
if ( $metadata['frameCount'] > 1 ) {
|
2012-07-24 01:04:15 +00:00
|
|
|
$info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
|
2011-04-11 20:43:04 +00:00
|
|
|
}
|
2012-10-19 20:10:42 +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
|
|
|
}
|
2012-10-19 20:10:42 +00:00
|
|
|
|
2011-01-10 22:18:08 +00:00
|
|
|
return $wgLang->commaList( $info );
|
2009-08-03 15:01:51 +00:00
|
|
|
}
|
2014-09-14 21:26:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the duration of the GIF file.
|
|
|
|
|
*
|
|
|
|
|
* Shown in the &query=imageinfo&iiprop=size api query.
|
|
|
|
|
*
|
2016-04-30 10:10:17 +00:00
|
|
|
* @param File $file
|
2014-09-14 21:26:05 +00:00
|
|
|
* @return float The duration of the file.
|
|
|
|
|
*/
|
|
|
|
|
public function getLength( $file ) {
|
|
|
|
|
$serMeta = $file->getMetadata();
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2014-09-14 21:26:05 +00:00
|
|
|
$metadata = unserialize( $serMeta );
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2014-09-14 21:26:05 +00:00
|
|
|
|
|
|
|
|
if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
|
|
|
|
|
return 0.0;
|
|
|
|
|
} else {
|
|
|
|
|
return (float)$metadata['duration'];
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-03 15:01:51 +00:00
|
|
|
}
|