Summary

Thought it might be interesting to have the possibility to check whether the image file exists when using JHTML::image( ... ) ( or JHTML::_( 'image', ... ) )

If the requested image is local and file does not exist (and the optional parameter $checkExist is set to true), the function would return false.

Can be useful when image is supposed to have been uploaded by a user.

The code for that:

/**
* Write a <img></img> element
*
* @access public
* @param string The relative or absolute URL to use for the src attribute
* @param string The target attribute to use
* @param array An associative array of attributes to add
* @param boolean check whether image file exists
* @since 1.5
*
* @return string the HTML image tag
* @return boolean false if image file does not exist
*/
function image($url, $alt, $attribs = null, $checkExist = false)
{
if (is_array($attribs)) {
$attribs = JArrayHelper::toString( $attribs );
}
if(strpos($url, 'http') !== 0) {
if ( $checkExist ) {
jimport( 'joomla.filesystem.file' );
$file = JPATH_ROOT . DS . str_replace( '/', DS, $url ) ;
if ( !JFile::exists( $file ) ) {
return false;
}
}
$url = JURI::root(true).'/'.$url;
};

return '<img src="'.$url.'" alt="'.$alt.'" '.$attribs.' />';
}
Opened On:
8 Jan 2008, 20:04 by Mathieu Chauvinc
Status:
Open

Filed Under

  • All

Responses

Posted on 27 Mar 2009, 7:32 by Marcel Würsch
Hello
The Image thing are good idea, only the Title Tag for Firefox is missed.
Posted on 4 Feb 2010, 3:18 by Hannes Papenberg
The function has been refactored. The functionality is slightly different. Look into 1.6. Closing this report.