3

In Magento 2, if you navigate to

 Products->Catalog->Edit->Images and Videos

you can add images to your products. The uploader will upload a temp image immediately, and after a product save there will be appropriately sized images in pub/media/catalog/product/cache.

Where is the code that handles saving the product gallery image data to the database, and where is the code that handles creating the new media image files on disk? The product save controller

#File: vendor/magento/module-catalog/Controller/Adminhtml/Product/Save.php
public function execute()
{
    //...
    $product = $this->initializationHelper->initialize(
        $this->productBuilder->build($this->getRequest())
    );
    $this->productTypeManager->processProduct($product);

    if (isset($data['product'][$product->getIdFieldName()])) {
        throw new \Magento\Framework\Exception\LocalizedException(__('Unable to save product'));
    }

    $originalSku = $product->getSku();
    $product->save();
    //...
}

Doesn't contain anything obvious about image saving, so the code is probably somewhere down the initializationHelper, productBuilder, productTypeManager, or save call stacks. I'm hoping someone here knows where this data gets saved in Magento 2.

1

Take a look: vendor/magento/module-catalog/Model/Product.php

public function afterSave()
{
        ......
        // Resize images for catalog product and save results to image cache
        /** @var Product\Image\Cache $imageCache */
        if (!$this->_appState->isAreaCodeEmulated()) {
            $imageCache = $this->imageCacheFactory->create();
            $imageCache->generate($this);
        }
        ......
}

As we can see, the process image is in afterSave() method.
There are some classes handle the process image:

vendor/magento/module-catalog/Model/Product/Image.php
vendor/magento/module-catalog/Helper/Image.php

0

when you upload before save. the image was save with filename.tmp. you should research class

'Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper'

with initialize and save function.

and

vendor\magento\module-catalog\Model\Product\Gallery\CreateHandler.php with moveImageFromTmp function

that's the best answer for you.

  • Not sure I follow this -- where in the initialization helper does data get saved to the database? – Alan Storm Nov 4 '16 at 15:22
0

You can find out from here.

public function execute()
    {
        try {
            $uploader = $this->_objectManager->create(
                'Magento\MediaStorage\Model\File\Uploader',
                ['fileId' => 'image']
            );
            $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
            /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
            $imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
            $uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(true);
            /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
            $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
                ->getDirectoryRead(DirectoryList::MEDIA);
            $config = $this->_objectManager->get('Magento\Catalog\Model\Product\Media\Config');
            $result = $uploader->save($mediaDirectory->getAbsolutePath($config->getBaseTmpMediaPath()));

            $this->_eventManager->dispatch(
                'catalog_product_gallery_upload_image_after',
                ['result' => $result, 'action' => $this]
            );

            unset($result['tmp_name']);
            unset($result['path']);

            $result['url'] = $this->_objectManager->get('Magento\Catalog\Model\Product\Media\Config')
                ->getTmpMediaUrl($result['file']);
            $result['file'] = $result['file'] . '.tmp';
        } catch (\Exception $e) {
            $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
        }

        /** @var \Magento\Framework\Controller\Result\Raw $response */
        $response = $this->resultRawFactory->create();
        $response->setHeader('Content-type', 'text/plain');
        $response->setContents(json_encode($result));
        return $response;
    }

File Path :

vendor/magento/module-catalog/Controller/Adminhtml/Product/Gallery/Upload.php

I hope useful for you.

  • 2
    Not sure I follow this. The above is the upload constructor that creates the temp file -- gallery data and files aren't created until the save controller runs. Am I missing something, or did you misunderstand the question? – Alan Storm Nov 4 '16 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.