问题
我正在与Magento集成,它首先从产品中删除所有图像,然后添加新图像。问题是,当我添加'thumbnail'时,'small_image'和'image'只在默认商店而不是单个商店中设置。
代码
M(:,:,1,1) = [ 225 230 250 243 20 3 244 247 255 255
242 252 239 0 239 224 27 252 255 255
224 239 28 243 236 231 240 11 255 255
243 252 15 224 15 12 251 0 255 255
224 11 252 227 0 11 232 251 0 255
243 4 255 228 19 4 243 224 0 255
0 255 236 251 228 251 224 243 255 0
19 236 255 224 27 12 251 236 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,2,1) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
255 255 0 255 255 255 255 0 255 255
255 255 0 255 0 0 255 0 255 255
255 0 255 255 0 0 255 255 0 255
255 0 255 255 0 0 255 255 0 255
0 255 255 255 255 255 255 255 255 0
0 255 255 255 0 0 255 255 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,3,1) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 0 0 0 255 255 255
255 255 0 0 0 0 0 0 255 255
255 255 0 0 0 0 0 0 255 255
255 0 0 0 0 0 0 0 0 255
255 0 0 0 0 0 0 0 0 255
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,1,2) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
255 255 0 255 255 255 255 0 255 255
255 255 0 255 255 255 255 0 255 255
255 0 255 255 255 255 255 255 0 255
255 0 255 255 255 255 255 255 0 255
0 255 255 255 255 255 255 255 255 0
0 255 255 255 255 255 255 255 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,2,2) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 255 255 0 255 255 255
255 255 0 255 255 255 255 0 255 255
255 255 0 255 255 255 255 0 255 255
255 0 255 255 255 255 255 255 0 255
255 0 255 255 255 255 255 255 0 255
0 255 255 255 255 255 255 255 255 0
0 255 255 255 255 255 255 255 255 0
0 255 255 255 255 255 255 255 255 0
255 0 0 0 0 0 0 0 0 255];
M(:,:,3,2) = [ 255 255 255 255 0 0 255 255 255 255
255 255 255 0 0 0 0 255 255 255
255 255 0 0 0 0 0 0 255 255
255 255 0 0 0 0 0 0 255 255
255 0 0 0 0 0 0 0 0 255
255 0 0 0 0 0 0 0 0 255
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
255 0 0 0 0 0 0 0 0 255];
[map, ~, Mind] = unique(reshape(M,[],3), 'rows');
[row, col, ~, frame]=size(M);
Mind = reshape(Mind, [row, col, frame]);
%%convert to rgb
Mrgb=zeros(row, col, 3, frame);
z = size(map,1);
Mrgb = cat(3, map(Mind), map(Mind + z), map(Mind + 2*z));
Mrgb = reshape(Mrgb, [row, col,3, frame]);
map=uint8(map);
map=im2double(map);
imshow(Mind(:,:,1), map)
有没有办法告诉addImageToMediaGallery在商店数组上设置?或者我错过了什么?
答案 0 :(得分:2)
通过替换
解决了这个问题foreach($images as $image) {
file_put_contents($path . DS . $image->filename, $image->content);
$type = in_array($image->name, array('image', 'small_image', 'thumbnail')) ? $image->name : null;
$product->addImageToMediaGallery($path . DS . $image->filename, $type, true, false);
}
$product->save();
用这个:
$imageTypes = array();
//Sets images on default store for product
foreach($images as $image){
file_put_contents($path . DS . $image->filename, $image->content);
$type = in_array($image->name, array('image', 'small_image', 'thumbnail')) ? $image->name : null;
$product->addImageToMediaGallery($path . DS . $image->filename, $type, true, false);
//Store image, small_image and thumbnail for later use
$imageTypes['image'] = $product->getImage();
$imageTypes['small_image'] = $product->getSmallImage();
$imageTypes['thumbnail'] = $product->getThumbnail();
}
$product->save();
//Go through all stores and set image, small_image and thumbnail
foreach($websiteIds as $websiteId) {
$website = Mage::getModel('core/website')->load($websiteId);
foreach($website->getStoreIds() as $storeId) {
Mage::app()->setCurrentStore($storeId);
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), $imageTypes, $storeId);
}
}
$product->save();