Wikipedia:Wikipedia Signpost/2008-03-03/Tutorial

This week's tutorial explains the syntax for the ImageMap extension of the MediaWiki software, which allows images and sections thereof, of any shape and size, to link to any type of page link that can be linked to via text (any valid wikilink, interwiki link, or external link).

A basic understanding of image syntax is assumed.

When to use ImageMap
An ImageMap does not behave the way a normal image does, where clicking anywhere on an image opens its image description page. Instead, with an ImageMap, clicking on the image is the equivalent of clicking on a link; different sections of the image may link to different pages, including external links. ImageMap should generally only be used with free images (that is, those that do not fall under the non-free content criteria), because creating an ImageMap can be regarded as a substantial modification of an image.

ImageMaps are generally used within templates, where it is desirable to have images or icons link to particular pages or sections of a page, and in portals, where it links icons for related portals to that portal and links the logos of various projects in the interwiki section to their respective projects. If ImageMap does not add to the presentation of a template, portal, or article, it should probably not be used, since the ideal behaviour—for transparency with image licensing—is to have the image link to the description page.

To summarize:
 * If the image is non-free content, it is advisable to not use ImageMap.
 * If ImageMap would not add significantly to the presentation of the page upon which it is used, one should probably not use ImageMap on that page.

Creating an ImageMap
To create an ImageMap, one can use either the tags  and , or the magic word (and ParserFunction). The format is generally preferable (a good practice) for simple ImageMaps, but does not (currently) function properly with template parameters, magic words, or ParserFunctions. In this case, the format is a simple and effective workaround, which replaced older, usability-troublesome CSS workarounds for templates that created images linked to alternate pages, such as.

To summarize:
 * There are two syntactically correct containers for ImageMap syntax:  and , or   and.
 * The former is best practice, but only the latter allows the use of template parameters, magic words, and ParserFunctions.

The image line
The first line of any ImageMap must be a line specifying the image being used. If no image is specified, or the image that is specified does not exist, then the ImageMap will produce the following error message:



Note, however, that there must be a linebreak after the imagemap tag (of either form) and before this line. This image code is identical in syntax to normal image syntax, albeit without the surrounding square brackets. All of the basic image syntax and extended image syntax applies, though options such as  may not be fully supported by ImageMap. The text provided as a comment in the image syntax on this line is not visible except as alternative text when the image is unavailable, except for options such as  which might use it.
 * For example, in the case that the syntax for the normal, unmapped image was, the ImageMap code for the first line would be

To summarize:


 * Construct the image syntax normally
 * Remove the  and   from around the image syntax
 * Use the resulting text as the image line

Mapping an area to a link
After the first line, any valid lines are acceptable. Usually the second line, and perhaps more following it, contain the area or areas which the ImageMap uses to map to the image. An ImageMap must have at least one area, or default target, specified, or it outputs an error message like the following:



Each line that describes an area can be subdivided into three parts: the type of area, numerical parameters for that area as necessary, and a valid wikilink or external link. These parts are separated on the line by spaces. When hovering over an area, the link destination, or the alternate text (if provided), is displayed as the tooltip when applicable.

Numerical parameters often take the form of coordinates; these coordinates are the number of pixels from the upper-left-hand corner given the default size of the image. When a 150px-wide image is scaled to 50px, the upper-right-hand corner will be designated  despite the image as rendered only being 50px wide.

When two or more areas overlap, the area specified first in the code takes precedence.

There are four types of area that can be used. For the more complex areas, it is easiest to use the visual ImageMap editor on the toolserver:


 * Default:A default area is specified by the type  and the link. It specifies a default target for the image where no areas are specified which overrides the default behaviour of no link for otherwise unlinked areas. Most simple ImageMaps with a single target should use only this type for simplicity. This type should only be specified once, and should be the last type specified by order.
 * For example, the following would be a valid line:
 * Rectangle:The simplest shape is a rectangle. First, the type  should be specified, and then two points are specified using four numbers separated only by spaces. The first two numbers correspond to the top-left corner of the rectangle, and the second pair correspond to its bottom-right corner. The number pairs are distances, in pixels, first horizontally and then vertically from the top-left pixel of the image, relative to its default size. For example, the top-left pixel of the image has the coordinates , while the pixel two to the right and one downwards from it would be  . After these coordinates are specified, any valid link, in normal wiki syntax, is used.
 * For example, the following would be a valid line:
 * Circle:Many buttons, icons, and other images conducive to use with ImageMap are circular. For this, one uses the type . After specifying the coordinates for the centre of the circle as for the corner of a rectangle, a third single number specifies the radius of the circle, in pixels. After this is specified, any valid link, as for the rectangle, is used.
 * For example, the following might be a valid line:
 * Polygon:Sometimes complex shapes are required for ImageMaps. This, using the type  allows one to specify a polygon as an area by specifying the coordinates for each point of the polygon in turn, using the same method used to define points for the rectangle. Irregular shapes may therefore be properly mapped. When polygons are defined, they must be placed before any other shapes in the code.  The number of points used should be kept to a minimum, especially as polygons with many points sometimes do not work. Polygons with fewer than 20 points should always work. For more complex shapes, using multiple overlapping polygons is an effective workaround and helps simplify each shape. After the last point, any valid link can be used.
 * For example, the following might be a valid line, which would create a triangle in the upper-left corner of an image:
 * Polygon:Sometimes complex shapes are required for ImageMaps. This, using the type  allows one to specify a polygon as an area by specifying the coordinates for each point of the polygon in turn, using the same method used to define points for the rectangle. Irregular shapes may therefore be properly mapped. When polygons are defined, they must be placed before any other shapes in the code.  The number of points used should be kept to a minimum, especially as polygons with many points sometimes do not work. Polygons with fewer than 20 points should always work. For more complex shapes, using multiple overlapping polygons is an effective workaround and helps simplify each shape. After the last point, any valid link can be used.
 * For example, the following might be a valid line, which would create a triangle in the upper-left corner of an image:
 * For example, the following might be a valid line, which would create a triangle in the upper-left corner of an image:

The image description icon
Since images should, as a good practice, include a link to the image description page, ImageMap includes, by default, an information icon on the image linking to the image description page. This by default displays on the bottom-right of an image, but can be changed by using  and a location: any of ,  ,  ,  , or   to disable it completely.


 * The  parameter allows you to change the location of the information icon, or disable it.
 * For example, the following would be a valid line disabling the description icon:

Overview
Once all of the elements have been determined, they can be placed within the container of choice, separated by linebreaks. The following ImageMap demonstrates valid syntax:

The code produces the following:

Resources
The following are helpful tools related to ImageMap:


 * For complex shapes and graphical editing, it is easiest to use the graphical ImageMap creator on the toolserver.
 * For easy conversion of the template format to ImageMap, it is easy for Mac users to use Nihiltres' conversion tool, which is written in AppleScript. With the advent of the #tag ParserFunction, which allows ImageMaps to receive template parameters and magic words, this isn't very important as  is no longer a usability problem, but the script may still be useful, if rough.
 * The template is a relatively easy way to use ImageMap in template form.
 * For reference information about the ImageMap extension to MediaWiki, see the extension's description page.
 * The Wikipedia article about image maps.