We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1764
    • 680 Posts
    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;
    
    }
    
      • 33337
      • 3,975 Posts
      Thats Cool ! smiley

      Thanks for sharing,

      regards,

      zi
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
      • Adam, requires GD library?
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 1764
          • 680 Posts
          Quote from: rthrash at Sep 15, 2005, 12:43 PM

          Adam, requires GD library?

          Nope. The only image function it uses is getimagesize which does not require GD.
            • 2762
            • 1,198 Posts
            nice snippet!
            should be a good idea integrate it with NewsListing to do something like thumbnails for news..
              Free MODx Graphic resources and Templates www.tattoocms.it
              -----------------------------------------------------

              MODx IT  www.modx.it
              -----------------------------------------------------

              bubuna.com - Web & Multimedia Design