VirtueMart Forum

VirtueMart 1.1.x [ Old version - no longer supported ] => Frontend Modules VM 1.1 => Topic started by: georgian on July 14, 2010, 11:48:28 am

Title: delete unused images script
Post by: georgian on July 14, 2010, 11:48:28 am
To delete unused images in /components/com_virtuemart/shop_image/product  /resized works well. Remain the only images used.
If anyone knows how to implement like an administrative utility script ...
Code: [Select]
<?php
// The files must have 0777 chmod permissions aswell as the directory that the file is stored in.

// the directory, where your images are stored

$imgdir IMAGEPATH."product/";
// list of filetypes you
$allowed_types = array('png','jpg','jpeg','gif'); 

$dimg opendir($imgdir);

while(
$imgfile readdir($dimg))
{
if(
in_array(strtolower(substr($imgfile,-3)),$allowed_types))
{
$db = new ps_DB;

$q "SELECT product_full_image FROM #__{vm}_product WHERE product_full_image='$imgfile'";

$db->query$q );
if ($db->num_rows()==0) {
echo $imgdir.$imgfile." </br>";
unlink($imgdir.$imgfile);
};
}
}
?>
Title: Re: delete unused images script
Post by: KooTjoo on December 16, 2010, 11:23:54 am
Hi,

I am also very interested in using this script. I can't get it working however. Did you manage to implement it in the admin area?

Thanks,
KooTjoo
Title: Re: delete unused images script
Post by: georgian on December 20, 2010, 10:42:18 am
I insert this code into browse page (for testing).
I do not know how to insert in the administration page. Maybe others ...
Title: Re: delete unused images script
Post by: positronio on May 22, 2011, 16:03:45 pm
i have applied the above php script and it deletes unused product images but unfortunately it also deletes the products additional images. what shall i do to exclude from deleting additional images;
Title: Re: delete unused images script
Post by: Gruz on December 26, 2011, 18:22:49 pm
I've improved the solution. Not it doesn't remove additional images.

http://gruz.org.ua/en/about-joomla/34-virtuemart/27-vydalennja-zobrazhen-shcho-ne-vykorystovujutsja-z-virtuemart-11x.html (http://gruz.org.ua/en/about-joomla/34-virtuemart/27-vydalennja-zobrazhen-shcho-ne-vykorystovujutsja-z-virtuemart-11x.html)
Title: Re: delete unused images script
Post by: sirius on January 21, 2012, 02:04:34 am
I've improved the solution. Not it doesn't remove additional images.

http://gruz.org.ua/en/about-joomla/34-virtuemart/27-vydalennja-zobrazhen-shcho-ne-vykorystovujutsja-z-virtuemart-11x.html (http://gruz.org.ua/en/about-joomla/34-virtuemart/27-vydalennja-zobrazhen-shcho-ne-vykorystovujutsja-z-virtuemart-11x.html)

Hi Gruz,

I've tried your little script, and it works ok, excepted that I confirm the remove of additional images.

Despite of the loop for 'jos_vm_product_files'.....

I will look more for this issue

