We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Step 1). Get the Product image from URL – create a file in Helper/Data.php

    public function getProductImage($imgurl){
    	 		$fileSystem = $this->_objectManager->create('MagentoFrameworkFilesystem');
    	        $mediaPath = $fileSystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath();
    	        if (!file_exists($mediaPath.'imagefolder')) {
    	            mkdir($mediaPath.'imagefolder', 0777, true);
    	        }
    	        $url = $imgurl;
    	        $ch = curl_init($url);
    	        $my_save_dir = $mediaPath.'imagefolder/';
    	        $filename = basename($url);
    	        $complete_save_loc = $my_save_dir . $filename;
    	        $fp = fopen($complete_save_loc, 'wb');
    	        curl_setopt($ch, CURLOPT_FILE, $fp);
    	        curl_setopt($ch, CURLOPT_HEADER, 0);
    	        curl_setopt($ch, CURLOPT_TIMEOUT, 28800);
    	        $return = curl_exec($ch);
    	        curl_close($ch);
    	        $image = $return;
            	return $image;
            }

    Step 2). Assign the images to existing products.

    $product = $this->productFactory->create()->load($proid);
    	$getimgurl = $url;
    	$imagename= basename($getimgurl);
    	$imgpath = $mediaPath.'imagefolder/'.$imagename;
    	if (!file_exists($imgpath)) { //check image already exist in directory
    	    $this->helperData->getProductImage($getimgurl);
    	}
    $product->addImageToMediaGallery($imgpath, array('image', 'small_image', 'thumbnail','media_gallery'), false, false);
    $product->save();

     

    field_5bfb909c5ccae

      Get a Free Quote