36 'svg' =>
'image/svg+xml',
80 if ( is_array( $this->descriptor ) && isset( $this->descriptor[
'lang'] ) ) {
81 foreach ( array_keys( $this->descriptor[
'lang'] ) as $langList ) {
82 if ( strpos( $langList,
',' ) !==
false ) {
83 $this->descriptor[
'lang'] += array_fill_keys(
84 explode(
',', $langList ),
85 $this->descriptor[
'lang'][$langList]
87 unset( $this->descriptor[
'lang'][$langList] );
92 if ( is_array( $this->descriptor ) ) {
93 unset( $this->descriptor[
'deprecated'] );
102 $extensions = array_unique( $extensions );
103 if ( count( $extensions ) !== 1 ) {
104 throw new InvalidArgumentException(
105 "File type for different image files of '$name' not the same in module '$module'"
108 $ext = $extensions[0];
109 if ( !isset( self::$fileTypes[
$ext] ) ) {
110 throw new InvalidArgumentException(
111 "Invalid file type for image files of '$name' (valid: svg, png, gif, jpg) in module '$module'"
114 $this->extension =
$ext;
141 return array_keys( $this->variants );
153 if ( !is_array( $desc ) ) {
156 if ( isset( $desc[
'lang'] ) ) {
157 $contextLang =
$context->getLanguage();
158 if ( isset( $desc[
'lang'][$contextLang] ) ) {
159 return $this->
getLocalPath( $desc[
'lang'][$contextLang] );
161 $fallbacks = Language::getFallbacksFor( $contextLang, Language::STRICT_FALLBACKS );
162 foreach ( $fallbacks as
$lang ) {
163 if ( isset( $desc[
'lang'][
$lang] ) ) {
168 if ( isset( $desc[
$context->getDirection()] ) ) {
171 if ( isset( $desc[
'default'] ) ) {
184 return $path->getLocalPath();
187 return "{$this->basePath}/$path";
197 if ( $format ===
'rasterized' && $this->extension ===
'svg' ) {
211 return self::$fileTypes[
$ext];
227 'variant' => $variant,
231 $query[
'lang'] =
$context->getLanguage();
234 $query[
'skin'] =
$context->getSkin();
235 $rl =
$context->getResourceLoader();
251 $contents = $this->
getImageData( $context, $variant, $format );
252 return CSSMin::encodeStringAsDataURI( $contents,
$type );
272 if ( $variant ===
false ) {
275 if ( $format ===
false ) {
280 if ( !file_exists(
$path ) ) {
281 throw new MWException(
"File '$path' does not exist" );
285 return file_get_contents(
$path );
288 if ( $variant && isset( $this->variants[$variant] ) ) {
294 file_get_contents(
$path );
297 if ( $format ===
'rasterized' ) {
300 wfDebugLog(
'ResourceLoaderImage', __METHOD__ .
" failed to rasterize for $path" );
320 header(
'Content-Type: ' . $mime );
321 header(
'Content-Disposition: ' .
333 $dom =
new DOMDocument;
334 $dom->loadXML( file_get_contents( $this->
getPath( $context ) ) );
335 $root = $dom->documentElement;
337 $wrapper = $dom->createElementNS(
'http://www.w3.org/2000/svg',
'g' );
339 while ( $root->firstChild ) {
340 $node = $root->firstChild;
342 if ( !$titleNode && $node->nodeType === XML_ELEMENT_NODE && $node->tagName ===
'title' ) {
346 $wrapper->appendChild( $node );
350 $root->appendChild( $titleNode );
352 $root->appendChild( $wrapper );
353 $wrapper->setAttribute(
'fill', $variantConf[
'color'] );
354 return $dom->saveXML();
368 $dom =
new DOMDocument;
369 $dom->loadXML( $svg );
370 foreach ( $dom->getElementsByTagName(
'path' ) as $node ) {
371 $pathData = $node->getAttribute(
'd' );
374 $pathData = preg_replace(
'/(-?)(\d*\.\d+|\d+)/',
' ${1}0$2 ', $pathData );
376 $pathData = preg_replace(
'/([ -])0(\d)/',
'$1$2', $pathData );
377 $node->setAttribute(
'd', $pathData );
379 return $dom->saveXML();
418 $process = proc_open(
420 [ 0 => [
'pipe',
'r' ], 1 => [
'pipe',
'w' ] ],
424 if ( is_resource( $process ) ) {
425 fwrite( $pipes[0], $svg );
427 $png = stream_get_contents( $pipes[1] );
429 proc_close( $process );
431 return $png ?:
false;
437 $tempFilenameSvg = tempnam(
wfTempDir(),
'ResourceLoaderImage' );
438 $tempFilenamePng = tempnam(
wfTempDir(),
'ResourceLoaderImage' );
440 file_put_contents( $tempFilenameSvg, $svg );
442 $svgReader =
new SVGReader( $tempFilenameSvg );
443 $metadata = $svgReader->getMetadata();
444 if ( !isset( $metadata[
'width'] ) || !isset( $metadata[
'height'] ) ) {
445 unlink( $tempFilenameSvg );
456 unlink( $tempFilenameSvg );
459 if (
$res ===
true ) {
460 $png = file_get_contents( $tempFilenamePng );
461 unlink( $tempFilenamePng );
464 return $png ?:
false;
474 return is_array( $this->descriptor ) && (
475 isset( $this->descriptor[
'ltr'] ) ||
476 isset( $this->descriptor[
'rtl'] ) ||
477 isset( $this->descriptor[
'lang'] ) );
$wgSVGConverter
Pick a converter defined in $wgSVGConverters.
$wgSVGConverterPath
If not in the executable PATH, specify the SVG converter path.
wfTempDir()
Tries to get the system directory for temporary files.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
static makeContentDisposition( $type, $filename='')
Build a Content-Disposition header value per RFC 6266.
Context object that contains information about the state of a specific ResourceLoader web request.
An object to represent a path to a JavaScript/CSS file, along with a remote and local base path,...
Class encapsulating an image used in a ResourceLoaderImageModule.
rasterize( $svg)
Convert passed image data, which is assumed to be SVG, to PNG.
variantize( $variantConf, ResourceLoaderContext $context)
Convert this image, which is assumed to be SVG, to given variant.
getUrl(ResourceLoaderContext $context, $script, $variant, $format)
Get the load.php URL that will produce this image.
getPath(ResourceLoaderContext $context)
Get the path to image file for given context.
massageSvgPathdata( $svg)
Massage the SVG image data for converters which don't understand some path data syntax.
getMimeType( $format='original')
Get the MIME type of the image.
string null $defaultColor
getModule()
Get name of the module this image belongs to.
getExtension( $format='original')
Get the extension of the image.
__construct( $name, $module, $descriptor, $basePath, $variants, $defaultColor=null)
varyOnLanguage()
Check if the image depends on the language.
getDataUri(ResourceLoaderContext $context, $variant, $format)
Get the data: URI that will produce this image.
sendResponseHeaders(ResourceLoaderContext $context)
Send response headers (using the header() function) that are necessary to correctly serve the image d...
getName()
Get name of this image.
getVariants()
Get the list of variants this image can be converted to.
static array $fileTypes
Map of allowed file extensions to their MIME types.
getImageData(ResourceLoaderContext $context, $variant=false, $format=false)
Get actual image data for this image.
rasterize( $srcPath, $dstPath, $width, $height, $lang=false)
Transform an SVG file to PNG This function can be called outside of thumbnail contexts.
if(!is_readable( $file)) $ext
if(!isset( $args[0])) $lang