2

I have written a small shell script, run via the command line. The script uses a naming schema for our product images to set the images to the corresponding products.

However, sometimes the script seems to take ages for each product, up to 20 seconds. I was never sure what caused this, and have sometimes used a whole weekend just to assign images to all our products.

By accident I had the script display open when I cleared the caches because of some changes I made somewhere else. Strangely enough, that sped up the process immensely. Instead of up to 20 seconds, the script was doing 2 products a second.

Checking my discovery further, I can see how with time the products/second decline, and after a few hours getting as slow as I remember it being.

I am interested in the why of the slowdown, and why it speeds up when clearing the caches. I have not tried to identify which specific cache it is, I just select all caches and clear them (all 8 cache types in the list).

Attached is the script.

<?php
  umask(0);
  require_once 'app/Mage.php';
  Mage::app('admin');

  $mediaDir = 'media/import';
  $mediaDirDest = $mediaDir . DS . 'done';

  $allImages=array();

  echo "Reading and sorting files ...\n";
  foreach(glob($mediaDir . DS . '*.jpg') as $image) {
    if(strpos($image, '_')!==false) {
      $imageParts=explode('_', basename($image));
      $sku=$imageParts[0];
    } else {
      //Assume somebody fux0red up
      $imageParts=explode('.', basename($image));
      $sku=$imageParts[0];
    }

    if(!array_key_exists($sku, $allImages)) {
      $myArray=array();
      $myArray[]=basename($image);
      $allImages[$sku]=$myArray;
    } else {
      $myArray=$allImages[$sku];
      $myArray[]=basename($image);
      $allImages[$sku]=$myArray;
    }
  }

  echo "Now processing ...\n";

  foreach ($allImages as $sku => $images) {
    echo "Processing SKU $sku.\n";
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);

    //Remove link from image to product
    $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
    try {
      $items = $mediaApi->items($product->getId());
      foreach($items as $item) {
        $mediaApi->remove($product->getId(), $item['file']);
      }
    } catch (Exception $exception) {
      print_r($exception);
      die;
    }

    //Set no image as standard
    foreach($product->getStoreIds() as $storeId) {
      echo "Removing all images from SKU $sku in store ID $storeId.\n";
      $product->setStoreId($storeId)->setImage(false)->setSmallImage(false)->setThumbnail(false)->save();
    }

    usort($images, 'sortByLength');

    $product->setStoreId(0);

    //Now to do some serious adding
    foreach($images as $key => $image) {
      if($key===0) {
        echo "Setting image $image as primary image for SKU $sku.\n";
        $product->addImageToMediaGallery($mediaDir.DS.$image,array('image', 'small_image', 'thumbnail'),false,false);
      } else {
        echo "Setting image $image as additional image for SKU $sku.\n";
        $product->addImageToMediaGallery($mediaDir.DS.$image,null,false,false);
      }
    }

    try {
      echo "Saving SKU $sku after adding all images.\n";
      $product->save();
    } catch (Exception $e) {
      print_r($exception);
      die;
    }

    //Move images
    foreach ($images as $image) {
      $source=$mediaDir.DS.$image;
      $destination=$mediaDirDest.DS.$image;

      if(!rename($source,$destination)) {
        echo "Renaming of $source to $destination failed! Aborting.\n";
        die;
      }
    }
  }

  function sortByLength($a,$b) {
    if($a == $b) return 0;
    if(strlen($a)===strlen($b)) {
      return strcmp($a,$b);
    } else {
      return (strlen($a) < strlen($b) ? -1 : 1);
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.