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(

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

    $originalSku = $product->getSku();

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.


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();

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



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


with initialize and save function.


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

You can find out from here.

public function execute()
        try {
            $uploader = $this->_objectManager->create(
                ['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');
            /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
            $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
            $config = $this->_objectManager->get('Magento\Catalog\Model\Product\Media\Config');
            $result = $uploader->save($mediaDirectory->getAbsolutePath($config->getBaseTmpMediaPath()));

                ['result' => $result, 'action' => $this]


            $result['url'] = $this->_objectManager->get('Magento\Catalog\Model\Product\Media\Config')
            $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');
        return $response;

File Path :


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.