Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / media / GIFHandler.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 const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
31
32 public function getMetadata( $image, $filename ) {
33 try {
34 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
35 } catch ( Exception $e ) {
36 // Broken file?
37 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
38
39 return self::BROKEN_FILE;
40 }
41
42 return serialize( $parsedGIFMetadata );
43 }
44
45 /**
46 * @param File $image
47 * @param bool|IContextSource $context Context to use (optional)
48 * @return array|bool
49 */
50 public function formatMetadata( $image, $context = false ) {
51 $meta = $this->getCommonMetaArray( $image );
52 if ( count( $meta ) === 0 ) {
53 return false;
54 }
55
56 return $this->formatMetadataHelper( $meta, $context );
57 }
58
59 /**
60 * Return the standard metadata elements for #filemetadata parser func.
61 * @param File $image
62 * @return array|bool
63 */
64 public function getCommonMetaArray( File $image ) {
65 $meta = $image->getMetadata();
66
67 if ( !$meta ) {
68 return [];
69 }
70 $meta = unserialize( $meta );
71 if ( !isset( $meta['metadata'] ) ) {
72 return [];
73 }
74 unset( $meta['metadata']['_MW_GIF_VERSION'] );
75
76 return $meta['metadata'];
77 }
78
79 /**
80 * @todo Add unit tests
81 *
82 * @param File $image
83 * @return bool
84 */
85 function getImageArea( $image ) {
86 $ser = $image->getMetadata();
87 if ( $ser ) {
88 $metadata = unserialize( $ser );
89 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
90 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
91 } else {
92 return $image->getWidth() * $image->getHeight();
93 }
94 } else {
95 return $image->getWidth() * $image->getHeight();
96 }
97 }
98
99 /**
100 * @param File $image
101 * @return bool
102 */
103 function isAnimatedImage( $image ) {
104 $ser = $image->getMetadata();
105 if ( $ser ) {
106 $metadata = unserialize( $ser );
107 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
108 return true;
109 }
110 }
111
112 return false;
113 }
114
115 /**
116 * We cannot animate thumbnails that are bigger than a particular size
117 * @param File $file
118 * @return bool
119 */
120 function canAnimateThumbnail( $file ) {
121 global $wgMaxAnimatedGifArea;
122 $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
123
124 return $answer;
125 }
126
127 function getMetadataType( $image ) {
128 return 'parsed-gif';
129 }
130
131 public function isMetadataValid( $image, $metadata ) {
132 if ( $metadata === self::BROKEN_FILE ) {
133 // Do not repetitivly regenerate metadata on broken file.
134 return self::METADATA_GOOD;
135 }
136
137 Wikimedia\suppressWarnings();
138 $data = unserialize( $metadata );
139 Wikimedia\restoreWarnings();
140
141 if ( !$data || !is_array( $data ) ) {
142 wfDebug( __METHOD__ . " invalid GIF metadata\n" );
143
144 return self::METADATA_BAD;
145 }
146
147 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
148 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
149 ) {
150 wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
151
152 return self::METADATA_COMPATIBLE;
153 }
154
155 return self::METADATA_GOOD;
156 }
157
158 /**
159 * @param File $image
160 * @return string
161 */
162 public function getLongDesc( $image ) {
163 global $wgLang;
164
165 $original = parent::getLongDesc( $image );
166
167 Wikimedia\suppressWarnings();
168 $metadata = unserialize( $image->getMetadata() );
169 Wikimedia\restoreWarnings();
170
171 if ( !$metadata || $metadata['frameCount'] <= 1 ) {
172 return $original;
173 }
174
175 /* Preserve original image info string, but strip the last char ')' so we can add even more */
176 $info = [];
177 $info[] = $original;
178
179 if ( $metadata['looped'] ) {
180 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
181 }
182
183 if ( $metadata['frameCount'] > 1 ) {
184 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
185 }
186
187 if ( $metadata['duration'] ) {
188 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
189 }
190
191 return $wgLang->commaList( $info );
192 }
193
194 /**
195 * Return the duration of the GIF file.
196 *
197 * Shown in the &query=imageinfo&iiprop=size api query.
198 *
199 * @param File $file
200 * @return float The duration of the file.
201 */
202 public function getLength( $file ) {
203 $serMeta = $file->getMetadata();
204 Wikimedia\suppressWarnings();
205 $metadata = unserialize( $serMeta );
206 Wikimedia\restoreWarnings();
207
208 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
209 return 0.0;
210 } else {
211 return (float)$metadata['duration'];
212 }
213 }
214 }