EDIT:
ok, I think you just forgot one little thing here:
Code: [Select]
                $q = "SELECT * #__{vm}_product_files WHERE `file_name`='".$reldir.$imgfile."'";
                if ($db->num_rows()>0) {

to launch the query:
Code: [Select]
                $q = "SELECT * #__{vm}_product_files WHERE `file_name`='".$reldir.$imgfile."'";
                $db->query( $q );
                if ($db->num_rows()>0) {

But a better way could be:
Code: [Select]
            else {
                $namefield = 'product_thumb_image';
                $q = "SELECT ".$namefield." FROM #__{vm}_product WHERE ".$namefield."='".$imgfile."' OR ".$namefield."='resized/".$imgfile."'";
                $db->query( $q );
                if ($db->num_rows()==0) {
                    $remove = true;
                }
                $q = "SELECT * #__{vm}_product_files WHERE `file_name`='".$reldir.$imgfile."'";
                $db->query( $q );
                if ($db->num_rows()>0) {
                    $remove = false;
                }

Because the additional images also are sometimes resized too but sources for them are not in the "/resized" folder as they use the "show_image_in_imgtag.php"
Title: Re: delete unused images script
Post by: Gruz on January 21, 2012, 10:20:55 am
Thanks, Sirius. I've updated my article here:
http://gruz.org.ua/en/about-joomla/34-virtuemart/27-vydalennja-zobrazhen-shcho-ne-vykorystovujutsja-z-virtuemart-11x.html (http://gruz.org.ua/en/about-joomla/34-virtuemart/27-vydalennja-zobrazhen-shcho-ne-vykorystovujutsja-z-virtuemart-11x.html)

Some other fixes also.
Title: Re: delete unused images script
Post by: sirius on January 22, 2012, 17:53:48 pm
Your welcome,

Far better now, but not enough to keep the aditional images.

That's because you don't check the thumbnails correctly for the additional images.

As I said in my previous post they use the "show_image_in_imgtag.php", so the script will never find them in the resized folder, and so the &remove still say true.

You need to also, in complement of the query to select at least the "file_image_thumb_height" and "file_image_thumb_width" and happen them to the source image name.

Example:
I got additional image called "nextimagecd.jpg"
I've resized it to 74 x 90 so now, stored in the resized/ folder with the name nextimagecd_74_90.jpg
There is nothing stored for nextimagecd_74_90.jpg in the db, only dynamic stuff here.
$remove will return true.

I think that we need a regex with a preg_match for this on the image.
If boolean is 1 , then explode to retrieve the "file_image_thumb_height" and "file_image_thumb_width" to consolidate the query.



Title: Re: delete unused images script
Post by: Gruz on January 22, 2012, 17:58:45 pm
If it is not stored in the DB, then it's generated automatically via the link. So I think there is no need to take care of it, as it will be regenerated automatically. Or am I wrong?
Title: Re: delete unused images script
Post by: sirius on January 22, 2012, 18:12:27 pm
Unfortunately not....

By default show_image_in_imgtag.php will roll back to the store default parameter.
So for my previous example, my additional image stay in 200x200.

That's because the thumbnail with the good size (74x90 in the example) is generated and stored at the upload time of the image.

show_image_in_imgtag.php just care about "file_image_thumb_height" and "file_image_thumb_width" to find the image in the folder, not to generate it on the fly.
Title: Re: delete unused images script
Post by: Gruz on January 22, 2012, 18:18:40 pm
Well, if you think it's worth of effors, then fix it please. I'll include the fixed code into the article at my web-site. Thanks.
Title: Re: delete unused images script
Post by: sirius on January 22, 2012, 18:22:54 pm
will try to do that ok
Title: Re: delete unused images script
Post by: pierregulpen on July 20, 2012, 20:46:08 pm
I found this script and used it on two of my sites. worked great on one site where I use a non changed last version of VM. Did not work on my other site, where I think it could be because i do not use there the standard layout of VM ... I think... still searching.

Is it possible that this script only works with a fully original VM ?

Anyhow, I got as a message :
Quote
Cheking folder /home/users/xxxx/xxxxxxxxxxxxxxx.nl/components/com_virtuemart/shop_image/product/ ==>Carne_de_Membril_4e982ad978644.jpg
Fatal error: Class 'ps_DB' not found in /home/users/xxxxxxxxx/xxxxxxxxxxxxxxx/templates/basics/somefile.php on line 27
Title: Re: delete unused images script
Post by: Gruz on July 20, 2012, 22:21:04 pm
Switch temporary to the default layout and run it (-:
Title: Re: delete unused images script
Post by: pierregulpen on July 21, 2012, 00:53:27 am
Nope,did not do the trick.

Could it be that i have a changed Vm ?

that is get Fatal error: Class 'ps_DB' not found  ?

Title: Re: delete unused images script
Post by: Gruz on July 21, 2012, 10:13:07 am
What VM version do you have?
Title: Re: delete unused images script
Post by: pierregulpen on July 21, 2012, 11:20:59 am
1.1.9 but i have a one page checkout build in and template from a third party.. Maybe there's the problem ?
Title: Re: delete unused images script
Post by: Gruz on July 21, 2012, 11:27:00 am
When adding the script to the template index.php you must run the web-site at a VM page. If a VM is not at the main page, then the VM class ps_DB is not loaded.

Add the script (from my web-site) and run proper url site.com/?option=com_virtuemart
Title: Re: delete unused images script
Post by: pierregulpen on July 21, 2012, 11:49:42 am
so instead of :  http://yoursite.com?tmpl=somefile

i should do :  site.com/?option=com_virtuemart
Title: Re: delete unused images script
Post by: Gruz on July 21, 2012, 12:08:42 pm
http://yoursite.com?tmpl=somefile&option=com_virtuemart

The somefile  must be placed in the active Joomla template.
Title: Re: delete unused images script
Post by: pierregulpen on July 21, 2012, 12:10:56 pm
ok, thnx i will try

let you know later if worked
Title: Re: delete unused images script
Post by: pierregulpen on July 21, 2012, 12:23:21 pm
 8) Works Great !!

Thank you , Total removed files :480 !!  saves me several MB in Webspace :-)
Title: Re: delete unused images script
Post by: amorino on March 29, 2013, 15:26:41 pm
Hello,
this script worked fine for me
thank you very much
Best regards
Amorino
Title: Re: delete unused images script
Post by: nunoms on October 03, 2013, 18:04:59 pm
Hi,

I am using Joomla 2.5 and Virtuemart 2.0.24.

I can not run this script, always gives the error: "Fatal error: Class 'ps_DB' "

I'm putting the following address: "http://localhost/site/?tmpl=script&option=com_virtuemart"


Someone can help me?


Thanks,
Nuno Amaral
Title: Re: delete unused images script
Post by: ex3mist on August 29, 2014, 09:09:01 am
Hello,
Now I'm also looking for a slution of this issue for VM2. Can anyone help? I've tried what is mentioned for VM1 by changing the path for the images folder, but unfortunately it doesn't work. Any help would be appreciated!
Title: Re: delete unused images script
Post by: ocean1 on September 25, 2014, 22:09:52 pm
I've updated my article here:
http://gruz.org.ua/en/about-joomla/34-virtuemart/27-vydalennja-zobrazhen-shcho-ne-vykorystovujutsja-z-virtuemart-11x.html (http://gruz.org.ua/en/about-joomla/34-virtuemart/27-vydalennja-zobrazhen-shcho-ne-vykorystovujutsja-z-virtuemart-11x.html)

Some other fixes also.


Hi, great script.
Is it possible that you update it also for VM2 and Joomla 2.5.x?

Title: Re: delete unused images script
Post by: d0ublezer0 on October 06, 2015, 20:16:13 pm
For vm2 or vm3:
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";
?>
Title: Re: delete unused images script
Post by: ex3mist on October 27, 2015, 17:19:26 pm
Hello d0ublezer0,

I've just tried your code but for some reason it didn't work. What I do is to create file /templates/YOURTEMPLATE/somefile.php and then execute it by typing this http://yoursite.com?tmpl=somefile in the browser. But nothing happens. Is it what I have to do? Am I missing something or doing anything wrong? Thanks!
Title: Re: delete unused images script
Post by: GJC Web Design on October 27, 2015, 22:42:55 pm
surely http://yoursite.com/templates/yourtemplate/somefile.php in the browser
Title: Re: delete unused images script
Post by: ex3mist on October 28, 2015, 08:45:59 am
Now it worked! I guess I was doing something wrong... Thanks a lot for the sollution!