wiki.techinc.nl/includes/media/GIF.php

159 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/**
* Handler for GIF images.
*
* 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
*
* @file
* @ingroup Media
*/
/**
* Handler for GIF images.
*
* @ingroup Media
*/
class GIFHandler extends BitmapHandler {
2011-04-11 20:43:04 +00:00
const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
function getMetadata( $image, $filename ) {
try {
$parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
} catch( Exception $e ) {
// Broken file?
wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
return self::BROKEN_FILE;
}
return serialize($parsedGIFMetadata);
}
2011-04-11 20:43:04 +00:00
2011-05-26 19:21:50 +00:00
/**
* @param $image File
* @return array|bool
*/
function formatMetadata( $image ) {
$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'] );
}
2011-02-18 23:34:24 +00:00
/**
* @param $image File
* @todo unittests
* @return bool
2011-02-18 23:34:24 +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 );
return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
2009-08-10 09:32:55 +00:00
} else {
return $image->getWidth() * $image->getHeight();
2009-08-10 09:32:55 +00:00
}
}
2011-02-18 23:34:24 +00:00
/**
* @param $image File
* @return bool
*/
function isAnimatedImage( $image ) {
$ser = $image->getMetadata();
2011-04-12 00:16:04 +00:00
if ( $ser ) {
$metadata = unserialize($ser);
2011-04-11 20:43:04 +00:00
if( $metadata['frameCount'] > 1 ) {
return true;
}
}
return false;
}
2011-04-11 20:43:04 +00:00
function getMetadataType( $image ) {
return 'parsed-gif';
}
2011-04-11 20:43:04 +00:00
function isMetadataValid( $image, $metadata ) {
if ( $metadata === self::BROKEN_FILE ) {
// Do not repetitivly regenerate metadata on broken file.
return self::METADATA_GOOD;
}
wfSuppressWarnings();
$data = unserialize( $metadata );
wfRestoreWarnings();
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;
}
2011-02-18 23:34:24 +00:00
/**
* @param $image File
* @return string
*/
function getLongDesc( $image ) {
2010-07-25 21:44:29 +00:00
global $wgLang;
$original = parent::getLongDesc( $image );
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) {
return $original;
2011-04-11 20:43:04 +00:00
}
/* Preserve original image info string, but strip the last char ')' so we can add even more */
$info = array();
$info[] = $original;
2011-04-11 20:43:04 +00:00
if ( $metadata['looped'] ) {
$info[] = wfMsgExt( 'file-info-gif-looped', 'parseinline' );
2011-04-11 20:43:04 +00:00
}
2011-04-11 20:43:04 +00:00
if ( $metadata['frameCount'] > 1 ) {
$info[] = wfMsgExt( 'file-info-gif-frames', 'parseinline', $metadata['frameCount'] );
2011-04-11 20:43:04 +00:00
}
2011-04-11 20:43:04 +00:00
if ( $metadata['duration'] ) {
$info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
2011-04-11 20:43:04 +00:00
}
return $wgLang->commaList( $info );
}
}