简体   繁体   English

Magento从具有相同名称的网址调整图片大小

[英]Magento resize image from Url with same name

I have a function based on Tim Bezhashvyly post about resizing images from url in Magento. 我有一个基于Tim Bezhashvyly发表的关于在Magento中调整url图像大小的函数。 My problem is with the cache and when someone change an image with the same name. 我的问题是缓存和有人更改相同名称的图像时。

Example : I have a vendor logo and the name is : mylogo.png. 示例:我有一个供应商徽标,名称是:mylogo.png。 If I want to change this logo with a different logo but the same name : mylogo.png the front-end version of the logo won't change because the function check if there's a cache version of mylogo.png and there's one so it display the old one. 如果要使用其他徽标但名称相同的徽标来更改此徽标:mylogo.png,则徽标的前端版本不会更改,因为该功能会检查是否存在mylogo.png的缓存版本,并且该版本会显示旧的。

Uploaded logo won't be done by me but by the vendor and I can't prevent him from naming all he's logo version logo.png. 上载的徽标不会由我而是由供应商来完成,并且我不能阻止他将他的徽标全部命名为logo.png。 I would need a smart way to change the cache system. 我需要一种聪明的方法来更改缓存系统。 This function will be called each time a front-end page is loaded that's why I need to cache images I don't want resized evreytime the page is loaded. 每次加载前端页面时都会调用此函数,这就是为什么我需要缓存不需要在页面加载时调整大小的图像的原因。

public function resizeImage($path='',$width=100,$height=null,$constrainOnly=true,$keepAspectRatio=true,$keepFrame=true,$keepTransparency=true){
  $Tpath = explode('/',$path);
  $end = end($Tpath); 
  $path = '';
  for ($i=0; $i < count($Tpath)-1 ; $i++) { 
        if($i!=count($Tpath)){
            $path .= $Tpath[$i].'/';
        }else{

        }
  }

  if($end!='')$_file_name = $end;
  else $_file_name = '404.png';

  $_media_dir = Mage::getBaseDir('media') . DS . $path;
  $cache_path = $path . 'cache' . DS . $width.'X'.$height . DS;
  $cache_dir = $_media_dir . 'cache' . DS . $width.'X'.$height . DS;

  if (file_exists($cache_dir . $_file_name)) {
      return Mage::getBaseUrl('media') . $cache_path . $_file_name;
  } elseif (file_exists($_media_dir . $_file_name)) {
      if (!is_dir($cache_dir)) {
          mkdir($cache_dir);
      }

      $_image = new Varien_Image($_media_dir . $_file_name);
      $_image->constrainOnly($constrainOnly);
      $_image->keepAspectRatio($keepAspectRatio);
      $_image->keepFrame($keepFrame);
      $_image->keepTransparency($keepTransparency);
      $_image->resize($width, $height);
      $_image->save($cache_dir . $_file_name);

      return Mage::getBaseUrl('media') . $cache_path . $_file_name;
  }
}

You can try a different approach: clear the cache for that specific image when a new one is being uploaded. 您可以尝试另一种方法:上传新图像时,清除该特定图像的缓存。 So, the next time your front-end helper will try to output the image, a new cached one will be generated. 因此,下一次您的前端助手将尝试输出图像时,将生成一个新的缓存图像。

Note: if you use the same URL for a cached image even if a new one was uploaded, consider handling browser caching also, otherwise, generate the name of the cached image based on the checksum of the original one. 注意:即使上传了新的图像,如果对缓存的图像使用相同的URL,请考虑也处理浏览器缓存,否则,请基于原始图像的校验和生成缓存的图像的名称。

EDIT: 编辑:

A good practice would be to generate your image names using the name of the entity, in this case the brand name. 良好的做法是使用实​​体名称(在这种情况下为品牌名称)生成图像名称。

  1. Name Your Images Descriptively and in Plain English 用简单的英语描述性地命名图像

When it comes to SEO, it's important to use acceptable keywords to help your webpage rank on search engines. 对于SEO,使用可接受的关键字来帮助您的网页在搜索引擎上排名很重要。 Creating descriptive, keyword-rich file names is absolutely crucial for image optimization. 创建描述性,关键字丰富的文件名对于图像优化绝对至关重要。 Search engines not only crawl the text on your webpage, but they also search for keywords within your image file names. 搜索引擎不仅会抓取您网页上的文本,而且还会搜索图像文件名中的关键字。

from: shopfiy 来自: shopfiy

If clearing the cache would be too much of hassle you can append the first 4 letters of the new file checksum: 如果清除缓存非常麻烦,则可以在新文件校验和的前4个字母后附加:

<?php 

    $fileName = sprintf("%s-%s.%s",
        convert_text_to_friendly_url_function($brandName),
        substr(md5_file($newUploadedFile), 0, 4).
        $fileExtension
    );

    // now you have a new fileName, do whatever you need with it.
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM