Skip to content

Commit

Permalink
Upload Module
Browse files Browse the repository at this point in the history
  • Loading branch information
magegadgets committed Jul 16, 2021
0 parents commit f8d24a9
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Console/Removeduplicate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
namespace Magegadgets\RemoveDuplicateImage\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Removeduplicate extends Command
{
protected function configure()
{
$this->setName('duplicate:remove');
$this->setDescription('Demo command line');

parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$app_state = $objectManager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('global');
ini_set('memory_limit','10240M');
ini_set('max_execution_time', 0);
set_time_limit(0);
$mediaApi = $objectManager->create('\Magento\Catalog\Model\Product\Gallery\Processor');
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeManager->setCurrentStore(0);
$mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$directoryList=$objectManager->get('\Magento\Framework\App\Filesystem\DirectoryList');
$path = $directoryList->getPath('media');
$productCollection=$objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$_products = $productCollection->addAttributeToSelect('*')->load();

$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$i =0;
$total = count($_products);
$count = 0;
$productCount = array();
foreach($_products as $_prod) {
$_product = $productRepository->getById($_prod->getId());
$_product->setStoreId(0);
$_md5_values = array();
$base_image = $_product->getImage();

if($base_image != 'no_selection') {
$mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$filepath = $path.'/catalog/product' . $base_image ;
if (file_exists($filepath)) {
$_md5_values[] = md5(file_get_contents($filepath));
}
$i++;

echo "\r\n processing product $i of $total ";
// Loop through product images
$gallery = $_product->getMediaGalleryEntries();
if ($gallery) {
foreach ($gallery as $key => $galleryImage) {
//protected base image
if($galleryImage->getFile() == $base_image) {
continue;
}
$filepath = $path.'/catalog/product' .$galleryImage->getFile();

if(file_exists($filepath)) {
$md5 = md5(file_get_contents($filepath));
} else {
continue;
}

if( in_array( $md5, $_md5_values )) {
if (count($galleryImage->getTypes()) > 0) {
continue;
}
unset($gallery[$key]);
echo "\r\n removed duplicate image from ".$_product->getSku();
$count++;
} else {
$_md5_values[] = $md5;
}
}

$_product->setMediaGalleryEntries($gallery);
$productRepository->save($_product);
}
//$_product->save();
}
}

$output->writeln("\r\n Duplicate Images are Removed");
}
}
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Remove Duplicate Product images in Magento 2

This Extension allows you to find duplicate product images from your product list and from this list you can easily remove them by running a command.


**IMPORTANT:** *Remove Duplicate Product Images is really used for people who importing product data and running the same import process over and over, Suppose If some products import fail and you aren’t sure exactly which ones so you run the product import process again but this process duplicates the product images and It’s very difficult task to find which product has duplicate images.*

**IMPORTANT:** *To remove this type of headache and to save your huge amount of server space we have developed this splendid extension, after installing this extension you have to just run command "php bin/magento duplicate:remove" and it will removed all duplicate product images.*

**IMPORTANT:** *if you like the extension, could you please add a star to this GitHub repository in the top right corner. This is really important for us. Thanks.*

## Installation

### Using Composer (recommended)
1) Go to your Magento root folder
2) Downaload the extension using composer:
```
composer require magegadgets/magento2-product-duplicate-images-remove
```
3) Run setup commands:

```
php bin/magento setup:upgrade;
php bin/magento setup:di:compile;
php bin/magento setup:static-content:deploy -f;
php bin/magento duplicate:remove;
```

### Manually
1) Go to your Magento root folder:

```
cd <magento_root>
```

2) Copy extension files to *app/code/Magegadgets/RemoveDuplicateImage* folder:
```
git clone https://github.com/magegadgets/magento2-product-duplicate-images-remove.git app/code/MagestyApps/WebImages
```
***NOTE:*** *alternatively, you can manually create the folder and copy the extension files there.*

3) Run setup commands:

```
php bin/magento setup:upgrade;
php bin/magento setup:di:compile;
php bin/magento setup:static-content:deploy -f;
php bin/magento duplicate:remove;
## Other Extensions
You can find more useful extensions for Magento 2 by visiting [Magegadgets Official Website](https://www.magegadgets.com/)
```
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "magegadgets/magento2-product-duplicate-images-remove",
"description":"Magento 2 find duplicate product images from your product list and from this list you can easily remove them by running a command",
"keywords": [
"magento 2",
"magento",
"m2",
"magegadgets",
"duplicate product images",
"duplicate product images remove",
"product images remove",
"magento 2 extension",
"magento 2 extension free"
],
"require": {
"php": "~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0|~7.2.0|~7.4.0",
"magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*|103.0.*"
},
"type": "magento2-module",
"version": "2.1.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"authors": [
{
"name": "Magegadgets",
"email": "[email protected]",
"role": "Magento 2 Extensions"
}
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magegadgets\\RemoveDuplicateImage\\": ""
}
}
}
10 changes: 10 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="removeDuplicate" xsi:type="object">Magegadgets\RemoveDuplicateImage\Console\Removeduplicate</item>
</argument>
</arguments>
</type>
</config>
5 changes: 5 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magegadgets_RemoveDuplicateImage" setup_version="1.0.10" schema_version="1.0.10">
</module>
</config>
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magegadgets_RemoveDuplicateImage',
__DIR__
);

0 comments on commit f8d24a9

Please sign in to comment.