I have imported the products and product images for my new website. There are more than 5000 products have added. When assigning product image , backside of the product image was added to main image like below. Manually doing is obviously a nightmare.

enter image description here

How can i easily swap this for all products? Any idea or scripts highly appreciated.

Thanks in advance.

2 Answers 2


You may be able to do it with database queries. the image attribute value is stored in the catalog_product_entity_varchar table. you can get the image attribute ID with select from eav_attribute_where attribute_code = 'image' and entity_type_id = (select wntity_type_id from eav_entity_type where entity_type_code = 'catalog_product').

With this id you can identify the image attributes values with select * from catalog_product_entity_varchar where attribute_id = {id from above}.

If you have a special name format for the images that should be set in the image attribute you can try updating this table.

  • thank you, i will try this.
    – Mujahidh
    Nov 17, 2022 at 13:57

I come up with running below script on pub directory to achieve this task. This may help some one in future.

image_swap.php file

use \Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('Magento\Framework\App\State');

$productRepository = $objectManager->get('Magento\Catalog\Model\ProductRepository');
$csv = "sku_vs_image.csv";
if (!empty($argv) && sizeof($argv) > 1) { 
  $csv = $argv[1];
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2000, "\t")) !== FALSE) {
    $num = count($data);
    if ($num < 1) {
    $sku = explode(',',trim($data[0])); 
    $imagePath = $sku[1];

    try {
        $product = $productRepository->get($sku[0]);
        $productId = $product->getId();
        $productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
        $product->addImageToMediaGallery('catalog/product'.$imagePath, array('image', 'small_image', 'thumbnail'), false, false);

        //Get existing image 
        $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
        foreach ($existingMediaGalleryEntries as $key => $entry) {
            //Remove the duplicate image after assigning roles
            if($entry->getFile() === $imagePath ){
    } catch (\Exception $e) {
        echo $sku[0].$e->getMessage()."</br>";

sku_vs_image.csv file sample


Update the csv and run the script from root. script first update the image role and create the image, and remove the existing image which assigned the role after.

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.