One of the biggest issue was product image duplicate on 2.2.* the issue of storage. The below script delete the unused product images.
Create a php file in your magento root directory like '/public_html/findduplicateimage.php' and past the code
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = true; // default false
$params[Bootstrap::PARAM_REQUIRE_IS_INSTALLED] = false; // default true
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$fileSystem = $objectManager->create('\Magento\Framework\Filesystem');
$mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();
//echo $mediaPath;
$path = $mediaPath . DIRECTORY_SEPARATOR . "catalog" . DIRECTORY_SEPARATOR . "product";
$product_folder = array();
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
//echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
if (!is_dir($filename)) {
$folder_file = str_replace($mediaPath, '', $filename);
$folder_file = str_replace('\\', '/', $folder_file);
$product_folder[] = $folder_file;
}
}
// creat and load all product collection
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
->addAttributeToSelect('*')
->load();
$images_array = array();
foreach ($collection as $product) {
$objectManager->get("Magento\Catalog\Model\Product\Gallery\ReadHandler")->execute($product);
$imageProcessor = $objectManager->create('\Magento\Catalog\Model\Product\Gallery\Processor');
$images = $product->getMediaGalleryImages();
foreach ($images as $child) {
$file_update = str_replace($mediaPath, "", $child->getPath());
$images_array[] = "/" . $file_update;
}
}
// uncoment below code print array all catalog media array & product use media file array
/*echo "<pre>";
print_r($images_array);
echo "<br/>=======";
print_r($product_folder);
echo "</pre>";*/
foreach ($product_folder as $file) {
if (!in_array($file, $images_array)) {
//echo "<br/> Unlink Found File :::: ".unlink($mediaPath . $file); // uncoment the code when remove the file
echo "<br/>Found file :::::: ". $mediaPath ." === ".$file;
}
}
die("stop");
?>
Create a php file in your magento root directory like '/public_html/findduplicateimage.php' and past the code
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = true; // default false
$params[Bootstrap::PARAM_REQUIRE_IS_INSTALLED] = false; // default true
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$fileSystem = $objectManager->create('\Magento\Framework\Filesystem');
$mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();
//echo $mediaPath;
$path = $mediaPath . DIRECTORY_SEPARATOR . "catalog" . DIRECTORY_SEPARATOR . "product";
$product_folder = array();
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
//echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
if (!is_dir($filename)) {
$folder_file = str_replace($mediaPath, '', $filename);
$folder_file = str_replace('\\', '/', $folder_file);
$product_folder[] = $folder_file;
}
}
// creat and load all product collection
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
->addAttributeToSelect('*')
->load();
$images_array = array();
foreach ($collection as $product) {
$objectManager->get("Magento\Catalog\Model\Product\Gallery\ReadHandler")->execute($product);
$imageProcessor = $objectManager->create('\Magento\Catalog\Model\Product\Gallery\Processor');
$images = $product->getMediaGalleryImages();
foreach ($images as $child) {
$file_update = str_replace($mediaPath, "", $child->getPath());
$images_array[] = "/" . $file_update;
}
}
// uncoment below code print array all catalog media array & product use media file array
/*echo "<pre>";
print_r($images_array);
echo "<br/>=======";
print_r($product_folder);
echo "</pre>";*/
foreach ($product_folder as $file) {
if (!in_array($file, $images_array)) {
//echo "<br/> Unlink Found File :::: ".unlink($mediaPath . $file); // uncoment the code when remove the file
echo "<br/>Found file :::::: ". $mediaPath ." === ".$file;
}
}
die("stop");
?>
No comments:
Post a Comment