Author Topic: cleanup old unlinked media images  (Read 3402 times)

ocean1

  • Guest
cleanup old unlinked media images
« on: February 26, 2014, 08:58:42 AM »
Hi,

how can we cleanup delete old unlinked images media in virtuemart

best regards
Ocean

tophatco

  • Beginner
  • *
  • Posts: 46
Re: cleanup old unlinked media images
« Reply #1 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.

ocean1

  • Guest
Re: cleanup old unlinked media images
« Reply #2 on: September 25, 2014, 22:01:28 PM »
After a few days I still can't find any solution.

John2400

  • Virtuemart - the Future is ever Changing
  • Global Moderator
  • Full Member
  • *
  • Posts: 1568
  • VirtueMart Version: Use VM3.6+
Re: cleanup old unlinked media images
« Reply #3 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.

d0ublezer0

  • Jr. Member
  • **
  • Posts: 215
Re: cleanup old unlinked media images
« Reply #4 on: October 06, 2015, 20:17:03 PM »
Try this solution:
Code: [Select]
<?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";
?>

ocean1

  • Guest
Re: cleanup old unlinked media images
« Reply #5 on: November 18, 2015, 21:04:22 PM »
Try this solution:


Thanks it works perfect for me

wyder

  • Beginner
  • *
  • Posts: 2
Re: cleanup old unlinked media images
« Reply #6 on: July 04, 2023, 15:31:01 PM »
tested on Virtuemart 4  and Joomla 4
works

Thanks :-)

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 10880
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.8.8
Re: cleanup old unlinked media images
« Reply #7 on: July 05, 2023, 10:14:24 AM »
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 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