简体   繁体   中英

How do I replace a product image in Magento?

I need to replace a product image with a new one.
I need to replace image in position 2 with the new image.
This is my code.

$position = 2;//this is the image to delete
$file_location = "../WS-Access-000036-Black-2.jpg";
$sku = "WS-Access-000036-Black";



//delete image
$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$product_id = $product->getId();
$items = $mediaApi->items($product_id);
foreach($items as $item){
    if($item['position']=="$position" or ($position==1 and $item['position']=="0")){

        $mediaApi->remove($product_id, $item['file']);//this doesnt remove file from directory

        $image_location = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product' . $item['file'];
        unlink ($image_location);//have to use this to delete image from disk because $mediaApi->remove doesn't delete it from disk
        break;
    }
}
//end delete image
//EVERYTHING UP TO HERE WORKS, THE IMAGE IS DELETED FROM THE DISK AND
//IN THE BACKEND OF THE PRODUCT




//save new image
$pathInfo = pathinfo($file_location);
switch($pathInfo['extension']){
    case 'png':
        $mimeType = 'image/png';
        break;
    case 'jpg':
        $mimeType = 'image/jpeg';
        break;
    case 'gif':
        $mimeType = 'image/gif';
        break;
}


$types = ($position == 1) ? array('image', 'small_image', 'thumbnail') : array();


$newImage = array(
    'file' => array(
        'content' => base64_encode($file_location),
        'mime' => $mimeType,
        'name' => pathinfo($sku."-".$position, PATHINFO_FILENAME), 
    ),
    'label' => '', 
    'position' => $position,
    'types' => $types,
    'exclude' => 0,
);

$media = Mage::getModel('catalog/product_attribute_media_api');
$media->create($sku, $newImage);
//end save new image

My issue is that when I delete image WS-Access-000036-Black-2.jpg from disk and the products gallery then I add the new WS-Access-000036-Black-2.jpg to the products gallery it loads the old WS-Access-000036-Black-2.jpg.
I'm thinking its a caching issue but i've cleared my cache and the old image keeps loading.
It's only when I change the name under this line

'name' => pathinfo($sku."-".$position, PATHINFO_FILENAME),

that everything works correctly.
I need to keep the original file name.
Why is this happening?

It is understood that you are still seeing the old image but have you checked whether the new image inserted at the 2nd position in disk?

If the new image inserted then probably you need to go to admin, System -> Cache Management & refresh all cache + please flush catalog images cache.

Also please try checking this in another browser.

Thank you.

Do This One If the new image inserted then probably you need to go to admin, System -> Cache Management & refresh all cache + please flush catalog images cache

After Cache Clear it will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM