Author Topic: delete unused images script  (Read 26926 times)

georgian

  • Beginner
  • *
  • Posts: 12
delete unused images script
« 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);
};
}
}
?>

KooTjoo

  • Beginner
  • *
  • Posts: 35
Re: delete unused images script
« Reply #1 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

georgian

  • Beginner
  • *
  • Posts: 12
Re: delete unused images script
« Reply #2 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 ...

positronio

  • Beginner
  • *
  • Posts: 15
Re: delete unused images script
« Reply #3 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;

Gruz

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 159
  • Copyleft (є) all rights reversed
Re: delete unused images script
« Reply #4 on: December 26, 2011, 18:22:49 pm »
Your installable language for VM2: http://gruz.org.ua/virtuemart-language-files.html

sirius

  • Beginner
  • *
  • Posts: 29
    • OCTeam
Re: delete unused images script
« Reply #5 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

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"

Gruz

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 159
  • Copyleft (є) all rights reversed
Re: delete unused images script
« Reply #6 on: January 21, 2012, 10:20:55 am »
Your installable language for VM2: http://gruz.org.ua/virtuemart-language-files.html

sirius

  • Beginner
  • *
  • Posts: 29
    • OCTeam
Re: delete unused images script
« Reply #7 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.




Gruz

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 159
  • Copyleft (є) all rights reversed
Re: delete unused images script
« Reply #8 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?
Your installable language for VM2: http://gruz.org.ua/virtuemart-language-files.html

sirius

  • Beginner
  • *
  • Posts: 29
    • OCTeam
Re: delete unused images script
« Reply #9 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.

Gruz

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 159
  • Copyleft (є) all rights reversed
Re: delete unused images script
« Reply #10 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.
Your installable language for VM2: http://gruz.org.ua/virtuemart-language-files.html

sirius

  • Beginner
  • *
  • Posts: 29
    • OCTeam
Re: delete unused images script
« Reply #11 on: January 22, 2012, 18:22:54 pm »
will try to do that ok

pierregulpen

  • Beginner
  • *
  • Posts: 28
Re: delete unused images script
« Reply #12 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

Gruz

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 159
  • Copyleft (є) all rights reversed
Re: delete unused images script
« Reply #13 on: July 20, 2012, 22:21:04 pm »
Switch temporary to the default layout and run it (-:
Your installable language for VM2: http://gruz.org.ua/virtuemart-language-files.html

pierregulpen

  • Beginner
  • *
  • Posts: 28
Re: delete unused images script
« Reply #14 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  ?