Enhanced Image Action


This is the development page for the enhanced image action.
 



The code here below is just a quick modification to the offical image action code. Only works from release 1.1.6.0!

<?php
/**
 * Displays an image.
 *
 * You have to specify the url for the location of the image. In contrary to inline images you can set the
 * apperance of the image better:
 *
 * You can specify "width" and "height" for the image (note that the image is just displayed different, it does
 * not save any bandwidth), set a target link or make the full size image open in a new window (only possible
 * if you don't assign a link to the image).
 *
 * Syntax:
 * {{image url ="http://www.example.com/example.jpg" alt="describing text" [link="http://www.example.com] [heigth=""] [width=""] [class="class"]}}
 *
 * @package Actions
 * @subpackage  ?
 * @name    image
 *
 * @author  ?
 * @author  {@link http://wikka.jsnx.com/ChristianBarthelemy ChristianBarthelemy} (heigth+width+openfull)
 * @author  {@link http://wikka.jsnx.com/NilsLindenberg NilsLindenberg} (default values, minor corrections)
 *
 * @version 0.9
 * @copyright   Copyright © ?
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @since   ?
 *
 * @input   string  $url  mandatory: url of the image to be displayed;
 * @input   string  $alt  mandatory: alternative text for the image;
 * @input   string  $link  optional: target link for image (optional). Supports URL, WikiName links,
 *              InterWiki links etc.;
 *              default: none;
 * @input   integer  $width  optional: alternative width for the image;
 *              default:  
 * @input   integer  $height  optional: alternative heigth for the image;
 *              default:
 * @input   string  $class  optional: a class for the image;
 *              default: none;
 *
 * @output  the given image;
 *
 * @uses        cleanUrl();
 * @todo        - do we need to pass the output through SafeHtml?
 *      - handling when no url and/or alternative text is/are provided
 *      - documentation for src (also possible instead of url)
 *      - option to open "full-image" in another window?
 */


// function to check if a string is a valid Length as specified in the XHTML DTDs
if (!function_exists('is_length')) {
    function is_length($input) {
        return preg_match("/^[0-9]+\%?$/",$input) ? TRUE : FALSE;
    }
}

//setting default values
$link = $class = $output =  $title =  $width = $height = NULL;

//getting the paramters
if (is_array($vars))
{
    foreach ($vars as $param => $value)
    {
        if ($param == 'src' and $vars['url'] == '') {$vars['url']=$value;}
        if ($param == 'title') {$title=$this->htmlspecialchars_ent($vars['title']);}
        if ($param == 'class') {$class=$this->htmlspecialchars_ent($vars['class']);}
        if ($param == 'alt') {$alt=$this->htmlspecialchars_ent($vars['alt']);}
        if ($param == 'width') {$width=$this->htmlspecialchars_ent($vars['width']);}
        if ($param == 'height') {$height=$this->htmlspecialchars_ent($vars['height']);}
        if ($param == 'link') {$link=$this->cleanUrl(trim($vars['link']));}
    }
}

//sanatizing url and proofing if it is an image
$url = $this->cleanUrl(trim($vars['url']));
$image = getimagesize($url);

//if it is an image, prepare the output
if ((is_array($image)) && ($alt))
{
    if (!is_length($width) || !is_length($height)) {
            $width = $image[0];
            $height = $image[1];
    }
    $output.="<img ";
    if ($class) {$output.="class=\"".$class."\" ";}
    $output.="src=\"".$url."\" ";
    if ($alt) {$output.="alt=\"".$alt."\" ";}
    if ($title && !$link) {$output.="title=\"".$title."\" ";}
    $output.="width=\"".$width."\" height=\"".$height."\" />";

    //link?
    if ($link && !$title) {$output = $this->Link($link, "", $output, 1, 0, 0);}
                elseif ($link && $title) {$output = $this->Link($link, "", $output, 1, 0, $title);}

    //make the output save and print it
    $output = $this->ReturnSafeHTML($output);
    print($output);
}
elseif (!$alt) {echo 'The necessary parameter alt was not provided';}
else {echo 'The image could not be found or it was no image. Please check the url';}
?>


Note that we have to change the function Link in the wikka.php, too:

changing:
 return $url ? "<a class=\"ext\" href=\"$url\" >$text</a>$external_link_tail" : $text;

to:
return $url ? "<a class=\"ext\" href=\"$url\" title=\"$title\">$text</a>$external_link_tail" : $text;



CategoryDevelopmentActions
Comments
Comment by JavaWoman
2005-03-21 18:08:14
Better, but no cigar ;-)
1) for required parameters, do *not* initialize to an empty string since an empty string may be a *valid* value; required are url/src and alt
2) for any optional parameter that legally may be an empty string do not initialize to an empty string either (this applies to title for instance)
3) "initialize" such parameters to NULL so their presence as a parameter can then be checked
4) since 0 may be a valid size, do not initialize width & height to zero, but also to NULL
5) the default of "WikiImage" for title is meaningless - a title attribute should be generated only if a title attribute was actually provided
6) 'heigth' should be 'height' (consistent spelling error)
7) provided width and height parameters should be validated - they can be integers or percentages; any non-valid value should be rejected; if any value is rejected the companion size parameter should not be used either
8) once valid parameters have been retrieved, do not test as if they are booleans [if (!width)] but test if they have been given a value [if (isset($width))]
9) if a link is specified *and* a title attribute, it's probably better to apply the title attribute to the anchor tag instead of the img tag

Sorry... but the img tag isn't the easiest to generate - especially not if based on user input. Same old mantra: DO NOT TRUST USER INPUT - EVER. ;-)
Comment by TimoK
2005-03-30 16:08:25
9 should work like this:
1. Replace the "if($title)" with "if($title && !$link)"
2. Replace the "if($link)" with "if($link && !$title)"
3. After the above add "elseif ($link && $title) {$output = $this->Link($link, "", $output, 1, 0, $title);}"
(I don't edit the page because I didn't test this.)
Comment by NilsLindenberg
2005-04-01 15:45:08
> 9 should work like this:
I did not work...

... because the link function is missing title=\"$title\" in the last line. After adding that, it works. Thank you, Timo.
Comment by NilsLindenberg
2005-04-01 15:48:27
7) still missing
Comment by JavaWoman
2005-04-05 10:00:26
The is_length() function is nice, but embedding it in the action file (liek this) will lead to the old, old problem of not being able to use the action twice on a page - *including* on page history or version comparisons.

The function should be in a separate file, appropriately placed (See WikkaCodeStructure !) and made available with require_once().
Comment by TimoK
2005-04-05 10:50:22
Doh, you are right (as usual). Quick fix put in place already, will make a proper fix later (thursday).
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki