Undefined index "media_type" while adding image to product's media gallery

Since addImageToMediaGallery is not added to interface \Magento\Catalog\Api\Data\ProductInterface, I'd not recommend to use addImageToMediaGallery with ProductRepository.

For better practice you can try to use Magento2 Service Layer. You can save a new image using \Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface::create

If you will go deep to the code, under the hood of the method the attribute "media_gallery" will be set and then \Magento\Catalog\Api\ProductRepositoryInterface::save will be invoked. And inside of this method the method \Magento\Catalog\Model\Product\Gallery\Processor::addImage will be invoked, which basically invoked, when you run addImageToMediaGallery


Pretty sure you have to initialize the media gallery first. I've done that several times and the code I used was always like this:

->setMediaGallery(
    [
        'images'=>  [],
        'values'=>  []
    ]
) // Media gallery initialization
->addImageToMediaGallery(
    $image,
    [
        'image',
        'thumbnail',
        'small_image'
    ],
    false,
    false) // Assigning image, thumb and small image to media gallery