VirtueMart Forum

VirtueMart 2 + 3 => Product creation => Topic started by: Shimon Hirschhorn on May 16, 2016, 13:54:43 pm

Title: Product Images - Clean up unused images ?
Post by: Shimon Hirschhorn on May 16, 2016, 13:54:43 pm
Hi,

I have many files in my public_html/images/stories/virtuemart/product/ directory and I am looking for solutions to solve this problem.

1) Is there a utility to clean up (remove or copy elsewhere) all images that are not being used?

2) Is there a way to organize images ?  eg have sub-directories under public_html/images/stories/virtuemart/product/ for each manufacturer ?  this would make each sub-dir much more manageable.

Thanks
Title: Re: Product Images - Clean up unused images ?
Post by: Jörgen on May 16, 2016, 15:02:00 pm
Hello

Yes this is possible. I arrange all my images in that way. The only problem is the upload of images. Using the media tools for Joomla or ftp solves that somehow. The next step is kind of awkward, You can use syncronize media in VM Tools. And then assign the syncronized items to Your products and change the description for the items that are found during syncronization.

I don´t know of any cleaning tool. But as You say this helps making everything more easy to manage.

regards

Jörgen @ Kreativ Fotografi
Title: Re: Product Images - Clean up unused images ?
Post by: Shimon Hirschhorn on May 16, 2016, 15:49:53 pm
Thank you for your reply.

I upload images via FTP, so that is not a problem.
I upload products via CSVI, so I guess that I will have to edit the template each time and set the "Location product files " to the "images/stories/virtuemart/product/manufacture_1" directory.  This is a pain, but not that big a deal.

I do not understand the rest of the procedure.  Is there documentation somewhere that I can read more.  The VM documentation has a paragraph that explains it, but it is still not clear to me.

Thanks
Title: Re: Product Images - Clean up unused images ?
Post by: GJC Web Design on May 16, 2016, 16:38:53 pm
I use the attached script to clean up unused images.. it was from a vm1.1. version I think..

just unzip and upload to the root and call from the browser..  www.xxx.com/vm_img_remove.php  - back up first!!!
use at your own risk

in the file you can target products or cats

Title: Re: Product Images - Clean up unused images ?
Post by: Jörgen on May 17, 2016, 08:29:37 am
Sorry  :-[

This is not really clear I know.

Go into Product images and use search images. Writing for example cap in the search for images box is equal to a search for the unique filename identifier *cap*  (n.b this does not have to be the same as the filename on disk).

You will see up to 16 images, choose the right image or press + for the next 16 images. Then click on the edit image iformation Icon on the image You selected and edit as You wish.

regards

Jörgen @ Kreativ Fotografi
Title: Re: Product Images - Clean up unused images ?
Post by: Shimon Hirschhorn on May 17, 2016, 09:01:05 am
Thanks.

I have thousands of images. This is not something that I want to do manually, image by image.

I will need a way to tell the database where everything is.
Title: Re: Product Images - Clean up unused images ?
Post by: Ghost on May 17, 2016, 09:10:17 am
http://shop.st42.fr/en/products/virtuemart-media-folder-clear.htm

There is this plugin. If you get an error, add this line to vmmedia_clear.php file:

Code: [Select]
jimport('joomla.filesystem.file');
Title: Re: Product Images - Clean up unused images ?
Post by: Shimon Hirschhorn on May 17, 2016, 11:11:12 am
Jorgen, (sorry no ubshlang on my keyboard)

VM has a  section in configuration/templates for Media File Settings where your set the "images/stories/virtuemart/product/"

If I upload the images to the images/stories/virtuemart/product/man1/ directory and use CSVI to import the product and tell it that the image is there, will it know to create the thumbnail in
images/stories/virtuemart/product/man1/resized ? and will VM know to find it there to display it ?

Shimon
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on May 17, 2016, 11:11:47 am
There is this plugin. If you get an error, add this line to vmmedia_clear.php file:
Code: [Select]
jimport('joomla.filesystem.file');
Hi Ghost,
I really appreciate when someone report me a bug ;)
Patrick
Title: Re: Product Images - Clean up unused images ?
Post by: Shimon Hirschhorn on May 17, 2016, 11:15:18 am
Patrick,

What do the two buttons do?

Clear - I assume clears the images that are not used.  I would actually prefer to move them to "recycle" directory
Error - ?? What does Error do ?

Shimon
Title: Re: Product Images - Clean up unused images ?
Post by: Jörgen on May 17, 2016, 11:47:52 am
Hi Shimon

