Merge "merging latest master" into Wikidata
[lhc/web/wiklou.git] / includes / media / GIF.php
1 <?php
2 /**
3 * Handler for GIF images.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Handler for GIF images.
26 *
27 * @ingroup Media
28 */
29 class GIFHandler extends BitmapHandler {
30
31 const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
32
33 function getMetadata( $image, $filename ) {
34 try {
35 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
36 } catch( Exception $e ) {
37 // Broken file?
38 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
39 return self::BROKEN_FILE;
40 }
41
42 return serialize($parsedGIFMetadata);
43 }
44
45 /**
46 * @param $image File
47 * @return array|bool
48 */
49 function formatMetadata( $image ) {
50 $meta = $image->getMetadata();
51
52 if ( !$meta ) {
53 return false;
54 }
55 $meta = unserialize( $meta );
56 if ( !isset( $meta['metadata'] ) || count( $meta['metadata'] ) <= 1 ) {
57 return false;
58 }
59
60 if ( isset( $meta['metadata']['_MW_GIF_VERSION'] ) ) {
61 unset( $meta['metadata']['_MW_GIF_VERSION'] );
62 }
63 return $this->formatMetadataHelper( $meta['metadata'] );
64 }
65
66 /**
67 * @param $image File
68 * @todo unittests
69 * @return bool
70 */
71 function getImageArea( $image ) {
72 $ser = $image->getMetadata();
73 if ( $ser ) {
74 $metadata = unserialize( $ser );
75 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
76 } else {
77 return $image->getWidth() * $image->getHeight();
78 }
79 }
80
81 /**
82 * @param $image File
83 * @return bool
84 */
85 function isAnimatedImage( $image ) {
86 $ser = $image->getMetadata();
87 if ( $ser ) {
88 $metadata = unserialize($ser);
89 if( $metadata['frameCount'] > 1 ) {
90 return true;
91 }
92 }
93 return false;
94 }
95
96 function getMetadataType( $image ) {
97 return 'parsed-gif';
98 }
99
100 function isMetadataValid( $image, $metadata ) {
101 if ( $metadata === self::BROKEN_FILE ) {
102 // Do not repetitivly regenerate metadata on broken file.
103 return self::METADATA_GOOD;
104 }
105
106 wfSuppressWarnings();
107 $data = unserialize( $metadata );
108 wfRestoreWarnings();
109
110 if ( !$data || !is_array( $data ) ) {
111 wfDebug(__METHOD__ . ' invalid GIF metadata' );
112 return self::METADATA_BAD;
113 }
114
115 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
116 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION ) {
117 wfDebug(__METHOD__ . ' old but compatible GIF metadata' );
118 return self::METADATA_COMPATIBLE;
119 }
120 return self::METADATA_GOOD;
121 }
122
123 /**
124 * @param $image File
125 * @return string
126 */
127 function getLongDesc( $image ) {
128 global $wgLang;
129
130 $original = parent::getLongDesc( $image );
131
132 wfSuppressWarnings();
133 $metadata = unserialize($image->getMetadata());
134 wfRestoreWarnings();
135
136 if (!$metadata || $metadata['frameCount'] <= 1) {
137 return $original;
138 }
139
140 /* Preserve original image info string, but strip the last char ')' so we can add even more */
141 $info = array();
142 $info[] = $original;
143
144 if ( $metadata['looped'] ) {
145 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
146 }
147
148 if ( $metadata['frameCount'] > 1 ) {
149 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
150 }
151
152 if ( $metadata['duration'] ) {
153 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
154 }
155
156 return $wgLang->commaList( $info );
157 }
158 }