Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Programmatically Manipulate A Product’s Media Gallery
 
SpeedBird
Sr. Member
 
Total Posts:  85
Joined:  2009-12-03
 

I have managed to bulk upload a few thousand products (with images, options etc.) programmatically, now I find I need to manipulate a lot of the images. Specifically, I need to configure some of the images to be the ‘small_image’ for the product, and there are some duplicate images I want to delete.

I have found I can easily add images to the existing products, for example:

<?php
umask
(0);
require_once 
"/var/www/my_website/app/Mage.php";
require_once 
"/var/www/my_website/app/dbconn.php";
Mage::app('default');

/*
Some SQL to determine highest & lowest product IDs
*/
$lowest_sql "
SELECT
entity_id

FROM
catalog_product_entity

ORDER BY
entity_id

ASC LIMIT 1
"
;
$lowest_query mysql_query($lowest_sql,$conn);
$lowest mysql_result($lowest_query,0,'entity_id');
$highest_sql "
SELECT
entity_id

FROM
catalog_product_entity

ORDER BY
entity_id

DESC LIMIT 1
"
;
$highest_query mysql_query($highest_sql,$conn);
$highest mysql_result($highest_query,0,'entity_id');

for(
$i $lowest$i $highest$i++){
       
       
/* Load current product */
       
$product Mage::getModel('catalog/product')->load($i);

    
/* Current product's media gallery */
       
$image_array $product->getMediaGallery('images');   
       echo 
print_r($image_array);
       
/* This will output:
       Array 
       ( 
       [0] => Array 
        ( 
        [value_id] => 1 
        [file] => /file/path/1.jpg 
        [label] => 
        [position] => 1 
        [disabled] => 0 
        [label_default] => 
        [position_default] => 
        [disabled_default] => 
        ) 
       [1] => Array 
        ( 
        [value_id] => 1177 
        [file] => /file/path/2.jpg 
        [label] => 
        [position] => 1 
        [disabled] => 0 
        [label_default] => 
        [position_default] => 
        [disabled_default] => 
        ) 
       )
       1

       The trouble is, none of the array elements specify base, small_image or thumbnail...
       */

       /*
       If I wanted to add a small_image to the product:
       */
       
$product->addImageToMediaGallery('/imagename.jpg',array('small_image'),true,false);

       
/*
       Save product
       */
       
try{
        $product
->save();
       
}catch(Exception $ex){
   
       }
}

/*
Cleanup
*/
mysql_close($conn);
?>

But I can’t work out how to modify existing images?

 
Magento Community Magento Community
Magento Community
Magento Community
 
puntubabu
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2011-07-11
 

You can try with this

if ($product->getId()){
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$items = $mediaApi->items($product->getId());
foreach($items as $item)
$mediaApi->remove($product->getId(), $item[’file’]);
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
szokart
Member
 
Avatar
Total Posts:  38
Joined:  2008-08-21
Bialystok, Poland
 

I try:

$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$items = $mediaApi->items($product->getId());
foreach($items as $item)
$mediaApi->remove($product->getId(), $item[’file’]);

but I have error

Fatal error: Uncaught exception ‘Mage_Eav_Model_Entity_Attribute_Exception’ with message ‘SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`base_xxx`.`catalog_product_entity_media_gallery_value`, CONSTRAINT `FK_CAT_PRD_ENTT_MDA_GLR_VAL_VAL_ID_CAT_PRD_ENTT_MDA_GLR_VAL_ID` FOREIGN KEY (`value_id`) REFERENCES `catalog_prod)’ in /home/users/xxx/public_html/app/code/core/Mage/Core/Model/Config.php:1348 Stack trace: #0 /home/users/xxx/public_html/app/Mage.php(462): Mage_Core_Model_Config->getModelInstance(’eav/entity_attr...’, ‘SQLSTATE[23000]...’) #1 /home/users/xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(661): Mage::getModel(’eav/entity_attr...’, ‘SQLSTATE[23000]...’) #2 /home/users/xxx/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1648): Mage_Eav_Model_Entity_Abstract->walkAttributes(’backend/afterSa...’, Array) #3 /home/users/xxx/public_html/app/code/core/Mage/Catalog/M in /home/users/xxx/public_html/app/code/core/Mage/Core/Model/Config.php on line 1348

 
Magento Community Magento Community
Magento Community
Magento Community
 
BelVG
Guru
 
Avatar
Total Posts:  306
Joined:  2011-02-16
Minsk, Belarus
 

Hi septsite,

try this code:

Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
        
$mediaApi Mage::getModel("catalog/product_attribute_media_api");
        
$items $mediaApi->items($product->getId());
        
$attributes $product->getTypeInstance()->getSetAttributes();
        
$gallery $attributes['media_gallery'];
        foreach(
$items as $item){
            
if ($gallery->getBackend()->getImage($product$item['file'])) {
                $gallery
->getBackend()->removeImage($product$item['file']);
            
}
        }
        $product
->save();
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top