Have seen this extension:
http://shop.ekerner.com/index.php/shop/joomla-extensions/com-vmproductscsv-detail (http://shop.ekerner.com/index.php/shop/joomla-extensions/com-vmproductscsv-detail)

I seems to be able to add images and make the media items neccessary if they are uploaded to You site. Other URL's than the standard Products can be used and You don´t have to syncronize medias through Virtuemart

regards

Jörgen @ Kreativ Fotografi
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on May 17, 2016, 11:52:26 am
The problem is that i use Joomla core function and this are in some case not loaded. So i need to add it.
1 button remove images : difference from images in folder and DB (unused files).
1 button remove DB entries : inexisting files.
If you need a "recycle" directory, best is to do a backup before using the tool(or zip all your images). The tool is only here to clean all bugs in db and all unused files.
Regards,
Patrick
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on May 17, 2016, 12:11:31 pm
Updated file for http://shop.st42.fr/en/products/virtuemart-media-folder-clear.htm
So now Joomla library is not missing.
Title: Re: Product Images - Clean up unused images ?
Post by: Shimon Hirschhorn on May 17, 2016, 13:01:07 pm
Patrick,

I like the first part : Getting rid of files that are cluttering up the product images directory.

As far as deleting non-existent images from the DB, I don't like this.  I would rather have a list of missing images ( and the products that they belong to) so that I can find them and upload them.

Shimon
Title: Re: Product Images - Clean up unused images ?
Post by: PRO on May 17, 2016, 13:34:14 pm
I wrote this to find images not assigned to products


<?php function fullmedia(){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__virtuemart_medias'));
$db->setQuery($query);
$rows=$db->loadAssocList();
$html='';
foreach ($rows as $row){
$mid=$row['virtuemart_media_id'];
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__virtuemart_product_medias'));
$query->where($db->quoteName('virtuemart_media_id')." = ".$db->quote($mid));
$db->setQuery($query);
$row2 =$db->loadAssocList();
if (empty($row2)){
$html.=$mid.'<br/>';
}
}
return $html;


}
?>
<?php echo fullmedia();?>
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on May 17, 2016, 13:42:08 pm
Hi shimon,

The tool is GNU/GPL, so you can modify it for your needs.
Remember to send back your changes ;)

Patrick
Title: Re: Product Images - Clean up unused images ?
Post by: kalestud on June 11, 2016, 13:25:54 pm
The plugin from Studio42 really works wonders!
Two additions to clear things up about this plugin:

1 After installing the plugin, as usual, go to your plugins menu and search for 'vmmedia'. Enable the vmmedia_clear plugin to allow itself to show in the Virtuemart Media Files section.

2 Go to Virtuemart > Media Files and first apply a filter to the shown results from the pulldown (e.g. Product, Product Category, Manufacturer or Vendor). Then click the clear button to remove obsolete files or database entries.

That description would help ;-)
Title: Re: Product Images - Clean up unused images ?
Post by: Henrik Holm Nielsen on September 13, 2016, 10:22:24 am
Hi,

I tried installing Studio 42's plugin, and have enabled it - but the buttons never show?
As mention here, I have also tried changing the filter to Products or Product Categories, but still no buttons?

Any suggestions?

We use VM 3.0.16 and J3.6.2 on PHP 7.0.9 (and now also just tried PHP 5.6.24 as a test - same result).

Cheers
Title: Re: Product Images - Clean up unused images ?
Post by: GJC Web Design on September 13, 2016, 12:29:53 pm
Hmm .. just tried and can confirm I can't get the buttons to show either

It used to work not sure what changed

VM3.0.16/J3.6.2/php7
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on September 13, 2016, 13:08:19 pm
Hi henrik,
I tested on a fresh install :
VM 3.0.14 and joomla 3.5 : OK
VM 3.0.16 and joomla 3.5 : OK
VM 3.0.16 and joomla 3.6 : OK
VM 3.0.16 and joomla 3.6.2 : OK
The direct link to see the 2 buttons is YOURSITE/administrator/index.php?option=com_virtuemart&view=media
If you need a better tool, i have now added the feature in Vm Be Pro http://shop.st42.fr/en/products/vm-be-pro-batch-product-edit.htm and delete only selected files.
Regards,
Patrick
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on September 13, 2016, 13:10:55 pm
Hmm .. just tried and can confirm I can't get the buttons to show either

It used to work not sure what changed

