Extension:EmbedImg

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
EmbedImg

Release status: beta

Implementation Parser function
Description Allows to embed external images with parameters on a wiki page
Author(s) Dmitry Shurupov
Last version 0.1
MediaWiki 1.10+
Database changes no
License GPL
Download see below
Check usage and version matrix

The EmbedImg extension adds the <img> tag for embedding external images. It supports width & height parameters.

Installation [edit]

  1. Create a new EmbedImg folder in your MediaWiki extensions directory.
  2. Copy EmbedImg.php to the new file EmbedImg.php in the folder that you just created.
  3. Enable the extension by adding this line to the bottom of your LocalSettings.php:
require_once("$IP/extensions/EmbedImg/EmbedImg.php");

Usage [edit]

  • <img>http://some.site.com/with/an/image.png</img>
  • <img size=420>http://some.site.com/with/an/image.png</img>
  • <img size=420x840>http://some.site.com/with/an/image.png</img>

Other Uses [edit]

Despite the ObsoleteExtension tag shown above, this extension is still required to do fancier tricks with external images, like this one:

<div class="thumb tright"><div class="thumbinner" style="width:400px;">
<span class="plainlinks">[http://some.site.com/ <img size=400>http://some.site.com/with/an/image.png</img>]</span>
<div class="thumbcaption"><span class="plainlinks">[http://some.site.com/ Your caption.]</span></div></div></div>

Options [edit]

  • size -- defines images width / width and height. Just a number, no "px" required.

Limitations [edit]

  • PNG, GIF, JPG/JPEG file extensions only.
  • Height only is not available (however, width only is okay).

Source of EmbedImg.php [edit]

<?php
/**
 * MediaWiki EmbedImg extension
 *
 * @version 0.1
 * @author Dmitry Shurupov
 * @link https://www.mediawiki.org/wiki/Extension:EmbedImg
 */
 
if ( !defined( 'MEDIAWIKI' ) )
  die( 'This file is part of MediaWiki. It is not a valid entry point.' );
 
$wgExtensionCredits['parserhook'][] = array(
  'name' => 'EmbedImg',
  'author' => 'Dmitry Shurupov',
  'version' => '0.1',
  'url' => 'https://www.mediawiki.org/wiki/Extension:EmbedImg',
  'description' => 'Allows to embed external images with parameters on a wiki page',
);
 
$wgExtensionFunctions[] = 'registerEmbedImgHandler';
 
function registerEmbedImgHandler () {
  global $wgParser;
  $wgParser->setHook( 'img', 'embedImgHandler' );
}
 
function makeHTMLforImg ($path, $width, $height) {
  return '<img src="'.$path.'" '.(isSet($width) ? 'width="'.$width.'" ' : '').(isSet($height) ? 'height="'.$height.'" ' : '').' />';
}
 
function embedImgHandler ($input, $argv) {
  if (!isSet($input)) return '<font color="red">Error: empty param in &lt;img&gt;!</font>';
 
  if (preg_match('/^https?\:\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@\?\^\=\%\&:\/\~\+\#]*[\w\-\@\?\^\=\%\&\/\~\+\#])?\.(jpe?g|png|gif|JPE?G|PNG|GIF|jpg|JPG)$/', $input)) {
    if (isSet($argv['size']) && preg_match('/^([0-9]+)(x([0-9]+))?(px)?$/', $argv['size'], $matches)) {
      if (isSet($matches[3])) return makeHTMLforImg($input, $matches[1], $matches[3]);
      else if (isSet($matches[1])) return makeHTMLforImg($input, $matches[1], NULL);
    }
    return makeHTMLforImg($input, NULL, NULL);
  } else return '<font color="red">Error: bad URI in &lt;img&gt;!</font>';
}

See also [edit]