Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

{html_image} with transparent PNG hack for IE5.x+

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
xces
Smarty Regular


Joined: 09 Apr 2004
Posts: 77

PostPosted: Fri Apr 09, 2004 10:28 am    Post subject: {html_image} with transparent PNG hack for IE5.x+ Reply with quote

Just recoded the htmlimage plugin to allow transparent PNG's in IE5.x+. Original idea after http://www.koivi.com/ie-png-transparency/.

Usage:
Just like the normal one. smarty will see if you are trying to "print" a PNG file and act accordingly.

Notes:
p.s. add a 1x1 transparent spacer.gif somewhere in your site and point the html_image plugin to it.

[php:1:7f4714924f]<?php
function smarty_function_html_image($params, &$smarty)
{
require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');

$alt = '';
$file = '';
$border = 0;
$height = '';
$width = '';
$extra = '';
$prefix = '';
$suffix = '';
$style = '';
$server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
$basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
foreach($params as $_key => $_val) {
switch($_key) {
case 'file':
case 'border':
case 'width':
$$_key = $_val;
break;

case 'height':
$$_key = $_val;
break;

case 'dpi':
case 'basedir':
$$_key = $_val;
break;

case 'alt':
if(!is_array($_val)) {
$$_key = smarty_function_escape_special_chars($_val);
} else {
$smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
}
break;

case 'link':
case 'href':
$prefix = '<a href="' . $_val . '">';
$suffix = '</a>';
break;

case 'style':
$$_key = $_val;
break;

default:
if(!is_array($_val)) {
$extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
} else {
$smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
}
break;
}
}

if (empty($file)) {
$smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
return;
}

if (substr($file,0,1) == '/') {
$_image_path = $basedir . $file;
} else {
$_image_path = $file;
}

if(!isset($params['width']) || !isset($params['height'])) {
if ($smarty->security &&
($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
(require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.is_secure.php')) &&
(!smarty_core_is_secure($_params, $smarty)) ) {
$smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);

} elseif (!$_image_data = @getimagesize($_image_path)) {
if(!file_exists($_image_path)) {
$smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
return;
} else if(!is_readable($_image_path)) {
$smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
return;
} else {
$smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
return;
}
}

if(!isset($params['width'])) {
$width = $_image_data[0];
}
if(!isset($params['height'])) {
$height = $_image_data[1];
}

}

if (isset($params['dpi'])) {
if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
$dpi_default = 72;
} else {
$dpi_default = 96;
}
$_resize = $dpi_default/$params['dpi'];
$width = round($width * $_resize);
$height = round($height * $_resize);
}

//====================================================================================================
// Transparent PNG hack for win IE5+
// After idea on: http://www.koivi.com/ie-png-transparency/
//====================================================================================================
if (preg_match('/.png$/i', $file) &&
isset($_SERVER['HTTP_USER_AGENT']) &&
preg_match('/msie\s([5-9])\.?[0-9]*.*(win)/i', $_SERVER['HTTP_USER_AGENT']) &&
!preg_match('/opera/i',$_SERVER['HTTP_USER_AGENT'])) {

//----------------------------------------------------------------------------------------------------
// make sure that we are only replacing for the Windows versions of Internet Explorer 5+
//----------------------------------------------------------------------------------------------------
$style.= ' width: '.$width.'px; height: '.$height.'px; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''.$file.'\', sizingMethod=\'scale\');';
$file = '/inc/img/spacer.gif';
}
if ($style != '') { $style = ' style="'.$style.'"'; }

//====================================================================================================
// Ready
//====================================================================================================
return $prefix . '<img src="'.$file.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$extra.$style.' />' . $suffix;
}
?>[/php:1:7f4714924f]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP