| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Logger\LoggerFactory; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use Wikimedia\XMPReader\Reader as XMPReader; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class WebPHandler extends BitmapHandler { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private const BROKEN_FILE = '0'; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private const MINIMUM_CHUNK_HEADER_LENGTH = 18; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private const MAX_METADATA_CHUNK_SIZE = 1024 * 1024 * 2; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private const _MW_WEBP_VERSION = 2; |
| 50 |
|
| 51 |
private const VP8X_ICC = 32; |
| 52 |
private const VP8X_ALPHA = 16; |
| 53 |
private const VP8X_EXIF = 8; |
| 54 |
private const VP8X_XMP = 4; |
| 55 |
private const VP8X_ANIM = 2; |
| 56 |
|
| 57 |
public function getSizeAndMetadata( $state, $filename ) { |
| 58 |
$parsedWebPData = self::extractMetadata( $filename ); |
| 59 |
if ( !$parsedWebPData ) { |
| 60 |
return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ]; |
| 61 |
} |
| 62 |
|
| 63 |
$parsedWebPData['metadata']['_MW_WEBP_VERSION'] = self::_MW_WEBP_VERSION; |
| 64 |
$info = [ |
| 65 |
'width' => $parsedWebPData['width'], |
| 66 |
'height' => $parsedWebPData['height'], |
| 67 |
'metadata' => $parsedWebPData |
| 68 |
]; |
| 69 |
return $info; |
| 70 |
} |
| 71 |
|
| 72 |
public function getMetadataType( $image ) { |
| 73 |
return 'parsed-webp'; |
| 74 |
} |
| 75 |
|
| 76 |
public function isFileMetadataValid( $image ) { |
| 77 |
$data = $image->getMetadataArray(); |
| 78 |
if ( $data === [ '_error' => self::BROKEN_FILE ] ) { |
| 79 |
|
| 80 |
return self::METADATA_GOOD; |
| 81 |
} |
| 82 |
|
| 83 |
if ( !$data || !isset( $data['_error'] ) ) { |
| 84 |
wfDebug( __METHOD__ . " invalid WebP metadata" ); |
| 85 |
|
| 86 |
return self::METADATA_BAD; |
| 87 |
} |
| 88 |
|
| 89 |
if ( !isset( $data['metadata']['_MW_WEBP_VERSION'] ) |
| 90 |
|| $data['metadata']['_MW_WEBP_VERSION'] != self::_MW_WEBP_VERSION |
| 91 |
) { |
| 92 |
wfDebug( __METHOD__ . " old but compatible WebP metadata" ); |
| 93 |
|
| 94 |
return self::METADATA_COMPATIBLE; |
| 95 |
} |
| 96 |
return self::METADATA_GOOD; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static function extractMetadata( $filename ) { |
| 108 |
wfDebugLog( 'WebP', __METHOD__ . ": Extracting metadata from $filename" ); |
| 109 |
|
| 110 |
$info = RiffExtractor::findChunksFromFile( $filename, 100 ); |
| 111 |
if ( $info === false ) { |
| 112 |
wfDebugLog( 'WebP', __METHOD__ . ": Not a valid RIFF file" ); |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
if ( $info['fourCC'] !== 'WEBP' ) { |
| 117 |
wfDebugLog( 'WebP', __METHOD__ . ': FourCC was not WEBP: ' . |
| 118 |
bin2hex( $info['fourCC'] ) ); |
| 119 |
return false; |
| 120 |
} |
| 121 |
$metadata = self::extractMetadataFromChunks( $info['chunks'], $filename ); |
| 122 |
if ( !$metadata ) { |
| 123 |
wfDebugLog( 'WebP', __METHOD__ . ": No VP8 chunks found" ); |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
return $metadata; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static function extractMetadataFromChunks( $chunks, $filename ) { |
| 138 |
$vp8Info = []; |
| 139 |
$exifData = null; |
| 140 |
$xmpData = null; |
| 141 |
|
| 142 |
foreach ( $chunks as $chunk ) { |
| 143 |
|
| 144 |
if ( !in_array( $chunk['fourCC'], [ 'VP8 ', 'VP8L', 'VP8X', 'EXIF', 'XMP ', "XMP\0" ] ) ) { |
| 145 |
|
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
$chunkHeader = file_get_contents( $filename, false, null, |
| 150 |
$chunk['start'], self::MINIMUM_CHUNK_HEADER_LENGTH ); |
| 151 |
wfDebugLog( 'WebP', __METHOD__ . ": {$chunk['fourCC']}" ); |
| 152 |
|
| 153 |
switch ( $chunk['fourCC'] ) { |
| 154 |
case 'VP8 ': |
| 155 |
$vp8Info = array_merge( $vp8Info, |
| 156 |
self::decodeLossyChunkHeader( $chunkHeader ) ); |
| 157 |
break; |
| 158 |
case 'VP8L': |
| 159 |
$vp8Info = array_merge( $vp8Info, |
| 160 |
self::decodeLosslessChunkHeader( $chunkHeader ) ); |
| 161 |
break; |
| 162 |
case 'VP8X': |
| 163 |
$vp8Info = array_merge( $vp8Info, |
| 164 |
self::decodeExtendedChunkHeader( $chunkHeader ) ); |
| 165 |
|
| 166 |
break; |
| 167 |
case 'EXIF': |
| 168 |
|
| 169 |
$exifData ??= self::extractChunk( $chunk, $filename ); |
| 170 |
break; |
| 171 |
case 'XMP ': |
| 172 |
case "XMP\0": |
| 173 |
$xmpData ??= self::extractChunk( $chunk, $filename ); |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
$vp8Info = array_merge( $vp8Info, |
| 178 |
self::decodeMediaMetadata( $exifData, $xmpData, $filename ) ); |
| 179 |
return $vp8Info; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
private static function decodeMediaMetadata( $exifData, $xmpData, $filename ) { |
| 191 |
if ( $exifData === null && $xmpData === null ) { |
| 192 |
|
| 193 |
return []; |
| 194 |
} |
| 195 |
$bitmapMetadataHandler = new BitmapMetadataHandler; |
| 196 |
|
| 197 |
if ( $xmpData && XMPReader::isSupported() ) { |
| 198 |
$xmpReader = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename ); |
| 199 |
$xmpReader->parse( $xmpData ); |
| 200 |
$res = $xmpReader->getResults(); |
| 201 |
foreach ( $res as $type => $array ) { |
| 202 |
$bitmapMetadataHandler->addMetadata( $array, $type ); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if ( $exifData ) { |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if ( substr( $exifData, 0, 6 ) === "Exif\x00\x00" ) { |
| 211 |
$exifData = substr( $exifData, 6 ); |
| 212 |
} |
| 213 |
$tmpFile = MediaWikiServices::getInstance()-> |
| 214 |
getTempFSFileFactory()-> |
| 215 |
newTempFSFile( 'webp-exif_', 'tiff' ); |
| 216 |
|
| 217 |
$exifDataFile = $tmpFile->getPath(); |
| 218 |
file_put_contents( $exifDataFile, $exifData ); |
| 219 |
$byteOrder = BitmapMetadataHandler::getTiffByteOrder( $exifDataFile ); |
| 220 |
$bitmapMetadataHandler->getExif( $exifDataFile, $byteOrder ); |
| 221 |
} |
| 222 |
return [ 'media-metadata' => $bitmapMetadataHandler->getMetadataArray() ]; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private static function extractChunk( $chunk, $filename ) { |
| 231 |
if ( $chunk['size'] > self::MAX_METADATA_CHUNK_SIZE || $chunk['size'] < 1 ) { |
| 232 |
return null; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
return file_get_contents( $filename, false, null, $chunk['start'] + 8, $chunk['size'] ); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
protected static function decodeLossyChunkHeader( $header ) { |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
$syncCode = substr( $header, 11, 3 ); |
| 250 |
if ( $syncCode !== "\x9D\x01\x2A" ) { |
| 251 |
wfDebugLog( 'WebP', __METHOD__ . ': Invalid sync code: ' . |
| 252 |
bin2hex( $syncCode ) ); |
| 253 |
return []; |
| 254 |
} |
| 255 |
|
| 256 |
$imageSize = unpack( 'v2', substr( $header, 14, 4 ) ); |
| 257 |
|
| 258 |
return [ |
| 259 |
'compression' => 'lossy', |
| 260 |
'width' => $imageSize[1] & 0x3FFF, |
| 261 |
'height' => $imageSize[2] & 0x3FFF |
| 262 |
]; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public static function decodeLosslessChunkHeader( $header ) { |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if ( $header[8] !== "\x2F" ) { |
| 275 |
wfDebugLog( 'WebP', __METHOD__ . ': Invalid signature: ' . |
| 276 |
bin2hex( $header[8] ) ); |
| 277 |
return []; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
$imageSize = unpack( 'C4', substr( $header, 9, 4 ) ); |
| 282 |
return [ |
| 283 |
'compression' => 'lossless', |
| 284 |
'width' => ( $imageSize[1] | ( ( $imageSize[2] & 0x3F ) << 8 ) ) + 1, |
| 285 |
'height' => ( ( ( $imageSize[2] & 0xC0 ) >> 6 ) | |
| 286 |
( $imageSize[3] << 2 ) | ( ( $imageSize[4] & 0x0F ) << 10 ) ) + 1 |
| 287 |
]; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public static function decodeExtendedChunkHeader( $header ) { |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
$flags = unpack( 'c', substr( $header, 8, 1 ) ); |
| 300 |
|
| 301 |
|
| 302 |
$width = unpack( 'V', substr( $header, 12, 3 ) . "\x00" ); |
| 303 |
$height = unpack( 'V', substr( $header, 15, 3 ) . "\x00" ); |
| 304 |
|
| 305 |
return [ |
| 306 |
'compression' => 'unknown', |
| 307 |
'animated' => ( $flags[1] & self::VP8X_ANIM ) === self::VP8X_ANIM, |
| 308 |
'transparency' => ( $flags[1] & self::VP8X_ALPHA ) === self::VP8X_ALPHA, |
| 309 |
'width' => ( $width[1] & 0xFFFFFF ) + 1, |
| 310 |
'height' => ( $height[1] & 0xFFFFFF ) + 1 |
| 311 |
]; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public function canRender( $file ) { |
| 319 |
if ( $this->isAnimatedImage( $file ) ) { |
| 320 |
return false; |
| 321 |
} |
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public function isAnimatedImage( $image ) { |
| 330 |
$metadata = $image->getMetadataArray(); |
| 331 |
if ( isset( $metadata['animated'] ) && $metadata['animated'] === true ) { |
| 332 |
return true; |
| 333 |
} |
| 334 |
|
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
public function canAnimateThumbnail( $file ) { |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public function getThumbType( $ext, $mime, $params = null ) { |
| 351 |
return [ 'png', 'image/png' ]; |
| 352 |
} |
| 353 |
|
| 354 |
protected function hasGDSupport() { |
| 355 |
return function_exists( 'gd_info' ) && ( gd_info()['WebP Support'] ?? false ); |
| 356 |
} |
| 357 |
|
| 358 |
public function getCommonMetaArray( File $image ) { |
| 359 |
$meta = $image->getMetadataArray(); |
| 360 |
return $meta['media-metadata'] ?? []; |
| 361 |
} |
| 362 |
|
| 363 |
public function formatMetadata( $image, $context = false ) { |
| 364 |
$meta = $this->getCommonMetaArray( $image ); |
| 365 |
if ( !$meta ) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
return $this->formatMetadataHelper( $meta, $context ); |
| 370 |
} |
| 371 |
} |