3

I have several hundreds of category Images containing a .png extension. But I want to convert that images to .jpg (outside of Magento and not part of that question) and then reupload them to media/catalog/category and tell Magento to use the new .jpg extension for all .png Category Images. Including Thumbnail Image and Image field.

I found the quite similar Question Migrating All Product Images in Magento from PNGs to JPEGs but the difference is that I want to change Category Images and not Product Images.

Can someone tell me how to change that script or a different approach, please? Updating all Categories in the backend manually would take to much time.

2 Answers 2

3

Since this is a one time change you may wish to consider taking a SQL approach. This will be quicker, and, if you backup your database before hand, you can do this safely.

Your images for the categories are held in catalog_category_entity_varchar table however you did not say whether these are the main images or the thumbnails. Therefore have a quick search of the table to find out:

SELECT * FROM  `catalog_category_entity_varchar` WHERE  `value` LIKE  '%.png%';

This should return images that you know. Look at the value for attribute_id. This could be something like '45'.

Now you want to run an update, replacing xx with your attribute_id:

UPDATE `catalog_category_entity_varchar` SET `value` = REPLACE(`value`, '.png', '.jpg') WHERE `attribute_id`=xx;

If you are not happy with updating the database directly in a few minutes you could write a Magento module, iterate on the categories and update the image accordingly. However, for a one off the sql approach should work out nicely.

1

I accepted the mySQL solution as it seems to be the easier one. But we have changed the code of that script in the meantime so I thought why not add this as well.

  <?php

require('app/Mage.php');
Mage::app();

$storeId = 0;
Mage::app()->setCurrentStore($storeId);

$category = Mage::getModel ( 'catalog/category' );

$tree = $category->getTreeModel ();

$tree->load();
$ids = $tree->getCollection()->getAllIds();

    foreach ($ids as $id) {

        $pk = Mage::getModel('catalog/category');

        $pk->load($id);

        #print_r($pk->getImageUrl()."<br>");

        $pk->setImage(str_ireplace('.png', '.jpg', $pk->getImage()));
        $pk->setThumbnail(str_ireplace('.png', '.jpg', $pk->getThumbnail()));
        $pk->setImageUrl(str_ireplace('.png', '.jpg', $pk->getImageUrl()));
        $pk->save();
        #print_r($pk->getImage()."<br>");

    }

Your Answer

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

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