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
-
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 ...
<?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);
};
}
}
?>
-
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
-
I insert this code into browse page (for testing).
I do not know how to insert in the administration page. Maybe others ...
-
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;
-
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)
-
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:
$q = "SELECT * #__{vm}_product_files WHERE `file_name`='".$reldir.$imgfile."'";
if ($db->num_rows()>0) {
to launch the query:
$q = "SELECT * #__{vm}_product_files WHERE `file_name`='".$reldir.$imgfile."'";
$db->query( $q );
if ($db->num_rows()>0) {
But a better way could be:
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"
-
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.
-
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.
-
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?
-
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.
-
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.
-
will try to do that ok
-
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 : 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
-
Switch temporary to the default layout and run it (-:
-
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 ?
-
What VM version do you have?
-
1.1.9 but i have a one page checkout build in and template from a third party.. Maybe there's the problem ?
-
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
-
so instead of : http://yoursite.com?tmpl=somefile
i should do : site.com/?option=com_virtuemart
-
http://yoursite.com?tmpl=somefile&option=com_virtuemart
The somefile must be placed in the active Joomla template.
-
ok, thnx i will try
let you know later if worked
-
8) Works Great !!
Thank you , Total removed files :480 !! saves me several MB in Webspace :-)
-
Hello,
this script worked fine for me
thank you very much
Best regards
Amorino
-
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
-
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!
-
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?
-
For vm2 or vm3:
<?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==0 && $thumb==0) echo "Nothing to delete";
?>
-
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!
-
surely http://yoursite.com/templates/yourtemplate/somefile.php in the browser
-
Now it worked! I guess I was doing something wrong... Thanks a lot for the sollution!