X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2Fmedia%2FSVG.php;h=6c2d98059aa5f17f9d228fe98a5e210069c367fd;hb=37013dc9207a585a82e066ddddc406a471378a33;hp=242b2645b72df9cf8be88a8659fee4d3bda7b48d;hpb=5583dacd0af5ff2d730f7c2c5ef6cb081d7430de;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/media/SVG.php b/includes/media/SVG.php index 242b2645b7..6c2d98059a 100644 --- a/includes/media/SVG.php +++ b/includes/media/SVG.php @@ -12,6 +12,8 @@ * @ingroup Media */ class SvgHandler extends ImageHandler { + const SVG_METADATA_VERSION = 2; + function isEnabled() { global $wgSVGConverters, $wgSVGConverter; if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) { @@ -26,6 +28,22 @@ class SvgHandler extends ImageHandler { return true; } + function isVectorized( $file ) { + return true; + } + + function isAnimatedImage( $file ) { + # TODO: detect animated SVGs + $metadata = $file->getMetadata(); + if ( $metadata ) { + $metadata = $this->unpackMetadata( $metadata ); + if( isset( $metadata['animated'] ) ) { + return $metadata['animated']; + } + } + return false; + } + function normaliseParams( $image, &$params ) { global $wgSVGMaxSize; if ( !parent::normaliseParams( $image, $params ) ) { @@ -61,7 +79,7 @@ class SvgHandler extends ImageHandler { return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, wfMsg( 'thumbnail_dest_directory' ) ); } - + $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight ); if( $status === true ) { return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath ); @@ -69,7 +87,7 @@ class SvgHandler extends ImageHandler { return $status; // MediaTransformError } } - + /* * Transform an SVG file to PNG * This function can be called outside of thumbnail contexts @@ -107,8 +125,16 @@ class SvgHandler extends ImageHandler { return true; } - function getImageSize( $image, $path ) { - return wfGetSVGsize( $path ); + function getImageSize( $file, $path, $metadata = false ) { + if ( $metadata === false ) { + $metadata = $file->getMetaData(); + } + $metadata = $this->unpackMetaData( $metadata ); + + if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) { + return array( $metadata['width'], $metadata['height'], 'SVG', + "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" ); + } } function getThumbType( $ext, $mime, $params = null ) { @@ -122,4 +148,74 @@ class SvgHandler extends ImageHandler { $wgLang->formatNum( $file->getHeight() ), $wgLang->formatSize( $file->getSize() ) ); } + + function getMetadata( $file, $filename ) { + $metadata = array(); + try { + $metadata = SVGMetadataExtractor::getMetadata( $filename ); + } catch( Exception $e ) { + // Broken file? + wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" ); + return '0'; + } + $metadata['version'] = self::SVG_METADATA_VERSION; + return serialize( $metadata ); + } + + function unpackMetadata( $metadata ) { + $unser = @unserialize( $metadata ); + if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) { + return $unser; + } else { + return false; + } + } + + function getMetadataType( $image ) { + return 'parsed-svg'; + } + + function isMetadataValid( $image, $metadata ) { + return $this->unpackMetadata( $metadata ) !== false; + } + + function visibleMetadataFields() { + $fields = array( 'title', 'description', 'animated' ); + return $fields; + } + + function formatMetadata( $file ) { + $result = array( + 'visible' => array(), + 'collapsed' => array() + ); + $metadata = $file->getMetadata(); + if ( !$metadata ) { + return false; + } + $metadata = $this->unpackMetadata( $metadata ); + if ( !$metadata ) { + return false; + } + unset( $metadata['version'] ); + unset( $metadata['metadata'] ); /* non-formatted XML */ + + /* TODO: add a formatter + $format = new FormatSVG( $metadata ); + $formatted = $format->getFormattedData(); + */ + + // Sort fields into visible and collapsed + $visibleFields = $this->visibleMetadataFields(); + foreach ( $metadata as $name => $value ) { + $tag = strtolower( $name ); + self::addMeta( $result, + in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed', + 'svg', + $tag, + $value + ); + } + return $result; + } }