26use Wikimedia\ScopedCallback;
41 'originalwidth' =>
'ImageWidth',
42 'originalheight' =>
'ImageLength',
43 'description' =>
'ImageDescription',
44 'title' =>
'ObjectName',
50 wfDebug(
"\$wgSVGConverter is invalid, disabling SVG rendering." );
71 # @todo Detect animated SVGs
73 if ( isset( $metadata[
'animated'] ) ) {
74 return $metadata[
'animated'];
95 if ( isset( $metadata[
'translations'] ) ) {
96 foreach ( $metadata[
'translations'] as
$lang => $langType ) {
98 $langList[] = strtolower(
$lang );
102 return array_unique( $langList );
121 foreach ( $svgLanguages as $svgLang ) {
122 if ( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) {
125 $trimmedSvgLang = $svgLang;
126 while ( strpos( $trimmedSvgLang,
'-' ) !==
false ) {
127 $trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang,
'-' ) );
128 if ( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) {
143 return $params[
'lang'] ?? $params[
'targetlang'] ??
'en';
171 if ( parent::normaliseParams( $image, $params ) ) {
191 # Don't make an image bigger than wgMaxSVGSize on the smaller side
192 if ( $params[
'physicalWidth'] <= $params[
'physicalHeight'] ) {
194 $srcWidth = $image->getWidth( $params[
'page'] );
195 $srcHeight = $image->getHeight( $params[
'page'] );
197 $params[
'physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
$wgSVGMaxSize );
200 $srcWidth = $image->getWidth( $params[
'page'] );
201 $srcHeight = $image->getHeight( $params[
'page'] );
202 $params[
'physicalWidth'] = File::scaleHeight( $srcHeight, $srcWidth,
$wgSVGMaxSize );
207 if ( isset( $params[
'targetlang'] ) && !$image->getMatchedLanguage( $params[
'targetlang'] ) ) {
208 unset( $params[
'targetlang'] );
222 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
226 $clientWidth = $params[
'width'];
227 $clientHeight = $params[
'height'];
228 $physicalWidth = $params[
'physicalWidth'];
229 $physicalHeight = $params[
'physicalHeight'];
232 if ( $flags & self::TRANSFORM_LATER ) {
237 if ( isset( $metadata[
'error'] ) ) {
238 $err =
wfMessage(
'svg-long-error', $metadata[
'error'][
'message'] );
245 wfMessage(
'thumbnail_dest_directory' ) );
248 $srcPath = $image->getLocalRefPath();
249 if ( $srcPath ===
false ) {
251 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
255 $params[
'width'], $params[
'height'],
264 $lnPath =
"$tmpDir/" . basename( $srcPath );
265 $ok = mkdir( $tmpDir, 0771 );
268 sprintf(
'Thumbnail failed on %s: could not create temporary directory %s',
271 $params[
'width'], $params[
'height'],
272 wfMessage(
'thumbnail-temp-create' )->text()
275 $ok = symlink( $srcPath, $lnPath );
277 $cleaner =
new ScopedCallback(
static function () use ( $tmpDir, $lnPath ) {
278 Wikimedia\suppressWarnings();
281 Wikimedia\restoreWarnings();
285 sprintf(
'Thumbnail failed on %s: could not link %s to %s',
288 $params[
'width'], $params[
'height'],
293 $status = $this->
rasterize( $lnPath, $dstPath, $physicalWidth, $physicalHeight,
$lang );
294 if ( $status ===
true ) {
312 public function rasterize( $srcPath, $dstPath, $width, $height,
$lang =
false ) {
320 if ( !is_callable( $func ) ) {
323 $err = $func( $srcPath,
330 $retval = (bool)$err;
334 [
'$path/',
'$width',
'$height',
'$input',
'$output' ],
338 Shell::escape( $srcPath ),
339 Shell::escape( $dstPath ) ],
344 if (
$lang !==
false ) {
345 $env[
'LANG'] =
$lang;
348 wfDebug( __METHOD__ .
": $cmd" );
353 if ( $retval != 0 || $removed ) {
362 $im =
new Imagick( $srcPath );
363 $im->setImageFormat(
'png' );
364 $im->setBackgroundColor(
'transparent' );
365 $im->setImageDepth( 8 );
367 if ( !$im->thumbnailImage( intval( $width ), intval( $height ),
false ) ) {
368 return 'Could not resize image';
370 if ( !$im->writeImage( $dstPath ) ) {
371 return "Could not write to $dstPath";
376 return [
'png',
'image/png' ];
392 if ( isset( $metadata[
'error'] ) ) {
393 return wfMessage(
'svg-long-error', $metadata[
'error'][
'message'] )->text();
399 $msg =
wfMessage(
'svg-long-desc-animated' );
404 $msg->numParams(
$file->getWidth(),
$file->getHeight() )->params( $size );
406 return $msg->parse();
419 $metadata += $svgReader->getMetadata();
420 }
catch ( Exception $e ) {
422 $metadata[
'error'] = [
423 'message' => $e->getMessage(),
424 'code' => $e->getCode()
426 wfDebug( __METHOD__ .
': ' . $e->getMessage() );
430 'width' => $metadata[
'width'] ?? 0,
431 'height' => $metadata[
'height'] ?? 0,
432 'metadata' => $metadata
437 if ( isset( $unser[
'version'] ) && $unser[
'version'] == self::SVG_METADATA_VERSION ) {
453 if ( !isset( $meta[
'originalWidth'] ) ) {
462 $fields = [
'objectname',
'imagedescription' ];
478 if ( !$metadata || isset( $metadata[
'error'] ) ) {
491 foreach ( $metadata as $name => $value ) {
492 $tag = strtolower( $name );
493 if ( isset( self::$metaConversion[$tag] ) ) {
494 $tag = strtolower( self::$metaConversion[$tag] );
501 in_array( $tag, $visibleFields ) ?
'visible' :
'collapsed',
508 return $showMeta ? $result :
false;
517 if ( in_array( $name, [
'width',
'height' ] ) ) {
519 return ( $value > 0 );
520 } elseif ( $name ==
'lang' ) {
523 || !MediaWikiServices::getInstance()->getLanguageNameUtils()
524 ->isValidCode( $value )
543 if ( $code !==
'en' ) {
544 $lang =
'lang' . strtolower( $code ) .
'-';
546 if ( !isset( $params[
'width'] ) ) {
550 return "$lang{$params['width']}px";
555 if ( preg_match(
'/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/i', $str, $m ) ) {
556 return [
'width' => array_pop( $m ),
'lang' => $m[1] ];
557 } elseif ( preg_match(
'/^(\d+)px$/', $str, $m ) ) {
558 return [
'width' => $m[1],
'lang' =>
'en' ];
565 return [
'img_lang' =>
'lang',
'img_width' =>
'width' ];
573 $scriptParams = [
'width' => $params[
'width'] ];
574 if ( isset( $params[
'lang'] ) ) {
575 $scriptParams[
'lang'] = $params[
'lang'];
578 return $scriptParams;
583 if ( !$metadata || isset( $metadata[
'error'] ) ) {
587 foreach ( $metadata as $name => $value ) {
588 $tag = strtolower( $name );
589 if ( $tag ===
'originalwidth' || $tag ===
'originalheight' ) {
594 if ( isset( self::$metaConversion[$tag] ) ) {
595 $tag = self::$metaConversion[$tag];
596 $stdMetadata[$tag] = $value;
$wgSVGConverter
Pick a converter defined in $wgSVGConverters.
$wgSVGMaxSize
Don't scale a SVG larger than this.
$wgSVGConverterPath
If not in the executable PATH, specify the SVG converter path.
$wgSVGConverters
Scalable Vector Graphics (SVG) may be uploaded as images.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTempDir()
Tries to get the system directory for temporary files.
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
wfHostname()
Get host name of the current machine, for use in error reporting.
wfShellExecWithStderr( $cmd, &$retval=null, $environ=[], $limits=[])
Execute a shell command, returning both stdout and stderr.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Implements some public methods and some protected utility functions which are required by multiple ch...
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Media handler abstract base class for images.
validateParam( $name, $value)
isVectorized( $file)
The material is vectorized and thus scaling is lossless.
normaliseParams( $image, &$params)
formatMetadata( $file, $context=false)
parseParamString( $str)
Parse a param string made with makeParamString back into an array.array|bool Array of parameters or f...
mustRender( $file)
True if handled types cannot be displayed directly in a browser but can be rendered.
getScriptParams( $params)
makeParamString( $params)
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
static array $metaConversion
A list of metadata tags that can be converted to the commonly used exif tags.
doTransform( $image, $dstPath, $dstUrl, $params, $flags=0)
validateMetadata( $unser)
getLanguageFromParams(array $params)
Determines render language from image parameters.
getAvailableLanguages(File $file)
Which languages (systemLanguage attribute) is supported.
getLongDesc( $file)
Subtitle for the image.
normaliseParamsInternal( $image, $params)
Code taken out of normaliseParams() for testability.
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
getThumbType( $ext, $mime, $params=null)
Get the thumbnail extension and MIME type for a given source MIME type.
getDefaultRenderLanguage(File $file)
What language to render file in if none selected.
rasterize( $srcPath, $dstPath, $width, $height, $lang=false)
Transform an SVG file to PNG This function can be called outside of thumbnail contexts.
getSizeAndMetadata( $state, $filename)
isEnabled()
False if the handler is disabled for all files.
canAnimateThumbnail( $file)
We do not support making animated svg thumbnails.
visibleMetadataFields()
Get a list of metadata items which should be displayed when the metadata table is collapsed.
static rasterizeImagickExt( $srcPath, $dstPath, $width, $height)
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
getMatchedLanguage( $userPreferredLanguage, array $svgLanguages)
SVG's systemLanguage matching rules state: 'The systemLanguage attribute ... [e]valuates to "true" if...
const SVG_METADATA_VERSION
getParamMap()
Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to iden...
Media transform output for images.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
if(!is_readable( $file)) $ext
if(!isset( $args[0])) $lang