I have my product images optimized very well, but magento cached images are very poor and larger (already cleared cache, it's what magento is doing to the optimized images which is making them larger). I have tried Kraken.io's plugin but it's so resource hungry and slows down page load for some reason on the front end more than it's worth because it is auto-optimizing images. Is there an alternative extension out there that will let me just optimize the cached images once in a while? I'd prefer Kraken's API as I already have a (paid) account with them, but I don't want anything automated, I'd rather do that myself when the website is not busy to reduce server load. Alternatively, would there be any issues with just downloading the cached directory now and again, optimizing it and re-uploading it to the server?
Magento 1.9 Cache Image Optimization – Kraken Extension Guide
cacheimagemagento-1.9
Related Solutions
It admin>system>configuration>web>
Secure tab and check Base Media URL
contain https protocol
.
That mean url should be start with https
for media
Please make sure the following things before running the script
- This script location is at root-folder/script
- System -> Configuration -> Catalog -> Catalog -> Frontend -> Use Flat Catalog Product should be No. If it is Yes then set it No first then execute the script and again you can set it Yes
- After execute the script reindex at System -> Index Management -> Product Flat Data
.
require_once('../app/Mage.php');
Mage::app();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*'); // select all attributes
$newPaths = array();
$normalizeChars = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',
'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T',
' '=>'-'
);
foreach ($collection as $productCollection) {
$product = mage::getModel('catalog/product')->load($productCollection->getId());
$images = $product->getMediaGallery('images');
foreach ($images as $image) {
$name = trim(strtolower($product->getName()));
$formattedName = preg_replace("/[^A-Za-z0-9\_\-\.]/", '', strtr($name, $normalizeChars));
$newPaths[$product->getSku()]['old'] = $image['file'];
$newPaths[$product->getSku()]['name'] = $name;
$newPaths[$product->getSku()]['formated'] = $formattedName;
$newPaths[$product->getSku()]['new'] = '/'.$formattedName[0].'/'.$formattedName[1].'/'.$newPaths[$product->getSku()]['formated'].'-'.$product->getSku().'.'.pathinfo($image['file'], PATHINFO_EXTENSION);
}
}
try {
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
foreach ($newPaths as $newPath) {
copy('../media/catalog/product' . $newPath['old'], '../media/catalog/product' . $newPath['new']);
$query = 'UPDATE catalog_product_entity_media_gallery SET value = "' . $newPath['new'] . '" WHERE value ="' . $newPath['old'].'"';
$write->query($query);
$query = 'UPDATE catalog_product_entity_varchar SET value = "' . $newPath['new'] . '" WHERE value ="' . $newPath['old'].'"';
$write->query($query);
}
}catch (Exception $e){
var_dump($e);
}
var_dump($newPaths);
Best Answer
you can try this solution
1. Try TinyPNG's Extension for magento
The plugin compress images well enough (Try there compression engine https://tinypng.com/), When you view a product in your webshop Magento creates different image sizes in its cache folders. This extension will compress these images for you automatically. since it work while creating cache it can slow the page if there are lot of products on the page, this happen only when cache is create otherwise the website will work as normal.
2. You can set Image quality while creating cache.
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(195, 280); ?>" />
to
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(195, 280)->setQuality(70); ?>" />
we have set the quality
setQuality(70)
of the cache image that is generating lower the quality , lower the file size. just set the quality that best suits you.