Search code examples
phpapachebashmagentoimage-processing

Automate Magento Image Optimization


Is it possible to set up an observer whenever a product image is added, run a shell script to optimize the image or something like that? Maybe it can extend further beyond just product images. Detect new image on the server and run jpegoptim or pngoptim only on the newly added images?


Solution

  • In magento, because catalog images are created dynamically from the image uploaded in admin on first page view (catalog list, product detail, etc) using

    $this->helper('catalog/image')->init($_product)->resize(163, 100);

    it is NOT possible to use any default product observer (like Magento Add New product event observer) to optimizing the images, plus you can always delete all product image using admin (System -> Cache Management).

    One possible solution (assuming that you have right access on your server)

    1. Create a bash script that has a cron job that run ever x interval
    2. Check the media folder for all newly created files since last cron job (now-x)
    3. Then optimize them using jpegoptim