0

Magento2 how to developed custom script export for list of all product images with associated sku, Alt-Text, Image path,Image size please if anyone creates the script to share with me as soon as possible

1 Answer 1

1

There are two ways to export csv, using script and another way is using sql queries

This is using Script with Magento

<?php

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;

require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('Magento\Framework\App\State')->setAreaCode('frontend');
$fileFactory = $objectManager->get('Magento\Framework\App\Response\Http\FileFactory');
$csvProcessor = $objectManager->get('Magento\Framework\File\Csv');
$directoryList = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList');
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection = $productCollection->addAttributeToSelect('*')->load();

$content[] = ['sku' => __('Sku'),
'label' => __('Alt Text'),
'value' => __('Image Path'),
'image-width' => __('Image Width'),
'image-height' => __('Image Height'),];

$fileName = 'product_export.csv'; // Add Your CSV File name
$filePath = $directoryList->getPath(DirectoryList::MEDIA) . "/" . $fileName;

foreach ($collection as $product){
    // print_r($product->getData());
    // die;
    //echo 'Name  =  '.$product->getName().'<br>';
    $productFactory = $objectManager->get('Magento\Catalog\Model\ProductFactory')->create()->load($product->getId());
    $productImages = $productFactory->getMediaGalleryImages();
    $label = '';
    foreach ($productImages as $image) {
        $label = $image->getLabel();
    }
    
    list($width, $height, $type, $attr) = getimagesize("pub/media/catalog/product/".$product->getImage());

    $content[] = [$product->getSku(),
        $label,
        $product->getImage(),
        $width,
        $height,];
        //print_r( $product->getImage()); die;
} 

$csvProcessor->setEnclosure('"')->setDelimiter(',')->saveData($filePath, $content);
$fileFactory->create($fileName, ['type' => "filename",
    'value' => $fileName,
    'rm' => true,
    // True => File will be remove from directory after download.
], DirectoryList::MEDIA, 'text/csv', null);

And this is another way where we are using sql queries

<?php

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;

require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('Magento\Framework\App\State')->setAreaCode('frontend');
$fileFactory = $objectManager->get('Magento\Framework\App\Response\Http\FileFactory');
$csvProcessor = $objectManager->get('Magento\Framework\File\Csv');
$directoryList = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList');
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection = $productCollection->addAttributeToSelect('*')->load();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();

//Select Data from table
$sql = "SELECT catalog_product_entity.sku, catalog_product_entity_media_gallery_value.value_id, catalog_product_entity_media_gallery_value.label, catalog_product_entity_media_gallery.value FROM `catalog_product_entity` LEFT JOIN catalog_product_entity_media_gallery_value ON catalog_product_entity.entity_id=catalog_product_entity_media_gallery_value.entity_id
LEFT JOIN catalog_product_entity_media_gallery ON catalog_product_entity_media_gallery_value.value_id = catalog_product_entity_media_gallery.value_id;";
$result = $connection->fetchAll($sql); 

$content[] = ['sku' => __('Sku'),
    'label' => __('Alt Text'),
    'value' => __('Image Path'),
    'image-width' => __('Image Width'),
    'image-height' => __('Image Height'),];

$fileName = 'product_export.csv'; // Add Your CSV File name
$filePath = $directoryList->getPath(DirectoryList::MEDIA) . "/" . $fileName;

foreach ($result as $product){
    list($width, $height, $type, $attr) = getimagesize("pub/media/catalog/product/".$product['value']);

    $content[] = [$product['sku'],
    $product['label'],
    $product['value'],
    $width,
    $height,];
    //print_r( $product->getImage()); die;
} 

$csvProcessor->setEnclosure('"')->setDelimiter(',')->saveData($filePath, $content);
$fileFactory->create($fileName, ['type' => "filename",
    'value' => $fileName,
    'rm' => true,
    // True => File will be remove from directory after download.
], DirectoryList::MEDIA, 'text/csv', null);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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