Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {html_image} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     html_image
Chris PeBenito 696b41
 * Date:     Feb 24, 2003
Chris PeBenito 696b41
 * Purpose:  format HTML tags for the image
Chris PeBenito 696b41
 * Input:
Chris PeBenito 696b41
 *         - file = file (and path) of image (required)
Chris PeBenito 696b41
 *         - height = image height (optional, default actual height)
Chris PeBenito 696b41
 *         - width = image width (optional, default actual width)
Chris PeBenito 696b41
 *         - basedir = base directory for absolute paths, default
Chris PeBenito 696b41
 *                     is environment variable DOCUMENT_ROOT
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Examples: {html_image file="images/masthead.gif"}
Chris PeBenito 696b41
 * Output:   
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
Chris PeBenito 696b41
 *      (Smarty online manual)
Chris PeBenito 696b41
 * @author   Monte Ohrt <monte at ohrt dot com>
Chris PeBenito 696b41
 * @author credits to Duda <duda@big.hu> - wrote first image function
Chris PeBenito 696b41
 *           in repository, helped with lots of functionality
Chris PeBenito 696b41
 * @version  1.0
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 * @uses smarty_function_escape_special_chars()
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_html_image($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    $alt = '';
Chris PeBenito 696b41
    $file = '';
Chris PeBenito 696b41
    $height = '';
Chris PeBenito 696b41
    $width = '';
Chris PeBenito 696b41
    $extra = '';
Chris PeBenito 696b41
    $prefix = '';
Chris PeBenito 696b41
    $suffix = '';
Chris PeBenito 696b41
    $server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
Chris PeBenito 696b41
    $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
Chris PeBenito 696b41
    foreach($params as $_key => $_val) {
Chris PeBenito 696b41
        switch($_key) {
Chris PeBenito 696b41
            case 'file':
Chris PeBenito 696b41
            case 'height':
Chris PeBenito 696b41
            case 'width':
Chris PeBenito 696b41
            case 'dpi':
Chris PeBenito 696b41
            case 'basedir':
Chris PeBenito 696b41
                $$_key = $_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'alt':
Chris PeBenito 696b41
                if(!is_array($_val)) {
Chris PeBenito 696b41
                    $$_key = smarty_function_escape_special_chars($_val);
Chris PeBenito 696b41
                } else {
Chris PeBenito 696b41
                    $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'link':
Chris PeBenito 696b41
            case 'href':
Chris PeBenito 696b41
                $prefix = '';
Chris PeBenito 696b41
                $suffix = '';
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            default:
Chris PeBenito 696b41
                if(!is_array($_val)) {
Chris PeBenito 696b41
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
Chris PeBenito 696b41
                } else {
Chris PeBenito 696b41
                    $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (empty($file)) {
Chris PeBenito 696b41
        $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
Chris PeBenito 696b41
        return;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (substr($file,0,1) == '/') {
Chris PeBenito 696b41
        $_image_path = $basedir . $file;
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        $_image_path = $file;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if(!isset($params['width']) || !isset($params['height'])) {
Chris PeBenito 696b41
        if ($smarty->security &&
Chris PeBenito 696b41
            ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
Chris PeBenito 696b41
            (require_once(SMARTY_CORE_DIR . 'core.is_secure.php')) &&
Chris PeBenito 696b41
            (!smarty_core_is_secure($_params, $smarty)) ) {
Chris PeBenito 696b41
            $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);
Chris PeBenito 696b41
Chris PeBenito 696b41
        } elseif (!$_image_data = @getimagesize($_image_path)) {
Chris PeBenito 696b41
            if(!file_exists($_image_path)) {
Chris PeBenito 696b41
                $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
Chris PeBenito 696b41
                return;
Chris PeBenito 696b41
            } else if(!is_readable($_image_path)) {
Chris PeBenito 696b41
                $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
Chris PeBenito 696b41
                return;
Chris PeBenito 696b41
            } else {
Chris PeBenito 696b41
                $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
Chris PeBenito 696b41
                return;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        if(!isset($params['width'])) {
Chris PeBenito 696b41
            $width = $_image_data[0];
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        if(!isset($params['height'])) {
Chris PeBenito 696b41
            $height = $_image_data[1];
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if(isset($params['dpi'])) {
Chris PeBenito 696b41
        if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
Chris PeBenito 696b41
            $dpi_default = 72;
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $dpi_default = 96;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $_resize = $dpi_default/$params['dpi'];
Chris PeBenito 696b41
        $width = round($width * $_resize);
Chris PeBenito 696b41
        $height = round($height * $_resize);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    return $prefix . ''.$alt.'' . $suffix;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>