VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product creation => Topic started by: ocean1 on February 26, 2014, 08:58:42 AM

Title: cleanup old unlinked media images
Post by: ocean1 on February 26, 2014, 08:58:42 AM
Hi,

how can we cleanup delete old unlinked images media in virtuemart

best regards
Ocean
Title: Re: cleanup old unlinked media images
Post by: tophatco on May 20, 2014, 20:41:43 PM
I have been looking for an answer to this for a while. A client decided to change his mind about which images to use... a few times... on a few hundred products.  Now everything is a mess.
Title: Re: cleanup old unlinked media images
Post by: ocean1 on September 25, 2014, 22:01:28 PM
After a few days I still can't find any solution.
Title: Re: cleanup old unlinked media images
Post by: John2400 on October 12, 2014, 05:12:53 AM
Ok. In reality you would like to see an option that says what photos or images are currently being used in the store and what images are not being used and then maybe delete the rest.
I can see in a large store that would be a real pain.
In a small store most would use FTP to download all the media files both original and thumbnails and then go trough the list of products . Then syncronise them back saying delete those not on file.
In VM under tools there is a syncronise option for media. This will make sure that the product are all linked correctly.
If you upload and see an image missing then syncronise again.


Now there are thrird party extensions that you can use to upload large volumes of products .. This could be an option .. You need to investigate this.option.


I did do a similar option once where all products were alway placed into a category call all products. I hid this category from the public but it allowed me to turn it on and look at all the products quickly. I could see what products has missing images and what products may have changed. I know now this might take a little time but for future reference it helps with similar situations.
Title: Re: cleanup old unlinked media images
Post by: d0ublezer0 on October 06, 2015, 20:17:03 PM
Try this solution:
<?php 
$type 
'product'// type of image: "product" or "category"
$imgPath $_SERVER['DOCUMENT_ROOT']."/images/stories/virtuemart/$type/"// default path to full-sized images
$resizedPath $_SERVER['DOCUMENT_ROOT']."/images/stories/virtuemart/$type/resized/"// default path to thumbnail images
set_time_limit(0); 
require 
$_SERVER['DOCUMENT_ROOT'].'/configuration.php'
$conf = new JConfig
$db = new mysqli($conf->host$conf->user$conf->password$conf->db);

$images array_slice(scandir($imgPath), 2);
$resized array_slice(scandir($resizedPath), 2);

$query="SELECT 
virtuemart_media_id, 
file_url, 
file_url_thumb 
FROM 
{$conf->dbprefix}virtuemart_medias
WHERE virtuemart_media_id IN(SELECT DISTINCT virtuemart_media_id FROM 
{$conf->dbprefix}virtuemart_{$type}_medias)";

$res $db->query($query);

if (!
$res) die("Cannot get database resource!");

while(
$r $res->fetch_array()){
    
$activeID[] = $r[0];

    
$p pathinfo($r[1]);
    
$activeImg[] = $p['basename'];

    
$p pathinfo($r[2]);
    
$activeResized[] = $p['basename'];
}       
if (!
is_null($activeImg)){
    
$notActiveImg array_diff($images$activeImg);   
};
if (!
is_null($activeResized)){
    
$notActiveResized array_diff($resized$activeResized);
}

if (!
is_null($activeID)){
    
$db->query("DELETE FROM {$conf->dbprefix}virtuemart_medias WHERE file_type='$type' AND virtuemart_media_id NOT IN(".implode(','$activeID).")");
}
$db->close();


$full=0;
$thumb=0;
if (!
is_null($activeImg)){

    foreach(
$notActiveImg as $i){
        if(
is_file($imgPath.$i) && $i != 'index.html'){
            if (
unlink($imgPath.$i)) $full++; else echo "Cannot delete $imgPath$i<br>";
        } else 
"File not exists $imgPath.$i<br>";
    }
}
if (!
is_null($activeResized)){

    foreach(
$notActiveResized as $i){
        if(
is_file($resizedPath.$i)  && $i != 'index.html'){
            if (
unlink($resizedPath.$i)) $thumb++; else echo "Cannot delete $imgPath$i<br>";
        } else 
"File not exists $imgPath.$i<br>";
    }
}
if (
$full>0) echo "Deleted $full full-sized images<br>";
if (
$thumb>0) echo "Deleted $thumb thumbnail images<br>";
if (
$full==&& $thumb==0) echo "Nothing to delete";
?>
Title: Re: cleanup old unlinked media images
Post by: ocean1 on November 18, 2015, 21:04:22 PM
Quote from: d0ublezer0 on October 06, 2015, 20:17:03 PM
Try this solution:


Thanks it works perfect for me
Title: Re: cleanup old unlinked media images
Post by: wyder on July 04, 2023, 15:31:01 PM
tested on Virtuemart 4  and Joomla 4
works

Thanks :-)
Title: Re: cleanup old unlinked media images
Post by: GJC Web Design on July 05, 2023, 10:14:24 AM
this freebie also works well

https://shop.st42.fr/en/products/virtuemart-media-folder-clear.htm