VM3.0.16/J3.6.2/php7
I don't tested php7. But the code is not so complex to have php dependencies
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on September 13, 2016, 13:14:51 pm
I found a potential way to not see the buttons.
if(!$user->get('isRoot') ) return; check for administrator, so if you are not super user in Joomla, you don't see the buttons.
Title: Re: Product Images - Clean up unused images ?
Post by: GJC Web Design on September 13, 2016, 13:31:16 pm
Hi Patrick,

yes.. with

#if(!$user->get('isRoot') ) return;

they show -- but am definitely a superuser although not shop owner or vendor

logged in as shop owner / super admin they still don't show with the above code in place  :(

apparently broken in 3.6.2

https://groups.google.com/forum/#!topic/joomla-dev-cms/A5GeOPcM86M



Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on September 13, 2016, 13:57:15 pm
Sometime, you have stupid Joomla coder. Same problem as mail bug, minor release and remove statut of a value used since Joomla 1.5 that was universal !
I use this in some other case, so now i have to modify other codes too for a so little unneeded change.
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on September 13, 2016, 14:21:46 pm
File updated : see http://shop.st42.fr/en/products/virtuemart-media-folder-clear.htm and donwload Now ;)
Title: Re: Product Images - Clean up unused images ?
Post by: GJC Web Design on September 13, 2016, 15:15:13 pm
Quote
Sometime, you have stupid Joomla coder. Same problem as mail bug, minor release and remove statut of a value used since Joomla 1.5 that was universal !

Can only agree... I feel your pain...  :)
Title: Re: Product Images - Clean up unused images ?
Post by: razor7 on September 15, 2016, 13:59:05 pm
Hi! the plugin thingy did he job!
Title: Re: Product Images - Clean up unused images ?
Post by: razor7 on September 15, 2016, 14:04:58 pm
Hi! Is it possible to add a new function to remove unused media entries (and files) that are not assigned to any product or cat or anything?
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on September 15, 2016, 16:19:31 pm
Hi! Is it possible to add a new function to remove unused media entries (and files) that are not assigned to any product or cat or anything?
You have 2 button, one remove the entries, one remove the files

Else to search and check files before and select what you want to remove
Quote
If you need a better tool, i have now added the feature in Vm Be Pro http://shop.st42.fr/en/products/vm-be-pro-batch-product-edit.htm and delete only selected files...
Regards,
Patrick
Title: Re: Product Images - Clean up unused images ?
Post by: Milbo on September 29, 2016, 15:49:45 pm
I found a potential way to not see the buttons.
if(!$user->get('isRoot') ) return; check for administrator, so if you are not super user in Joomla, you don't see the buttons.

