Jump to content

Extension:ImgTag

From mediawiki.org
MediaWiki extensions manual
ImgTag
Release status: stable
Implementation Tag , Parser function
Description Adds back the ‎<img /> tag removed in MediaWiki 1.42
Author(s) PetraMagnatalk
Latest version v0.2.1
MediaWiki 1.43+
Database changes No
License GNU General Public License 2.0 or later
Download
  • $wgImgTagSanitizeDomain
  • $wgImgTagProtocols
  • $wgImgTagDomains

The ImgTag extension adds back the ‎<img /> tag removed in MediaWiki 1.42. It also adds a parser function {{#fileused that marks a local file as being used on the page.

Installation

[edit]
  • Download and place the file(s) in a directory called ImgTag in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'ImgTag' );
    
  • Configure as required.
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Configuration

[edit]
$wgImgTagSanitizeDomain
Whether the domain name of the image tag's src attribute should be sanitized.
Default: true.
$wgImgTagDomains
An array of permitted domains in the src attribute. Disabled if $wgImgTagSanitizeDomain is set to false.
Default: ['upload.wikimedia.org'].
$wgImgTagProtocols
An array of permitted protocols for loading images.
Default: ['http', 'https'].

Usage examples

[edit]

Below is a basic example.

<img src="https://upload.wikimedia.org/wikipedia/commons/8/80/Wikipedia-logo-v2.svg" width="100px" class="some-class" alt="Logo of Wikipedia" />

You can also use a parser function:

{{#img:
|src=https://upload.wikimedia.org/wikipedia/commons/8/80/Wikipedia-logo-v2.svg
|width=100px
|class=some-class
|alt=Logo of Wikipedia
}}

This example registers a file as being used on a page using the parser function {{#fileused. Note that alternatives such as using Scribunto's exists function is expensive and therefore not scalable.

<img src="{{filepath:Logo.png}}" width="50px" />
{{#fileused:Logo.png}}

Permitted attributes

[edit]

Below is an example element using all permitted attributes. Note that this example should not be used in practice because some attributes render others useless. See Mozilla's documentation for more details on how to use each attribute.

<img
 id="img-id"
 style="border: 1px solid black"
 alt="An example image"
 title="Example"
 width="200px"
 height="auto"
 class="some-class"
 fetchpriority="low"
 loading="lazy"
 sizes="(width <= 600px) 200px, 50vw"/>

Caveats

[edit]

Although ‎<img> is valid HTML markup since it is a self-closing tag, MediaWiki cannot properly parse it, so it is not supported by this extension. All usages must end with a slash (i.e. <img ... />). Some extensions with similar functionalities can automatically add a slash at the end: see AddImgTag and MoeImgTag listed at the end of the page.

See also

[edit]