Q: Magento resize image from Url with same name I have a function based on Tim Bezhashvyly post about resizing images from url in Magento. 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. 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.
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. 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;
  }
}


A: 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.
EDIT:
A good practice would be to generate your image names using the name of the entity, in this case the brand name.


*

*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. 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
If clearing the cache would be too much of hassle you can append the first 4 letters of the new file checksum:
<?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.
?>

