Jump to content

Delete previous prestashop images


ivanchu83
 Share

Recommended Posts

Well, what I need is a script to delete images from old prestashop, specifically those with the thickbox name that PS 1.7 does not use. I already deleted redundant images with a script that I saw on the forum, but those images are not taken, does anyone know how I could achieve it?

Share this post


Link to post
Share on other sites

Posted (edited)

Is the image id written in the database, in the ps_image table?


Can you give an example of a image name ?

Eg. 1111-thickbox.jpg

 

What is the path to the old image?
Eg. ./img/p/1/1/1/1/1111-thickbox.jpg


Can you link to the script you used here?

Edited by knacky (see edit history)

Share this post


Link to post
Share on other sites

Posted (edited)
include_once( './config/config.inc.php');

$images = Db::getInstance()->executeS('SELECT id_image FROM '._DB_PREFIX_.'image');

foreach ($images as $image){
    $folders = str_split((string)$image['id_image']);
    $image_path = _PS_IMAGE_DIR_.'p/'.implode('/', $folders).'/'.$image['id_image'].'-thickbox.jpg';
/* ./img/p/1/1/1/1/1111-thickbox.jpg */

    if (file_exists($image_path)){
        unlink($image_path);
    }
}

 

Edited by knacky (see edit history)

Share this post


Link to post
Share on other sites

Hello thanks for answering, answering the first post the image has the name of its code I think and its size 11111-thickbox_default.jpg...

I found another way to delete it, regenerating product images and choosing the option all at once, not doing it one by one, that is, first cart defaul, then home_defaul... because that way it doesn't delete them, but choosing products and all and In my case, I deactivated the high-quality images option, it deleted all of them and regenerated only the ones that I have active on my page. Still thank you very much for the script I will try it locally to see how it works.
I paste the redundant image code that I think I got from this forum.
Greetings.

 

<?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']."/mirelojeria/"; // 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 'Aquí habia '.$last_id.' imágenes en la base de datos, pero ahora solo se usan '.$counted_images.' Veamos cuántos de ellos están consumiendo nuestro almacenamiento sin ninguna razón..<br>';

//$limit = 300; // 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 '¡Viva! Hemos eliminado '.$removed_images.' imágenes de productos!';
else
	echo 'Todo está bien con tus imágenes. No eliminé ninguno o lo hice antes. ¡Buen trabajo Presta!';

 

Share this post


Link to post
Share on other sites

Posted (edited)

Hi, I didn't know that regenerating images all at the same time erased those too, since previously I had done it by dimensions and didn't erase them. When by chance I did it yesterday and I realized that today when I saw the post I put it to share what happened.
I didn't need to regenerate images, I needed to save disk space from images that weren't being used and images that were taking up a lot of space. I have made the regeneration to lower the quality and as I said the high quality option and lower from 9 GB to 3.60 GB
Thank you

Edited by ivanchu83 (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

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