Magento 2: How to Set Product Image Using Image URL Programmatically
Learn how to automatically assign product images in Magento 2 using a direct image URL. This tutorial demonstrates how to download an image from a remote location and attach it to a product's media gallery programmatically.
Overview
When importing products from external systems, APIs, ERPs, or marketplaces, you may need to assign product images directly from image URLs. Magento 2 provides methods to add images to the media gallery programmatically. In this tutorial, we'll create a simple PHP script that downloads an image from a URL and sets it as the product's Base Image, Small Image, and Thumbnail Image.
Step 1: Create PHP Script File
Create a file in your Magento root directory:
<?php
use Magento\Framework\AppInterface;
try
{
require_once __DIR__ . '/app/bootstrap.php';
}
catch (\Exception $e)
{
echo 'Autoload error: ' . $e->getMessage();
exit(1);
}
try
{
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$product_id = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager
->create('Magento\Catalog\Model\Product')
->load($product_id);
saveimage(
$product,
"https://yourdomain.com/media/logo/websites/1/image.png"
);
$product->save();
echo "Product Save with images";
}
catch(\Exception $e)
{
echo "Error : ".$e->getMessage();
}
function saveimage($product, $imageUrl, $visible = false, $imageType = [])
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$file = $objectManager->get(
'Magento\Framework\Filesystem\Io\File'
);
$tmpDir = getMediaDirTmpDir();
$file->checkAndCreateFolder($tmpDir);
$newFileName = $tmpDir . basename($imageUrl);
$result = $file->read($imageUrl, $newFileName);
if ($result) {
$product->addImageToMediaGallery(
$newFileName,
['image','small_image','thumbnail'],
false,
$visible
);
}
return $result;
}
function getMediaDirTmpDir()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$directoryList = $objectManager->get(
'Magento\Framework\App\Filesystem\DirectoryList'
);
return $directoryList->getPath(
Magento\Framework\App\Filesystem\DirectoryList::MEDIA
) . DIRECTORY_SEPARATOR . 'tmp';
}
?>
Step 2: Execute the Script
After creating the file, run the following URL in your browser:
Once executed successfully, the image will be downloaded and assigned to the specified product.
Important Notes
- Use a publicly accessible image URL.
- The URL must contain a valid image file extension.
- Supported formats include JPG, JPEG, PNG
- Verify that the product ID exists before executing the script.
https://yourdomain.com/media/logo/websites/1/image.png
Result
After executing the script successfully, Magento 2 will:
- Download the image from the provided URL.
- Add the image to the product media gallery.
- Assign it as Base Image.
- Assign it as Small Image.
- Assign it as Thumbnail Image.