News:

Support the VirtueMart project and become a member

Main Menu

cleanup old unlinked media images

Started by ocean1, February 26, 2014, 08:58:42 AM

Previous topic - Next topic

ocean1

Hi,

how can we cleanup delete old unlinked images media in virtuemart

best regards
Ocean

tophatco

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.

ocean1

After a few days I still can't find any solution.

John2400

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.

d0ublezer0

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";
?>


wyder

tested on Virtuemart 4  and Joomla 4
works

Thanks :-)

GJC Web Design

GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation