zencart zen_image函数介绍

zen cart zen_image函数是输出一个图片的HTML代码,其实这个函数就是封装了img标签的输出方式。第一个参数就是输出图片的地址,第二个参数就是alt属性,如果这个参数有值的话,也同时给这个标签的title设置同样的值。这个值就是鼠标经过图片的时候显示的文字。
第 三个和第四个参数是图片的宽度和高度。如果安装了image handler插件的话,该插件会根据这两个参数来对输出的图片进行压缩,压缩以最小值为基础。比如设置宽和高比是100×200,但是如果按照这个比例 来压缩的话,如果原始图片的宽和高的比例是1:1.5的话,结果显示的图片大小会是100×150,压缩采用的就是最小值100做为宽来自动按原始图片的 比例来压缩。这样就保证了图片不会变形。
最后一个参数就是img标签的其他属性,比如id,class,style

函数原型代码

function zen_image($src, $alt = ”, $width = ”, $height = ”, $parameters = ”) {
global $template_dir;
// soft clean the alt tag
$alt = zen_clean_html($alt);
// use old method on template images
if (strstr($src, ‘includes/templates’) or strstr($src, ‘includes/languages’) or PROPORTIONAL_IMAGES_STATUS == ’0′) {
return zen_image_OLD($src, $alt, $width, $height, $parameters);
}
//auto replace with defined missing image
if ($src == DIR_WS_IMAGES and PRODUCTS_IMAGE_NO_IMAGE_STATUS == ’1′) {
$src = DIR_WS_IMAGES . PRODUCTS_IMAGE_NO_IMAGE;
}
if ( (empty($src) || ($src == DIR_WS_IMAGES)) && (IMAGE_REQUIRED == ‘false’) ) {
return false;
}
// if not in current template switch to template_default
if (!file_exists($src)) {
$src = str_replace(DIR_WS_TEMPLATES . $template_dir, DIR_WS_TEMPLATES . ‘template_default’, $src);
}
// hook for handle_image() function such as Image Handler etc
if (function_exists(‘handle_image’)) {
$newimg = handle_image($src, $alt, $width, $height, $parameters);
list($src, $alt, $width, $height, $parameters) = $newimg;
}
// Convert width/height to int for proper validation.
// intval() used to support compatibility with plugins like image-handler
$width = empty($width) ? $width : intval($width);
$height = empty($height) ? $height : intval($height);
// alt is added to the img tag even if it is null to prevent browsers from outputting
// the image filename as default
$image = ‘<img src=”‘ . zen_output_string($src) . ‘” alt=”‘ . zen_output_string($alt) . ‘”‘;
if (zen_not_null($alt)) {
$image .= ‘ title=” ‘ . zen_output_string($alt) . ‘ “‘;
}
if ( ((CONFIG_CALCULATE_IMAGE_SIZE == ‘true’) && (empty($width) || empty($height))) ) {
if ($image_size = @getimagesize($src)) {
if (empty($width) && zen_not_null($height)) {
$ratio = $height / $image_size[1];
$width = $image_size[0] * $ratio;
} elseif (zen_not_null($width) && empty($height)) {
$ratio = $width / $image_size[0];
$height = $image_size[1] * $ratio;
} elseif (empty($width) && empty($height)) {
$width = $image_size[0];
$height = $image_size[1];
}
} elseif (IMAGE_REQUIRED == ‘false’) {
return false;
}
}
if (zen_not_null($width) && zen_not_null($height) and file_exists($src)) {
// $image .= ‘ width=”‘ . zen_output_string($width) . ‘” height=”‘ . zen_output_string($height) . ‘”‘;
// proportional images
$image_size = @getimagesize($src);
// fix division by zero error
$ratio = ($image_size[0] != 0 ? $width / $image_size[0] : 1);
if ($image_size[1]*$ratio > $height) {
$ratio = $height / $image_size[1];
$width = $image_size[0] * $ratio;
} else {
$height = $image_size[1] * $ratio;
}
// only use proportional image when image is larger than proportional size
if ($image_size[0] < $width and $image_size[1] < $height) {
$image .= ‘ width=”‘ . $image_size[0] . ‘” height=”‘ . intval($image_size[1]) . ‘”‘;
} else {
$image .= ‘ width=”‘ . round($width) . ‘” height=”‘ . round($height) . ‘”‘;
}
} else {
// override on missing image to allow for proportional and required/not required
if (IMAGE_REQUIRED == ‘false’) {
return false;
} else {
$image .= ‘ width=”‘ . intval(SMALL_IMAGE_WIDTH) . ‘” height=”‘ . intval(SMALL_IMAGE_HEIGHT) . ‘”‘;
}
}
// inject rollover class if one is defined. NOTE: This could end up with 2 “class” elements if $parameters contains “class” already.
if (defined(‘IMAGE_ROLLOVER_CLASS’) && IMAGE_ROLLOVER_CLASS != ”) {
$parameters .= (zen_not_null($parameters) ? ‘ ‘ : ”) . ‘class=”rollover”‘;
}
// add $parameters to the tag output
if (zen_not_null($parameters)) $image .= ‘ ‘ . $parameters;
$image .= ‘ />’;
return $image;
}

posted @ 2013-05-11 17:46  也许明天  阅读(394)  评论(0编辑  收藏  举报