MODx: img tag on steroids... :)
<?php
/*
img Snippet for MODx Revo
This snippet will either return a complete image tag
or return a comment including debugging information.
Author: info@pepebe.de
Version: 1.0
The main reason I created this snippets is because I wanted to prevent
phpthumbof from sending ugly error codes to the browser.
Automatically adding height and width to all image comes as a bonus.
Usage:
------
Snippet call:
[[img?
&input=`[[+image]]`
&options=`w=100&f=jpg&q=75`
&alt=`Alt`
&title=`Title`
&toPlaceholder=`img`
]]
Result:
-------
<img src="/assets/components/phpthumbof/cache/aad4c1a7ba318effbd93206d1c52200c.e06007eb554a08ff50d1b024f130183b.jpg"
height="74"
width="100"
alt="Alternative text"
title="Image title"
/>
Parameters:
-----------
&input=`` image path
&options=`` phpthumbof options (if empty, the snipppet will simply return the image src)
&alt=`` an alternative text
&title=`` a title tag for more info
&toPlaceholder=`` output to a placeholder
*/
/* Functions */
if(!function_exists('outputArray')) {
function outputArray($myArray) {
if( is_array($myArray) )
{
foreach( $myArray as $key => $value )
{
$items .= $key.": ".$value."\n";
}
return $items;
}
else
{
return;
}
}
}
/* Importent variables */
$scriptVariables['base_path'] = $modx->config['base_path'];
/* Let's go... */
if( file_exists( $scriptVariables['base_path'].$input ) )
{
if( !empty( $scriptProperties['options'] ) && 1 == 1 )
{
$image = $modx->runSnippet(
"phpthumbof" ,
array(
"input" => $scriptProperties['input'] ,
"options" => $scriptProperties['options']
)
);
}
else
{
$image = $scriptProperties['input'];
}
list($width, $height, $type, $attr) = getimagesize($modx->config['base_path'].$image);
$alt = !empty($alt) ? " alt='".$alt."'" : "";
$title = !empty($title) ? " title='".$title."'" : "";
$img = "<img src='".$image."' ".$attr.$alt.$title." />";
if( !empty($toPlaceholder) )
{
$modx->setPlaceholder($toPlaceholder,$img);
return;
}
else
{
return $img;
}
}
else
{
$msg = "";
// There is no general way how browsers handle broken images.
// As a fix we don't display the image as a whole and display
// the alternative Text instead
$msg .= "<div class='brokenImage'>".$alt."</div>"."\n";
$msg .= "<!-- "."\n";
$msg .= "Error: ".$input." does not exist"."\n";
$msg .= "----------------------------------"."\n";
$msg .= "scriptProperties"."\n";
$msg .= "----------------------------------"."\n";
$msg .= outputArray($scriptProperties);
$msg .= "----------------------------------"."\n";
$msg .= "scriptVariables"."\n";
$msg .= "----------------------------------"."\n";
$msg .= outputArray($scriptVariables);
$msg .= " -->"."\n";
if( !empty($toPlaceholder) )
{
$modx->setPlaceholder($toPlaceholder,$msg);
return;
}
else
{
return $msg;
}
}