Im looking for a complete explanation on how Magento 2 processes and compresses product images.
- How does Magento 2 compress JPG & PNG product images?
- What compression methods does it use?
- Which image processing adapter setting is better, ImageMagick or PHP GD2?
- Which specific files in the magento core are responsible for setting the compression levels for product images.
8 Bit PNG Images
If 8bit PNG images are uploaded they become very pixelated by Magento, with both the PHP GD2 or ImageMagick image processor configured.
I have found that in vendor/magento/module-catalog/Model/Product/Image.php
the jpeg image quality is set to 80 by default.
protected $_quality = 80;
And in vendor/magento/framework/Image/Adapter/Gd2.php
the quality is set to 9.
switch ($this->_fileType) {
case IMAGETYPE_PNG:
$quality = 9; // For PNG files compression level must be from 0 (no compression) to 9.
break;
Although when I change the png compression level to 0 from 9 (for testing purposes only) I see no change, even after clearing cache, flushing the catalog images and removing cached and generated directories. With xDebug on I can confirm this line is being called, but it has no noticeable effect.
Following setting the quality, it's added to the $functionParameters
which also holds the image processor adapter type, and image destination path. Can other parameters be added.
$functionParameters = [$this->_imageHandler, $fileName];
if ($quality) {
$functionParameters[] = $quality;
}