Showing posts with label Image compressor function magneto. Show all posts
Showing posts with label Image compressor function magneto. Show all posts

Thursday 16 June 2016

How to compress external images in magento


How to compress external images in magento

I have shown here image compressor code for who uses the custom module like Banner Module. You can also put this code in magento core functionality as well.

Put below function in your module controller where the image save code available

public function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);

elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);

elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);

imagejpeg($image, $destination, $quality);

return $destination;
}

Now call the above function after the image save code like this :

$this->compress($path.$_FILES['filename']['name'], $path.$_FILES['filename']['name'], 85);   // you can change the compressor ratio 85% to 90%