1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Brend Wanders <b.wanders@utwente.nl>
5 */
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die('Meh.');
8
9/**
10 * The image type.
11 */
12class plugin_strata_type_image extends plugin_strata_type {
13    function isExternalMedia($value) {
14        return preg_match('#^(https?|ftp)#i', $value);
15    }
16
17    function normalize($value, $hint) {
18        global $ID;
19
20        // strip leading {{ and closing }}
21        $value = preg_replace(array('/^\{\{/','/\}\}$/u'), '', $value);
22
23        // drop any title and alignment spacing whitespace
24        $value = explode('|', $value, 2);
25        $value = trim($value[0]);
26
27        if($this->isExternalMedia($value)) {
28            // external image
29            // we don't do anything else here
30        } else {
31            // internal image
32
33            // discard size string and other options
34            $pos = strrpos($value, '?');
35            if($pos !== false ) {
36                $value = substr($value, 0, $pos);
37            }
38
39            // resolve page id with respect to selected base
40            resolve_mediaid(getNS($ID),$value,$exists);
41        }
42
43        return $value;
44    }
45
46    function render($mode, &$R, &$T, $value, $hint) {
47        if(preg_match('/([0-9]+)(?:x([0-9]+))?/',$hint,$captures)) {
48            if(!empty($captures[1])) $width = (int)$captures[1];
49            if(!empty($captures[2])) $height = (int)$captures[2];
50        }
51
52        if($this->isExternalMedia($value)) {
53            // render external media
54            $R->externalmedia($value,null,null,$width,$height);
55        } else {
56            // render internal media
57            // (':' is prepended to make sure we use an absolute pagename,
58            // internalmedia resolves media ids, but the name is already resolved.)
59            $R->internalmedia(':'.$value,null,null,$width,$height);
60        }
61    }
62
63    function getInfo() {
64        return array(
65            'desc'=>'Displays an image. The optional hint is treated as the size to scale the image to. Give the hint in WIDTHxHEIGHT format.',
66            'hint'=>'size to scale the image to'
67        );
68    }
69}
70