This is a very simple snippet but I thougt it could come in handy for someone. Basically it just displays an image tag but it dynamically get’s the height, with and alt attributes from the image file. It also won’t show the image tag unless the file exists.
All you have to do is pass it the name of the TV with the image URL.
[[Image?src_tv=ImagePathTVName]]
/*
* Written by: Adam Crownoble
* Contact: [email protected]
* Created: 9/14/2005
* For: MODx cms (modxcms.com)
* Name: Image
* Description: Renders an image tag with automatic height, width and alt
* Properties: &src_tv=Source Template Variable Name;string; &id=HTML ID Attribute;string; &class=HTML Class Attribute;string;
*/
/* License
Image - Renders an image tag with automatic height, width and alt
Copyright (C) 2005 Adam Crownoble
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if($src_tv) {
$base = $_SERVER['DOCUMENT_ROOT'];
$tv = $modx->getTemplateVar($src_tv);
$src = $tv['value'];
$url = parse_url($src);
$path = $url['path'];
$file = $base.'/'.$path;
$alt = basename($file);
$alt = ereg_replace('_','',$alt);
$alt = ereg_replace('^(.*)\.[a-zA-Z]*$','\\1',$alt);
$alt = ucfirst($alt);
if(file_exists($file) && !is_dir($file)) {
list($w, $h) = getimagesize($file);
$html = <<<EOD
<img id="{$id}" class="{$class}" src="{$src}" alt="{$alt}" style="width:{$w}; height:{$h};" />
EOD;
}
return $html;
}