Zen Cart Logo
Forums / All Other Contributions/Addons / Identifying and Removing Redundant Images

Identifying and Removing Redundant Images

Locked

Views: 5,355

Results 1 to 19 of 19
This thread is locked. New replies are disabled.
1 Aug 2007, 10:05 AM
#1
gsis avatar

gsis

Zen Follower

Join Date:
Jun 2007
Posts:
140
Plugin Contributions:
0

Identifying and Removing Redundant Images

I sell, mainly, used books from my site. Whatever the item is I always upload an image of the actual item. I don't, currently, embed images in descriptions although this may be a possibility in the future. I only use stock images for brand new items.

I'd like to be able to run a housekeeping process to identify and remove images for products that are disabled (I keep the description for the next time I have 'one of those' to sell) or products that have been deleted (there is another one already available or it's most unlikely I'll ever have another one).

In the long term I'm talking about uploading, possibly, 300 to 500 images a month and - if the business grows as I hope - possibly into the 1,000's or even 10,000's. Sales will increase such that I'll need to remove, initially, small numbers (can be done manually) but increasing up to 1,000's per month (must be automated otherwise mistakes will happen).

Clearly I'm going to have to do something about this before it gets out of hand. :blink:

Does anyone know if such a tool already exists?

19 Nov 2007, 9:43 AM
#2
infocom avatar

infocom

Zen Follower

Join Date:
Jul 2006
Location:
Cardiff, Wales
Posts:
308
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

I know this is old but there's no answer and I too am having a problem...

Over the years many, many images have built up on the server that are not used anymore. The site has thousands of products, but has thousands mor images.

How can we remove old, unused images from the server without doing it manually, as it would take months. The sites uses too much disk space.

Thanks

19 Dec 2007, 2:01 AM
#3
purple_circle avatar

purple_circle

New Zenner

Join Date:
Oct 2005
Posts:
28
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

I ended up at this page looking for exactly the same solution. One of my clients adds and removes 20 products per week and has over 1100 products in their Zen Cart system but has over 2000 images on the server.

They don't add images to the description and any other images used throughout the site are in a separate images directory.

If someone could could suggest a MySQL command that can be run on the database to show a list of images currently in use, I'm quite happy to delete all the images not used manually.

19 Dec 2007, 3:10 AM
#4
barco57 avatar

barco57

Totally Zenned

Join Date:
Apr 2006
Location:
West Salem, IL
Posts:
2,845
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

If you use ez-populate to pull the products list it will have a column with the image filename for each product listed.

11 Jan 2008, 6:59 PM
#5
gsis avatar

gsis

Zen Follower

Join Date:
Jun 2007
Posts:
140
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

I now have a housekeeping utility that:

  • reads the images directory
  • identifies and removes orphan images over n days old
  • identifies and reports orphan images less than n days old
  • reports images used by more than one product
  • ignores a list of 'untouchable' files
    It's rough and ready, but works for me on my UNIX server. The date handling will need to be reworked if you're on a Windows server.

No guarantees, but you're welcome to a copy as a starter to meet your own requirements.

UPDATED VERSION OF THE CODE IS POSTED IN THE PLUGINS AREA HERE: http://www.zen-cart.com/downloads.php?do=file&id=1690

The attachment below is the old original code.

11 Jan 2008, 8:04 PM
#6
jammin320 avatar

jammin320

Zen Follower

Join Date:
Mar 2007
Location:
Seattle, WA
Posts:
158
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

I haven't done this, but I have seen a need for it and have given it a small amount of thought. An idea would be to build a PHP script external of Zencart. Make it looks something like...

$sql="SELECT products_image FROM products WHERE products_status = 0";
$sql_result=mysql_query($sql,$connection);
 
while($row=$mysql_fetch_array($sql_result))
{
   unlink("store/images/$row[0]");
}

I haven't actually built this, this is just the rough sketch of an idea I have in my head, but it could be a start to what you need.

11 Jan 2008, 8:40 PM
#7
gsis avatar

gsis

Zen Follower

Join Date:
Jun 2007
Posts:
140
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

jammin320:

I haven't done this, but I have seen a need for it and have given it a small amount of thought. An idea would be to build a PHP script external of Zencart. Make it looks something like...

$sql="SELECT products_image FROM products WHERE products_status = 0";
$sql_result=mysql_query($sql,$connection);

while($row=$mysql_fetch_array($sql_result))
{
unlink("store/images/$row[0]");
}


I admire your enthusiasm, but:

a) It's already written and working, and, 

b) If you follow your own suggestion without thinking it through a *lot *more I do believe you will delete all of your images, if not more.
11 Jan 2008, 8:46 PM
#8
jammin320 avatar

jammin320

Zen Follower

Join Date:
Mar 2007
Location:
Seattle, WA
Posts:
158
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

GSIS:

I admire your enthusiasm, but:

a) It's already written and working, and,

b) If you follow your own suggestion without thinking it through a *lot *more I do believe you will delete all of your images, if not more.

I absolutly agree that it would need work (thus my disclaimer) and your solution would be ideal for many. It is just another thought to add to the post. As written here however, it wouldn't delete all of the images, it runs a greater chance of not deleting any. The premis is solid, I have this basic login included in several tools that link into ZenCart currently and it works well. The advantage of something small like this is that it can be built into a custom page. Yours may as well...I haven't seen it.:mellow:

