Extension:EmbedImg

From MediaWiki.org

Jump to: navigation, search

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
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.x+
License GPL
Download see below

check usage (experimental)

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

[edit] Installation

  1. Copy EmbedImg.php to the new file EmbedImg.php in your MediaWiki extensions directory.
  2. Enable the extension by adding this line to the bottom of your LocalSettings.php:
require_once('extensions/EmbedImg.php');

[edit] Usage

  • <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>

[edit] Options

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

[edit] Limitations

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

[edit] Source of EmbedImg.php

<?php
/**
 * MediaWiki EmbedImg extension
 *
 * @version 0.1
 * @author Dmitry Shurupov
 * @link http://www.mediawiki.org/wiki/Extension:EmbedImg
 */
 
$wgExtensionCredits['parserhook'][] = array(
  'name' => 'EmbedImg',
  'author' => 'Dmitry Shurupov',
  'version' => '0.1',
  'url' => 'http://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('/^http\:\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@\?\^\=\%\&:\/\~\+\#]*[\w\-\@\?\^\=\%\&\/\~\+\#])?\.(jpe?g|png|gif)$/', $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>';
}
?>