Do you mean resizing it in the array? In that case you can use the Magento catalog image helper.
'product_image' => (string)Mage::helper('catalog/image')->init($asc_product, 'thumbnail')->resize(100);
This would return you the url to the resized image.
Try passing an array as the media type. Theer is no need to iterate over each mediaType and setting them individually.
your code would thus be, which will be faster as well.
ini_set('memory_limit', '-1');
require 'app/Mage.php';
Mage::app();
echo "Loading catalog...\n";
$products = Mage::getModel('catalog/product')->setStoreId(13)->getCollection()->addAttributeToSelect('*');
echo count($products) . " products selected for image import.\n";
// Remove unset images, add image to gallery if exists
$importDir = Mage::getBaseDir('media') . DS . 'import/';
foreach ($products as $product) {
$sku = Mage::getModel('catalog/product')->load($product->getId())->getSku();
if (strstr($sku, " ")) {
echo "Sku $sku contains whitespace, skipping.\n";
continue;
}
$imgFile = $importDir . $sku . ".jpg";
// Add three image sizes to media gallery
$mediaArray = array(
'thumbnail',
'small_image',
'image'
);
if (file_exists($imgFile)) {
try {
$product->addImageToMediaGallery($imgFile, $mediaArray, false, false);
$product->save();
echo "Sku: $sku updated.\n";
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
echo "Could not match image to $sku. Path was: {$filePath}\n";
break;
}
}
Anyways, I suspect your issue is related to the fact that your product->save was within the media loop of your original code.
For reference, the end-point of the mediaAttribute is this function, located in class Mage_Catalog_Model_Product_Attribute_Backend_Media
/**
* Set media attribute value
*
* @param Mage_Catalog_Model_Product $product
* @param string|array $mediaAttribute
* @param string $value
* @return Mage_Catalog_Model_Product_Attribute_Backend_Media
*/
public function setMediaAttribute(Mage_Catalog_Model_Product $product, $mediaAttribute, $value)
{
$mediaAttributeCodes = array_keys($product->getMediaAttributes());
if (is_array($mediaAttribute)) {
foreach ($mediaAttribute as $atttribute) {
if (in_array($atttribute, $mediaAttributeCodes)) {
$product->setData($atttribute, $value);
}
}
} elseif (in_array($mediaAttribute, $mediaAttributeCodes)) {
$product->setData($mediaAttribute, $value);
}
return $this;
}
Best Answer
request url :
magentourl/index.php/rest/V1/products/sku/media
use this :