11 Jan 2008, 9:12 PM
#9
gsis avatar

gsis

Zen Follower

Join Date:
Jun 2007
Posts:
140
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

jammin320:

I absolutly agree that it would need work (thus my disclaimer) and your solution would be ideal for many. It is just another thought to add to the post. As written here however, it wouldn't delete all of the images, it runs a greater chance of not deleting any. The premis is solid, I have this basic login included in several tools that link into ZenCart currently and it works well. The advantage of something small like this is that it can be built into a custom page. Yours may as well...I haven't seen it.:mellow:

I now see the product_status = 0 bit of the sql statement.

Doesn't meet my requirements anyway as I need the image to hang around for a while.

I think mine is best left as a stand-alone utility. I'm looking into running it as a cron job once a week so I don't have to think about it. It would be daft to have a computer and think for yourself. :wink:

13 Jan 2008, 6:17 AM
#10
jammin320 avatar

jammin320

Zen Follower

Join Date:
Mar 2007
Location:
Seattle, WA
Posts:
158
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

GSIS:

It would be daft to have a computer and think for yourself. :wink:

--Well said!:bigups:

26 May 2010, 7:56 PM
#11
donplay avatar

donplay

New Zenner

Join Date:
Jun 2007
Posts:
72
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

Hi GSIS,

I'm running into the same issue -- could I get a copy of that script you created to delete images that have been disabled or deleted? Would really appreciate it...I tried to create one based on the last accessed date in unix but since I backup the entire folder every night the time stamps are off.

thanks,
Don

15 Aug 2013, 5:28 PM
#12
jar3300 avatar

jar3300

New Zenner

Join Date:
Dec 2005
Posts:
62
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

Does anyone know if this will work on 1.3.8a? I'd love to upgrade this site to the most current version, but the wish list module is a must have for my client and it doesn't work in newer versions.
Thanks
Jim

16 Aug 2013, 4:59 AM
#13
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Identifying and Removing Redundant Images

Yes it will.
But you should really be upgrading since your site has security problems when using that very old version.

18 Feb 2014, 4:54 AM
#14
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: Identifying and Removing Redundant Images

ZC 1.5.1

Ran this utility on the images folder - all ok, great script.

My client's site has the product images organized in categories eg images/mens, images/sarongs, images/kaftans etc.

Instead of changing the path to the directory to clean every time, would this work if I set

$imagedirectory = "images/[B]*[/B]";

so it checks and cleans all subdirectories in one cron run?

If not, how could this be implemented?

3 Feb 2015, 10:28 PM
#15
creyesx6 avatar

creyesx6

Inactive

Join Date:
Dec 2010
Location:
U.S. State of Texas
Posts:
76
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

Thank you so much for this plugin. I should have found this years ago. One question, when running the cron job, and viewing the files, I am noticing that the files displayed are the in the images folder. However I would like to clear out the LRG and MED images as well. Can you tell me what I am missing so that I can view the MED and LRG files since they are the real space hoggers on my server?

7 Jun 2015, 8:09 AM
#16
travbacon avatar

travbacon

Zen Follower

Join Date:
Mar 2015
Posts:
169
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

Can we run this without a cron job. Just run it when we need to?

22 Feb 2016, 12:30 AM
#17
markflynn avatar

markflynn

New Zenner

Join Date:
Mar 2011
Posts:
17
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

I have run this as a job several times and get the same list of images. I note the description says it will "optionally" delete the images. How do I do that, or optionally, move them to a different directory where I can move all off the site?

29 Oct 2017, 11:47 AM
#18
steveyork136 avatar

steveyork136

New Zenner

Join Date:
Nov 2005
Posts:
84
Plugin Contributions:
0

Re: Identifying and Removing Redundant Images

Hi, just found this script and I'm finding it useful for my site, but I have noticed the script is reporting some of my extra images (the ones named xx_01.jpg, xx_02.jpg) as being orphaned even if the main image and product is still used.

So I've made a tweak to the code (see below) hope someone finds it useful!
Please note, I've tested it on my site and it works but I've only one site to test on!
It searches for underscore then 2 digits, so shouldn't affect image names containing underscores.

Between line 109 and 110, add the preg_replace, this strips the numbers after the _ in the extra image names.

  
  // strip zen extra file upload underscaore and number
      if (preg_replace('/_[0-9]{2}/', '', $subject)) {
       $subjectNew = preg_replace('/_[0-9]{2}/', '', $subject);
         } else {
          $subjectNew = $subject;
         }

Then replace line 114 (change $subject to $subjectNew) so the query searches for the filename without the _ suffix and retains the $subject var for use further on in script.

      
$result = $db->Execute( "SELECT products_id, products_status, products_image FROM " . TABLE_PRODUCTS . " WHERE products_image = '" . zen_db_input($subjectNew ) ."'" );
23 Jun 2021, 11:55 AM
#19
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,710
Plugin Contributions:
56

Re: Identifying and Removing Redundant Images

Follow ups to https://www.zen-cart.com/showthread.php?228148-Image-Purge-Support-Thread please.

Image Purge has been updated to use the additional images processing from the core to identify (and not report as orphans) additional images.