As we have found our website is using PNG images and has page sizes ranging from 1 MB up to 2 MB sometimes, I did a comparison using small images.. and have found PNG size is 36 KB, while JPEGs with the same quality/size are just 6-7 KB. Since we have a lot products, is there an way to do such changes in bulk/automatically using a script or plugin?

  • yes, currently into png and we want to go with jpeg. Apr 29, 2013 at 21:25

2 Answers 2


Magento handles resize of your images from and back to the format they were uploaded in. You can re-format your images in PHP using the GD library with the following function:

Reformat with PHP:

// Quality is a number between 0 (best compression) and 100 (best quality)
function png2jpg($originalFile, $outputFile, $quality) {
    $image = imagecreatefrompng($originalFile);
    imagejpeg($image, $outputFile, $quality);

This function uses the imagecreatefrompng() and the imagejpeg() functions from the GD library.

Source: https://stackoverflow.com/questions/1201798/use-php-to-convert-png-to-jpg-with-compression

Reformat to JPG on disk via shell command:

You can do all on-disk at once, but it will require you to use something like Imagemagick

convert image.png image.jpg

The problem here is that you can't just go about changing the files and their extensions on your Magento install willy-nilly. You'll have to notify Magento of the change either by editing the entry directly in the db (don't do this) or re-uploading all of them.

Source: https://superuser.com/questions/71028/batch-converting-png-to-jpg-in-linux


Alternatively, use a webservice like Smush.it to optimize your PNGs for you. The Magento Wiki has a little command-line hack at the bottom of this image optimization post to give you some direction about getting a list of all of your images and you can paste that list directly into Smush.it.

I have a feeling that at 36kb apiece, your PNGs are not optimized, or you're not using Magento's built-in resize in your theme. You should be doing both.


Updating within Magento

So, you've done the above. Now you have to update Magento. Assuming that all image names remained the same, with the only difference being the suffix (file extension) being changed from ".png" to ".jpg" you can do the following in a standalone script, executed from the command line:



$collection = Mage::getModel('catalog/product')
    ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$backendModel = $collection->getResource()->getAttribute('media_gallery')->getBackend();

foreach ($collection as $product){
    $backendModel->afterLoad($product); // Load the media gallery images for the product
    foreach($product->getMediaGalleryImages() as $image){
        $image->setPath(str_ireplace('.png', '.jpg', $image->getPath()));
        $image->setUrl(str_ireplace('.png', '.jpg', $image->getUrl()));
        $image->setFile(str_ireplace('.png', '.jpg', $image->getFile()));


Execute with the following:

php update-image-names.php

I advise strongly not to do this on your live production server - do it offline in a maintenance window or on a staging box that you can migrate or promote the database changes into production.

There's a high chance that you could run out of PHP memory while running this if you have a large catalog - you may want to call an ini_set at the top to increase your memory_limit to something more substantial than the recommended 256MB.

  • I am not 100% sure, but I have the feeling OP asks how to update actual DB values of assigned images. Because converting PNGs to JPGs with reasonable quality is task that a Photoshop script will do better than PHP GD, but placing them in correct spot in Magento and updating database is the harder part. In my opinion Apr 29, 2013 at 22:47
  • I know about ImageMagick convert tool, that is how I did comparsion of png to jpeg, but is here an way to get it automatically done ???? for example.. 1. execute an sql request to get all product images inside db... 2. then convert image from png to jpeg... and update extension inside database... for that file, etc.. execute till all files will be processes, do you know which magento table store that data ? Petar Dzhambazov = you exactly right...! Apr 29, 2013 at 22:49
  • See updated answer. Thanks for the clarification - I thought this was a bit off-topic... trying to help out a first-timer here...
    – philwinkle
    Apr 30, 2013 at 0:32
  • so... 1. I doing recursive search of product images... and conveting them with convert for example.. 2. running php script which you mention in order to update filename extension inside db, right ? please confirm, and thanks for that script :) May 2, 2013 at 10:54
  • Yes, you've got it.
    – philwinkle
    May 2, 2013 at 12:59

You can process in the following way:

  • Collect all png images from Magento.
  • Mass convert them into jpeg.
  • Put all jpeg images in media/import folder.
  • reimport all products by replacing images extension.
  • Why is this voted down? This seems a much simpler way to me thanks to Magento's ability to resize everything according to view.xml configuration. Plus, as someone else mentioned, Photoshop can handle this just fine en masse. Sep 6, 2020 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.