Skip to content

Magento 2.3 Fix Notice and Exception while adding image to product programmatically #18952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 16, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions app/code/Magento/Catalog/Model/Product/Gallery/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Catalog\Model\Product\Gallery;

use Magento\Framework\Api\Data\ImageContentInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DriverInterface;
Expand Down Expand Up @@ -56,25 +57,34 @@ class Processor
*/
protected $resourceModel;

/**
* @var \Magento\Framework\File\Mime
*/
protected $mime;

/**
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
* @param \Magento\Framework\File\Mime $mime
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function __construct(
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
\Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb,
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
\Magento\Framework\Filesystem $filesystem,
\Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
\Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel,
\Magento\Framework\File\Mime $mime
) {
$this->attributeRepository = $attributeRepository;
$this->fileStorageDb = $fileStorageDb;
$this->mediaConfig = $mediaConfig;
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->resourceModel = $resourceModel;
$this->mime = $mime;
}

/**
Expand Down Expand Up @@ -183,6 +193,13 @@ public function addImage(
$attrCode = $this->getAttribute()->getAttributeCode();
$mediaGalleryData = $product->getData($attrCode);
$position = 0;

$absoluteFilePath = $this->mediaDirectory->getAbsolutePath($file);
$imageMimeType = $this->mime->getMimeType($absoluteFilePath);
$imageContent = $this->mediaDirectory->readFile($absoluteFilePath);
$imageBase64 = base64_encode($imageContent);
$imageName = $pathinfo['filename'];

if (!is_array($mediaGalleryData)) {
$mediaGalleryData = ['images' => []];
}
Expand All @@ -197,9 +214,17 @@ public function addImage(
$mediaGalleryData['images'][] = [
'file' => $fileName,
'position' => $position,
'media_type' => 'image',
'label' => '',
'disabled' => (int)$exclude,
'media_type' => 'image',
'types' => $mediaAttribute,
'content' => [
'data' => [
ImageContentInterface::NAME => $imageName,
ImageContentInterface::BASE64_ENCODED_DATA => $imageBase64,
ImageContentInterface::TYPE => $imageMimeType,
]
]
];

$product->setData($attrCode, $mediaGalleryData);
Expand Down