Please use instead if(!vmAccess::manager('media.delete')){

It checks directly if the user is superadmin or has the rights to delete media. Additionally your plugin will also work in WP. Greets
Title: Re: Product Images - Clean up unused images ?
Post by: manwlios on October 21, 2016, 14:19:42 pm
Studio 42 thank you very much fot this usefull plugin!
Title: Re: Product Images - Clean up unused images ?
Post by: stankoz on June 26, 2017, 13:21:34 pm
Hi, I have problem with Studio42 Virtuemart media clear plugin. After click on button Clear I receive message:

Warning
Removed 0 file(s).
11272 file(s) found in DB


After click on button Error I receive message:

Warning
Removed 0 Records(s).
19733 record(s) found with file(s)

What is wrong?
I have 19733 files, but I can't delete unused pictures (after updates many product I have many pictures unused).
Can somebody help me please
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on June 26, 2017, 13:30:36 pm
Hi, it only remove DB errors.
1st button remove media that are not in DB
2nd button remove Db entry that have no file image.

If you need more advanced feature (not only removing images) you can buy my other tool : https://shop.st42.fr/en/products/vm-be-pro-batch-product-edit.htm
YOu have a filter "unused images" and you can select all or each file you need to remove.
Title: Re: Product Images - Clean up unused images ?
Post by: MKI-Miro on September 10, 2017, 13:13:12 pm
Updated file for http://shop.st42.fr/en/products/virtuemart-media-folder-clear.htm
So now Joomla library is not missing.

we nice plugin but resized images are not cleaned, is this functionality implemented or do i something wrong?
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on September 10, 2017, 13:48:13 pm

we nice plugin but resized images are not cleaned, is this functionality implemented or do i something wrong?

resized images are not cleaned.
Only bad entries. Missing files but entry exist in DB or unused files
Title: Re: Product Images - Clean up unused images ?
Post by: PRO on September 11, 2017, 19:33:59 pm
Updated file for http://shop.st42.fr/en/products/virtuemart-media-folder-clear.htm
So now Joomla library is not missing.

we nice plugin but resized images are not cleaned, is this functionality implemented or do i something wrong?

you can delete all thumbs and resize easily.
Title: Re: Product Images - Clean up unused images ?
Post by: razor7 on October 13, 2017, 22:15:21 pm
Hi! This snippet gives you a list of unused product media entries. Just paste werever you like in your joomla project, just remember to remove it later.

Code: [Select]
echo '<h2>Unused Product Media Entries</h2>';

    $db = JFactory::getDbo();

    $query = $db->getQuery( true );
    $query->select( 'virtuemart_media_id' );
    $query->from( $db->quoteName( '#__virtuemart_product_medias' ) );
    $db->setQuery( $query );
    $resultArray = $db->loadAssocList();
    $ids = array();

    foreach ( $resultArray as $row )
    {
        $ids[] = $row['virtuemart_media_id'];
    }

    $query = $db->getQuery( true );
    $query->select( '*' );
    $query->from( $db->quoteName( '#__virtuemart_medias' ) );
    $query->where( $db->quoteName( 'virtuemart_media_id' ) . " NOT IN ( " . implode(",", $ids) . ")" );
    $query->andWhere($db->quoteName( 'file_type' ) . " = " . $db->quote( 'product' ));
    $db->setQuery( $query );
    $resultArray = $db->loadAssocList();

    if (!empty($resultArray)) { ?>
        <table>
            <thead>
                <tr>
                    <th>virtuemart_media_id</th>
                    <th>file_url</th>
                    <th>file_type</th>
                </tr>
            </thead>
            <tbody>
            <?php
            
foreach ( $resultArray as $row )
            { 
?>

                <tr>
                    <td><?php echo $row['virtuemart_media_id']; ?></td>
                    <td><?php echo $row['file_url']; ?></td>
                    <td><?php echo $row['file_type']; ?></td>
                </tr>
                <?php
            
}
            
?>

            </tbody>
        </table>
    <?php
    
} else {
        echo 
'No unused medias in products!';
    }
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on October 13, 2017, 23:41:44 pm
Sorry razor7, but your script is really poor and don't remove or check any files and NOT IN() is limited depending the server.
For 10000 medias, your script fail and return a Db error in most case.
Title: Re: Product Images - Clean up unused images ?
Post by: razor7 on October 14, 2017, 19:55:50 pm
Sorry razor7, but your script is really poor and don't remove or check any files and NOT IN() is limited depending the server.
For 10000 medias, your script fail and return a Db error in most case.

Hi! I stated clearly this snippet gives you a list of unused product media entries. It's not the holy grial of PHP code, just my two cents to find unused product media files. If you feel it's poor, then you may get it better and post the code here.

EDIT: Forgot to mention that with this script I was able to delete 1800+ unused product media entries in DB (using the ID's given by the script), then with Studio 42 plugin I was able to phisically remove the files (because there was no DB entry in the medias table for those files).
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on October 14, 2017, 21:27:36 pm
This raw query should displayed all unused medias.

Code: [Select]
$query = 'SELECT m.* FROM #__virtuemart_medias as m LEFT JOIN  `#__virtuemart_product_medias` pm  on m.virtuemart_media_id = pm.virtuemart_media_id where pm.virtuemart_media_id IS NULL AND file_type="product" ';
$db->setQuery( $query );
$resultArray = $db->loadAssocList();

Only one query and unlimited results ;)
Title: Re: Product Images - Clean up unused images ?
Post by: razor7 on October 17, 2017, 02:12:55 am
Great! you see...it could be always better!
Title: Re: Product Images - Clean up unused images ?
Post by: AzMandius on May 01, 2019, 20:54:18 pm
Hi all,
Sorry for rising a necro-thread.
Just wanted to know if someone tested Studio 42's plugin (https://shop.st42.fr/en/products/virtuemart-media-folder-clear.htm) with Joomla 2.5.xx?
Thanks
Title: Re: Product Images - Clean up unused images ?
Post by: Studio 42 on May 02, 2019, 21:20:32 pm
It should work with Joomla 2.5
Title: Re: Product Images - Clean up unused images ?
Post by: AzMandius on May 03, 2019, 14:51:24 pm
Hooray! I confirm, it works on Joomla! 2.5.28 with VirtueMart 3.0.4! I was able to safely remove all orphan images and database entries.
Thank you Studio 42!