How can I set main product image as thumbnail image programmatically in Magento 2?


I have a loop running trough all products and each product has a main image, only some products have not defined an image for thumbnail. So I want to set the main image also as the product thumbnail for each product.


2 Answers 2


Thank you for the reply.

I already found the answer by myself and I will share it below.

foreach ($products as $product){

    $smallImage = $product->getSmallImage();
    $imagePath  = '/catalog/product' . $product->getImage();

    if( $smallImage == 'no_selection' ){
        echo 'FIXING ' . $imagePath . "\r\n";
        $product->addImageToMediaGallery( $imagePath, array( 'small_image' ), false, false );

But still, I'm curious to know what are the Boolean variables are here.

$product->addImageToMediaGallery( $imagePath, array( 'small_image' ), false, false );
  • 2
    The first is $move - if true, it will move source file. The second is $exclude - if set true, mark image as disabled in product page view Nov 15, 2016 at 18:25
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load product object
$mediaAttribute = array ('thumbnail','small_image','image');

$mediaDir = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList')->getPath('media');// Im Magento 2
$mediaDir = Mage::getBaseDir('media');// In Magento 1

//Case 1: When image files are alredy in your server
$fpath = 'product/images/';// path to your file
$count = 0;
$imgArray  = array('image1.png','image2.png','image3.png','image4.png');
foreach ($imgArray as $image){
    $imgUrl = _save_image( $fpath.$image,$objectManager,$mediaDir );// copies the file from your local storage to  your-magento-root-path/pub/media   
    if ($count == 0){
        $product->addImageToMediaGallery( $imgUrl , $mediaAttribute, true, false ); 
    }else {
        $product->addImageToMediaGallery( $imgUrl , null, true, false );

function _save_image($img,$objectManager,$mediaDir) {
    $imageFilename      = basename($img);    
    $image_type         = substr(strrchr($imageFilename,"."),1); //find the image extension
    $filename           = md5($img . strtotime('now')).'.'.$image_type; //give a new name, you can modify as per your requirement

    if (!file_exists($mediaDir)) mkdir($mediaDir, 0777, true);
    else chmod($mediaDir, 0777);

    $filepath           = $mediaDir . '/'. $filename; //path for temp storage folder: pub/media
    file_put_contents($filepath, file_get_contents(trim($img))); //store the image from external url to the temp storage folder
    return $filepath;

In your case set only 'thumbnail' in $mediaAttribute array.Hence array will be $mediaAttribute = array ('thumbnail');

  • Thank you for your reply, I already found an answer and posted it here.
    – nuwaus
    Aug 8, 2016 at 14:58

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.