7

I'm moving my store from ver 1.7.2 to 2.0.2.

Since I have less than 200 products I will export/import them using CSV files, however I'd like to know how to import product images.

  • > You can use a different folder on the Magento server, as long as the > path to the folder is specified during the import process. I'm trying to do so but did not find the correct way. I want to sort my files myself. All files start with TO100.2240.png TO120.6255.png and so on. So I want to put them in folders TO100/ TO120/ when I now import into magento2 magento plces the files in /t/0/ folder. So all files are in the same folder 4000+ images. like: pub/media/catalog/labels/to100 files start to100 pub/media/catalog/labels/to120 files start to120 wat is the exact pad then? – geertDL Jan 28 at 15:18
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review – Rama Chandran M Jan 28 at 15:36
8

Magento 2 uses the same folder structure for product images. Assuming your CSV contains the product image attribute, you can simply copy /media/catalog/ to /pub/media/catalog/

  • 1
    Not enough. At least not in Magento 2.1.5. You must copy the whole /media/catalog/product/ to /pub/media/import and then run de CSV list import. Don't forget to specify the path for importing images: /pub/media/import – Alain Banutz Mar 14 '17 at 14:32
17

Method 1: Import Images from the Local Server

  1. On the Magento server, upload the image files to the pub/media/import folder. This is the default folder for importing product images.

    [magento installation folder]/pub/media/import 
    

    You can use a different folder on the Magento server, as long as the path to the folder is specified during the import process.

  2. In the CSV data, enter the name of each image file to be imported on the correct row, by sku, and in the correct column according to image type (base_image, small_image, thumbnail_image, or additional_images).

    For images in the default import folder(/pub/media/import), do not include the path before the filename in the CSV data.

    The CSV file must include only the sku column and the related image columns.

    CSV Import Image from Default Location
                                            CSV Import Image from Default Location

  3. Follow the instructions to import the data.

    After selecting the file to import, enter the relative path following Images File Directory:

    /pub/media/import
    

    Images File Directory
                                                            Images File Directory

    If importing multiple images for a single sku, insert a blank row below the sku, and enter the additional image file names in the appropriate columns. The additional rows are understood to belong to the parent sku.

Method 2: Import Images from External Server

  1. Upload the images to be imported to the designated folder on the external server.
  2. In the CSV data, enter the full URL for each image file in the correct column by image type (base_image, small_image, thumbnail_image, or additional_images).

    http://example.com/images/image.jpg
    
  3. Follow the instructions to import the data.

http://docs.magento.com/m2/ce/user_guide/system/data-import-product-images.html

  • 1
    This answer is much more verbose and helpful. It should definitely be chosen as the correct answer IMO. – colefner May 2 '17 at 21:45
  • Does anyone know why this doesn't work as described in the docs? I have posted a question related to this, here: magento.stackexchange.com/questions/189344/… – robgt Aug 23 '17 at 11:04
  • Import image from external url will save image name as it is e.g http://example.com/images/image.jpg will save image name as: httpexamplecomimagesimage.jpg this can't be right? – Juliano Vargas Mar 12 '18 at 16:26
0

The following code set main and additional images magento 2

if ($mainImage) {
    $image_directory = $mediaPath . DS . 'data' . DS . $mainImage;
    if (file_exists($image_directory)) {
        $product->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
            ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);//assigning image, thumb and small image to media gallery
    } 
    else {
        $image_directory = $mediaPath . 'data' . DS . 'comingsoon.jpg';
        $product->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
            ->addImageToMediaGallery($image_directory, array('image', 'thumbnail', 'small_image'), false, false);
    }
}

// additional images
if ($importProduct[29] != '') {
    $addImages = explode(",", trim($importProduct[29]));
    foreach ($addImages as $additional_image) {
        $image_directory = $mediaPath .DS.'data'.DS. trim($additional_image);
        if (file_exists($image_directory)) {
            $product->addImageToMediaGallery($image_directory, null, false, false);
        } 
        else {
            $image_directory = $mediaPath . 'data' . DS . 'comingsoon.jpg';
            $product->addImageToMediaGallery($image_directory, null, false, false);
        }
    }
    echo 'Additional images for product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
}

Please click here for detailed explanation and absolute media path. http://www.pearlbells.co.uk/code-snippets/import-product-images-magento-2-programmatically/

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.