Jump to content
Syns

Removing unused images

Recommended Posts

hello.

 

is there any way to remove unused images from Prestashop? maybe a module?

I have around 30k images but only less than 10k used.

 

thank's

Share this post


Link to post
Share on other sites

I have the same need, the server space is running out and i would like to delete unused images.

 

There is a script here but i miss the skill to use it, can anyone help?

 

Thanks

navid

Share this post


Link to post
Share on other sites

Thank's for the link, I've tried it. just create a php file on your root, then copy the code on to it (I'm using the last one from mkbond777)

and change the $shop_root url and $limit on the code. and then call it directly from your browser "yoursite.com/yourfile.php".

 

the code is working, but I still don't understand what and how it does actually?!

Share this post


Link to post
Share on other sites

Can you confirm that it is working?? 

 

I am not a coder, i inserted my config but get a Parse error: syntax error, unexpected ':' on line 9

 

Would you help me to fix it?

 

Can I send you a private message with my php file to look at?

 

Thanks

Navid 

Share this post


Link to post
Share on other sites

it says:

count images database: 14833

...

result: files: 22954 checked: 922 not_found: 240 found: 682

 

sure just send me the php.

Share this post


Link to post
Share on other sites

Hi Syns,

 

in the php script, i used 'mysql:host='95.142.159.11' and get the following error:

Parse error: syntax error, unexpected '95.142' (T_DNUMBER) in

 

what i am doing wrong  :( 

Navid

Share this post


Link to post
Share on other sites

Ok, that is it! I was so angry that I can't find any working solution for this so I created my own and it is working. This is the code which You can place in Your file or just download the attached file and put it into main folder of Your shop and run http://yourshop.pl/remove_unused_images.php in Your browser.

Things to consider:

  • Change $limit value from 19 line when testing, for example like 100, for production mode use $limit = $counted_images;
  • if scan return 0 or there is an error just check if $scan_dir is ok by adding ddd($scan_dir); arround 11 line of code. Upload script again, test it if scan directory is properly created, if not then change $shop_root and $image_folder values from the beginning of the script, remove ddd part, reupload and run script again.
  • It might take a long time so if You will run out of time then just run the script again.
  • Feel free to not ask about anything because I won't support this code, I am not responsible for it and I won't help You. 
  • Likes for this post will be appreciated because there are some paid solutions, modules that are doing basically the same thing. This one is free.
  • This will take some time to process - if You have large ammount of files it can exceed your max_execution_time limit on Your hosting. If this will happen You can try to take parts like 5000 or 10 000 and edit for ($i=1 to start with for ($i=5000 and then restart the script again and again with changing the limit 
     
<?php
####PUT THIS FILE INTO YOUR MAIN SHOP FOLDER####

// root path of the shop, almost no one needs to change something here.
$shop_root = $_SERVER['DOCUMENT_ROOT']."/"; // need to have slash / at the end
$image_folder = 'img/p/'; // also needs slash at the ennd
$scan_dir = $shop_root.$image_folder;

include_once($shop_root.'config/config.inc.php');
include $shop_root . 'config/settings.inc.php';

#---------------------------------------------#
$last_id = (int)Db::getInstance()->getValue('
	SELECT id_image FROM '._DB_PREFIX_.'image ORDER BY id_image DESC
');

$counted_images = Db::getInstance()->executeS('
	SELECT count(*) as qnt FROM '._DB_PREFIX_.'image
');
$counted_images = (int)$counted_images[0]['qnt'];


echo 'There was '.$last_id.' images in database but only '.$counted_images.' is used right now. Lets check how many of them are eating up our storage without no reason.<br>';

//$limit = 150; // for testing
$limit = $last_id; // for production

$removed_images = 0;

for ($i=1; $i <= $limit; $i++) {
	if (!imageExistsInDB($i)){
		$imageDir = str_split($i);
		$imageDir = implode('/', $imageDir);
		$path = $scan_dir.$imageDir;
		deleteImagesFromPath($path);
	}
}

function deleteImagesFromPath($path) {
	global $removed_images;
	$images = glob($path . '/*.{jpg,png,gif,jpeg}', GLOB_BRACE);
	if ($images){
		foreach ($images as $file) {
			if (is_file($file)) {
				unlink($file);
			}
		}
		$removed_images++;
		echo 'Deleted images from folder ' . $path . '/' ."<br/>";
	}
}

function imageExistsInDB($id_image){
	return Db::getInstance()->getValue('
	    SELECT id_image FROM '._DB_PREFIX_.'image WHERE id_image = '.(int)$id_image
	);
}

echo '--------------------------------------<br>';
if  ($removed_images > 0)
	echo 'Hurray! We removed '.$removed_images.' product images!';
else
	echo 'Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta!';


 

 

remove_unused_images.php

Edited by hakeryk2 (see edit history)
  • Thanks 2
  • Haha 1

Share this post


Link to post
Share on other sites

It didn't failed when I was removing from database when 60 000 images were found. It will fail only if someone have some limits on their server but even that they can change first $i=

for ($i=1; $i <= $limit; $i++) {

to $i=WHATEVER_THE_NUMBER_THAT_SCRIPT_FAILED

... but from my testing it was quite fast for fastly written script. Even if script failed then it will just check some mysql and directories without deleting previously deleted images so it will go quicker on the second, third attempt.

 

Edited by hakeryk2 (see edit history)

Share this post


Link to post
Share on other sites
On 2/28/2019 at 10:19 AM, hakeryk2 said:

Ok, that is it! I was so angry that I can't find any working solution for this so I created my own and it is working. This is the code which You can place in Your file or just download the attached file and put it into main folder of Your shop and run http://yourshop.pl/remove_unused_images.php in Your browser.

Things to consider:

  • Change $limit value from 19 line when testing, for example like 100, for production mode use $limit = $counted_images;
  • if scan return 0 or there is an error just check if $scan_dir is ok by adding ddd($scan_dir); arround 11 line of code. Upload script again, test it if scan directory is properly created, if not then change $shop_root and $image_folder values from the beginning of the script, remove ddd part, reupload and run script again.
  • It might take a long time so if You will run out of time then just run the script again.
  • Feel free to not ask about anything because I won't support this code, I am not responsible for it and I won't help You. 
  • Likes for this post will be appreciated because there are some paid solutions, modules that are doing basically the same thing. This one is free.
  • This will take some time to process - if You have large ammount of files it can exceed your max_execution_time limit on Your hosting. If this will happen You can try to take parts like 5000 or 10 000 and edit for ($i=1 to start with for ($i=5000 and then restart the script again and again with changing the limit 
     

<?php
####PUT THIS FILE INTO YOUR MAIN SHOP FOLDER####

// root path of the shop, almost no one needs to change something here.
$shop_root = $_SERVER['DOCUMENT_ROOT']."/"; // need to have slash / at the end
$image_folder = 'img/p/'; // also needs slash at the ennd
$scan_dir = $shop_root.$image_folder;

include_once($shop_root.'config/config.inc.php');
include $shop_root . 'config/settings.inc.php';

#---------------------------------------------#
$last_id = (int)Db::getInstance()->getValue('
	SELECT id_image FROM '._DB_PREFIX_.'image ORDER BY id_image DESC
');

$counted_images = Db::getInstance()->executeS('
	SELECT count(*) as qnt FROM '._DB_PREFIX_.'image
');
$counted_images = (int)$counted_images[0]['qnt'];


echo 'There was '.$last_id.' images in database but only '.$counted_images.' is used right now. Lets check how many of them are eating up our storage without no reason.<br>';

//$limit = 150; // for testing
$limit = $last_id; // for production

$removed_images = 0;

for ($i=1; $i <= $limit; $i++) {
	if (!imageExistsInDB($i)){
		$imageDir = str_split($i);
		$imageDir = implode('/', $imageDir);
		$path = $scan_dir.$imageDir;
		deleteImagesFromPath($path);
	}
}

function deleteImagesFromPath($path) {
	global $removed_images;
	$images = glob($path . '/*.{jpg,png,gif,jpeg}', GLOB_BRACE);
	if ($images){
		foreach ($images as $file) {
			if (is_file($file)) {
				unlink($file);
			}
		}
		$removed_images++;
		echo 'Deleted images from folder ' . $path . '/' ."<br/>";
	}
}

function imageExistsInDB($id_image){
	return Db::getInstance()->getValue('
	    SELECT id_image FROM '._DB_PREFIX_.'image WHERE id_image = '.(int)$id_image
	);
}

echo '--------------------------------------<br>';
if  ($removed_images > 0)
	echo 'Hurray! We removed '.$removed_images.' product images!';
else
	echo 'Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta!';


 

 

remove_unused_images.php

Amazing, thanks!

  • Thanks 1

Share this post


Link to post
Share on other sites
Posted (edited)

Done but I've got this message (PRESTASHOP 1.6.1.17) :

 

Notice: Constant _DB_SERVER_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 2

Notice: Constant _DB_NAME_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 3

Notice: Constant _DB_USER_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 4

Notice: Constant _DB_PASSWD_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 5

Notice: Constant _DB_PREFIX_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 6

Notice: Constant _MYSQL_ENGINE_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 7

Notice: Constant _PS_CACHING_SYSTEM_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 8

Notice: Constant _PS_CACHE_ENABLED_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 9

Notice: Constant _COOKIE_KEY_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 10

Notice: Constant _COOKIE_IV_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 11

Notice: Constant _PS_CREATION_DATE_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 12

Notice: Constant _RIJNDAEL_KEY_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 15

Notice: Constant _RIJNDAEL_IV_ already defined in /homepages/24/d742654074/htdocs/config/settings.inc.php on line 16
There was 34998 images in database but only 6101 is used right now. Lets check how many of them are eating up our storage without no reason.
--------------------------------------
Everything is ok with Your images. I did not removed any of them or I made it before. Good Job Presta!

Edited by xarnoux
précision de la version prestashop utilisée (see edit history)

Share this post


Link to post
Share on other sites

Hi!

In case someoune is still looking for an easy solution, I just added a feature to delete all unused images from the img/p folder to the Tidy module.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More