Magento 2.2 adding images to product programmatically does not set small_image

Magento 2.2 adding images to product programmatically does not set small_image

September 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2.2 adding images to product programmatically does not set small_image

Title: Solving Magento 2.2 Image Upload Issues When Adding Products Programmatically

Introduction

When developing Magento extensions or customizing product management features programmatically, you may encounter issues while adding images to products. One common problem is that the small_image and thumbnail attributes are not set correctly when using the addImageToMediaGallery method.

This post will guide you through solving this issue efficiently in Magento 2.2.

Understanding the Problem

The primary challenge lies in maintaining image order while setting the base, small, and thumbnail images for each product. The provided code snippet sorts the images array but fails to set the correct attributes when adding them.

ksort($prod_images);
foreach($prod_images as $key => $img){
    //...
    if($key == 1){
        $prod->addImageToMediaGallery($imgPath, array('image', 'small_image','thumbnail'), false, false);
    }else{
        $prod->addImageToMediaGallery($imgPath, null, false, false);
    }
    $prod->save();
}

While this approach orders the images, it does not correctly assign the primary image as required.

The Current Workaround

The initial workaround involves reversing the images array and adding the first image last. This method sets the correct attributes but reverses the desired order:

rsort($prod_images);
foreach($prod_images as $key => $img){
    //...
    if($key == 1){
        $prod->addImageToMediaGallery($imgPath, array('image', 'small_image','thumbnail'), false, false);
    }else{
        $prod->addImageToMediaGallery($imgPath, null, false, false);
    }
    $prod->save();
}

However, this solution does not preserve the original order and may require additional steps to correct it after saving the product.

The Efficient Solution

A more elegant approach is to directly set the primary image attributes when adding the first image. To achieve this, you can use the setImageToMediaGallery method instead of addImageToMediaGallery:

ksort($prod_images);
foreach($prod_images as $key => $img){
    //...
    if($key == 1){
        $prod->setImageToMediaGallery($imgPath, array('image', 'small_image','thumbnail'));
    }else{
        $prod->addImageToMediaGallery($imgPath, null, false, false);
    }
    $prod->save();
}

This method ensures that the first image is correctly set as both image and small_image, preserving the original order.

Conclusion

When working with Magento 2.2 product images programmatically, it’s essential to use the appropriate methods for assigning primary image attributes. By leveraging the setImageToMediaGallery method instead of addImageToMediaGallery, you can efficiently set the base, small, and thumbnail images while preserving the desired order.

This approach eliminates the need for workarounds like reversing the array or recalling existing images after saving the product. It provides a clean and efficient solution to the image management issue in Magento 2.2.

For more advanced Magento development techniques and customization options, feel free to explore our resources at Laravel Company.

Magento 2 Catalog Extensions Configuration

Our network