zen-cart网站,模版,采集,二次开发

首页 » ZenCart » ZenCart分析 » 阅读文章

ZenCart产品图片实现原理分析

2011-12-10 15:01 28946 0 发表评论
标签:


zencart产品图片信息是放在表products的products_image字段中的,图片存储在images下,其中大图放在images/large,中图放在images/medium下,图片命名的格式如原始小图地址是在images/abc/model001.jpg,对应的中图为images/medium/abc/model001_MED.jpg,大图为images/medium/abc/model001_LRG.jpg

主图分析

主图实现是在includes\modules\main_product_image.php中

$products_image_extension = substr($products_image, strrpos($products_image, '.'));

$products_image,数据库中产品图片的信息,在includes\modules\pages\product_info\main_template_vars.php中获取的
strrpos()函数获取字符串中指定字符最后一次出现的位置
substr()截取字符串函数,里面指定从什么位置截取到什么位置
结合起来$products_image_extension获取图片扩展名的。如上示例中$products_image_extension=".jpg"

$products_image_base = str_replace($products_image_extension, '', $products_image);

str_replace()字符串替换函数
这句即得到不含扩展名的部分,示例中$products_image_base="abc/model001"

$products_image_medium = $products_image_base . IMAGE_SUFFIX_MEDIUM . $products_image_extension;
$products_image_large = $products_image_base . IMAGE_SUFFIX_LARGE . $products_image_extension;

这2句就简单了,组合起来获得中图,大图的地址,其中IMAGE_SUFFIX_MEDIUM 和IMAGE_SUFFIX_LARGE中图和大图后缀可以在后台-->Configuration-->Imagess设置,默认是_MED和_LRG

if (!file_exists(DIR_WS_IMAGES . 'medium/' . $products_image_medium)) {
  $products_image_medium = DIR_WS_IMAGES . $products_image;
} else {
  $products_image_medium = DIR_WS_IMAGES . 'medium/' . $products_image_medium;
}

DIR_WS_IMAGES常量为images/
file_exists()判断文件是否存在
这句话也就很好理解了,判断中图是否存在,如果不存在就用原始小图。
同理

if (!file_exists(DIR_WS_IMAGES . 'large/' . $products_image_large)) {
  if (!file_exists(DIR_WS_IMAGES . 'medium/' . $products_image_medium)) {
    $products_image_large = DIR_WS_IMAGES . $products_image;
  } else {
    $products_image_large = DIR_WS_IMAGES . 'medium/' . $products_image_medium;
  }
} else {
  $products_image_large = DIR_WS_IMAGES . 'large/' . $products_image_large;
}

判断大图是否存在,不存在就判断中图,存在就是用中图,不存在就是用小图

细节图分析

细节图实现是在includes\modules\additional_images.php中

if ($products_image != '' && $flag_show_product_info_additional_images != 0)

$flag_show_product_info_additional_images参数在includes\modules\pages\product_info\main_template_vars.php中获取的,产品布局开关,在后台-->Catalog-->Product Types控制

  if (strrpos($products_image, '/')) {
    $products_image_match = substr($products_image, strrpos($products_image, '/')+1);
    //echo 'TEST 1: I match ' . $products_image_match . ' - ' . $file . ' -  base ' . $products_image_base . '<br>';
    $products_image_match = str_replace($products_image_extension, '', $products_image_match) . '_';
    $products_image_base = $products_image_match;
  }

判断是否有子文件夹存在,如果存在着逐步剥离,目的是使$products_image_base为得到细节图图片名主体部分(如上示例$products_image_base="model001_")

  $products_image_directory = str_replace($products_image, '', substr($products_image, strrpos($products_image, '/')));
  if ($products_image_directory != '') {
    $products_image_directory = DIR_WS_IMAGES . str_replace($products_image_directory, '', $products_image) . "/";
  } else {
    $products_image_directory = DIR_WS_IMAGES;
  }

这句是获取图片的路径,前面判断是图片是否在子文件夹中

  if ($dir = @dir($products_image_directory)) {
    while ($file = $dir->read()) {
      if (!is_dir($products_image_directory . $file)) {
        if (substr($file, strrpos($file, '.')) == $file_extension) {
          //          if(preg_match("/" . $products_image_match . "/i", $file) == '1') {
          if(preg_match("/" . $products_image_base . "/i", $file) == 1) {
            if ($file != $products_image) {
              if ($products_image_base . str_replace($products_image_base, '', $file) == $file) {
                //  echo 'I AM A MATCH ' . $file . '<br>';
                $images_array[] = $file;
              } else {
                //  echo 'I AM NOT A MATCH ' . $file . '<br>';
              }
            }
          }
        }
      }
    }
    if (sizeof($images_array)) {
      sort($images_array);
    }
    $dir->close();
  }

dir() 函数打开一个目录句柄,并返回一个对象。这个对象包含三个方法:read() , rewind() 以及 close()
这句话执行流程是:打开图片所在文件夹-->循环出文件夹里所有文件和文件夹-->筛选剔除不是文件的-->筛选剔除和原始图扩展名不同的-->正则匹配和细节图主体$products_image_base一致的-->剔除原始主图-->组合验证下是否完全匹配-->都通过后将文件名放入$images_array数组
sort()函数对数组重新排序

  if ($num_images < IMAGES_AUTO_ADDED || IMAGES_AUTO_ADDED == 0 ) {
    $col_width = floor(100/$num_images);
  } else {
    $col_width = floor(100/IMAGES_AUTO_ADDED);
  }

定义细节图横排的宽度

  for ($i=0, $n=$num_images; $i<$n; $i++)

依次循环出细节图

    $products_image_large = str_replace(DIR_WS_IMAGES, DIR_WS_IMAGES . 'large/', $products_image_directory) . str_replace($products_image_extension, '', $file) . IMAGE_SUFFIX_LARGE . $products_image_extension;
    $flag_has_large = file_exists($products_image_large);
    $products_image_large = ($flag_has_large ? $products_image_large : $products_image_directory . $file);
    $flag_display_large = (IMAGE_ADDITIONAL_DISPLAY_LINK_EVEN_WHEN_NO_LARGE == 'Yes' || $flag_has_large);
    $base_image = $products_image_directory . $file;
    $thumb_slashes = zen_image($base_image, addslashes($products_name), SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
    $thumb_regular = zen_image($base_image, $products_name, SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
    $large_link = zen_href_link(FILENAME_POPUP_IMAGE_ADDITIONAL, 'pID=' . $_GET['products_id'] . '&pic=' . $i . '&products_image_large_additional=' . $products_image_large);

依次为组合细节图大图地址,判断大图是否存在,不存在即用小图,组合小图地址,小图输出html格式,大图链接

    $list_box_contents[$row][$col] = array('params' => 'class="additionalImages centeredContent back"' . ' ' . 'style="width:' . $col_width . '%;"',
    'text' => "\n      " . $link);

组合$list_box_contents数组,然后利用includes\templates\template_default\common\tpl_columnar_display.php输出。

相关日志:

评论 共0条 (RSS 2.0) 发表评论

  1. 暂无评论,快抢沙发吧。

发表评论

  • 
  • 插入代码

联系我 Contact Me

回到页首