bill110 Posted September 19, 2009 Posted September 19, 2009 I have a script that creates thumbnails from shop images and places them in a directory called "thumbnails". The script renames the images using the size in its name. example : original image name: dress.jpg (the size would be 400x400) thumbnail name: dress.jpg.thumbnail_60x60.jpg (60x60 is the thumbnail size) if the original image is a .gif the thumbnails extension is also .gif I want, when I delete the original image in admin, to search the thumbnail directory for the correct thumbnail file to delete also. I have no problems navigating the script to the correct directory to search, but to find the thumbnail file name based on the original filename is where I'm stuck. The "60x60" will be different on each image based on the original image size. My idea is to create a pattern to search for.. I'm just not sure how to do it. then use something like this $pattern $dleete_me = path_to/thumbnail_directory/ . 'original_image_name.thumb_' . $pattern; I can figure out how to add the correct extension ".gif, .jpg, .png" Then i'll use the unlink php function to delete the image. any help would be appreciated. My Contributions Stylesheet With Descriptions Glassy Grey Boxtops Our Products Meta Tags On The Fly Password Protect Admin "No matter where you go....There you are" - Buccaroo Bonsai
♥FWR Media Posted September 19, 2009 Posted September 19, 2009 [deleted as was incorrect] Ultimate SEO Urls 5 PRO - Multi Language Modern, Powerful SEO Urls KissMT Dynamic SEO Meta & Canonical Header Tags KissER Error Handling and Debugging KissIT Image Thumbnailer Security Pro - Querystring protection against hackers ( a KISS contribution ) If you found my post useful please click the "Like This" button to the right. Please only PM me for paid work.
GemRock Posted September 19, 2009 Posted September 19, 2009 <br />I have a script that creates thumbnails from shop images and places them inthis should not be a difficult thing to do. the tip is, since the thumb would always have the name of the original image file in it, so you just need to read the thumb files and do a search in the file name based on the fact i just mentioned. once found, then delete it is easy. Good Luck! Ken commercial support - unProtected channel, not to be confused with the forum with same name - open to everyone who need some professional help: either PM/email me, or go to my website (URL can be found in my profile). over 20 years of computer programming experience.
♥FWR Media Posted September 19, 2009 Posted September 19, 2009 Revised code, you can run this file as a standalone php file if you want to test. Hope it helps. <?php $imagename = 'graph.png'; // The base image $mime = strstr( $imagename, '.' ); // Grab the mime type $prefix = '.thumbnail_'; // Prefix $image_returned = 'graph.png.thumbnail_60x60.png'; // Dummy return from the thumbnail directory search $pattern = '@^' . $imagename . $prefix . '[0-9]{1,3}[x]{1}[0-9]{1,3}' . $mime . '$@'; // Search regex $pattern = str_replace( '.', '\.', $pattern ); // escape the dots preg_match( $pattern, $image_returned, $matches ); // Action the match $counted_matches = count( $matches ); // how many results were returned? // Test the result and take the relevant action switch( true ) { case $counted_matches > 1: echo 'Not good .. more matches than just 1!'; break; case $counted_matches == 1: echo 'Perfect! .. one match found which was ' . $matches[0]; break; default: echo 'No matches found unfortunately'; break; } ?> Ultimate SEO Urls 5 PRO - Multi Language Modern, Powerful SEO Urls KissMT Dynamic SEO Meta & Canonical Header Tags KissER Error Handling and Debugging KissIT Image Thumbnailer Security Pro - Querystring protection against hackers ( a KISS contribution ) If you found my post useful please click the "Like This" button to the right. Please only PM me for paid work.
bill110 Posted September 20, 2009 Author Posted September 20, 2009 Revised code, you can run this file as a standalone php file if you want to test. Hope it helps. <?php $imagename = 'graph.png'; // The base image $mime = strstr( $imagename, '.' ); // Grab the mime type $prefix = '.thumbnail_'; // Prefix = 'graph.png.thumbnail_60x60.png'; // Dummy return from the thumbnail directory search $pattern = '@^' . $imagename . $prefix . '[0-9]{1,3}[x]{1}[0-9]{1,3}' . $mime . '$@'; // Search regex $pattern = str_replace( '.', '\.', $pattern ); // escape the dots preg_match( $pattern, $image_returned, $matches ); // Action the match $counted_matches = count( $matches ); // how many results were returned? // Test the result and take the relevant action switch( true ) { case $counted_matches > 1: echo 'Not good .. more matches than just 1!'; break; case $counted_matches == 1: echo 'Perfect! .. one match found which was ' . $matches[0]; break; default: echo 'No matches found unfortunately'; break; } ?> Thanks I will give it a try. I'm assuming $matches[0] will be the file name? I'll then convert to string to use the unlink php function. My Contributions Stylesheet With Descriptions Glassy Grey Boxtops Our Products Meta Tags On The Fly Password Protect Admin "No matter where you go....There you are" - Buccaroo Bonsai
GemRock Posted September 20, 2009 Posted September 20, 2009 there is a simple thing theres a simple way. heres my simple way: this is the line to delete an image in categories.php: unlink(DIR_FS_CATALOG_IMAGES . $HTTP_POST_VARS['products_image']); add this below it: $image2delete = $_POST['products_image']; $len = strlen($image2delete)+1; $dir = DIR_WS_CATALOG_IMAGES . '/thumbnail'; $img_files = scandir($dir); $count = count($img_files); for ($i=0; $i<$count; $i++){ $thumb = $img_files[$i]; if (stristr($thumb,$image2delete) && substr($thumb,$len,1)!='.' ){ unlink(DIR_FS_CATALOG_IMAGES . $thumb); } } you may need to change the image path a bit based on your definition in configure.php Ken commercial support - unProtected channel, not to be confused with the forum with same name - open to everyone who need some professional help: either PM/email me, or go to my website (URL can be found in my profile). over 20 years of computer programming experience.
GemRock Posted September 20, 2009 Posted September 20, 2009 forgor to mention the code only work for php5. will need to change for php4 (but who would still use php4?). you may want to add below unlink(DIR_FS_CATALOG_IMAGES . $thumb); this: $i = $count to end the for loop. Ken commercial support - unProtected channel, not to be confused with the forum with same name - open to everyone who need some professional help: either PM/email me, or go to my website (URL can be found in my profile). over 20 years of computer programming experience.
GemRock Posted September 20, 2009 Posted September 20, 2009 $i = $count should be $i = $count; my eyes telling to go to bed now! Ken commercial support - unProtected channel, not to be confused with the forum with same name - open to everyone who need some professional help: either PM/email me, or go to my website (URL can be found in my profile). over 20 years of computer programming experience.
GemRock Posted September 20, 2009 Posted September 20, 2009 oops, about to turn off computer but soemthing seems not right in my mind. substr($thumb,$len,1)!='.' should be substr($thumb,$len,1)=='.' and $len = strlen($image2delete)+1; should be $len = strlen($image2delete)-4; this is to make sure dress.jpg would not mistakenly delete mydress.jpg. Ken commercial support - unProtected channel, not to be confused with the forum with same name - open to everyone who need some professional help: either PM/email me, or go to my website (URL can be found in my profile). over 20 years of computer programming experience.
♥FWR Media Posted September 20, 2009 Posted September 20, 2009 Had a few secs this morning, here is the working version for mine. Presumes you are running php5. You would need to ensure that $_POST['products_image'] contains the image name and change the thumbs path. <?php $imagename = $_POST['products_image']; // Presuming $_POST['products_image'] contains the image name $thumbspath = DIR_WS_IMAGES; // change to the thumbs path $mime = strstr( $imagename, '.' ); // Grab the mime type $prefix = '.thumbnail_'; // Prefix $pattern = '@^' . $imagename . $prefix . '[0-9]{1,3}[x]{1}[0-9]{1,3}' . $mime . '$@'; // Search regex $pattern = str_replace( '.', '\.', $pattern ); // escape the dots $it = new DirectoryIterator( $thumbspath ); while( $it->valid() ) { if( false === $it->isDir() ) { preg_match( $pattern, $it->getFilename(), $matches ); // Action the match if ( ( count( $matches ) == 1 ) && is_readable( $thumbspath . $matches[0] ) ) { // Valid return and readable image if ( false === @unlink( $thumbspath . $matches[0] ) ) { // Unlink the image trigger_error( 'Unable to delete thumb ' . $thumbspath . $matches[0], E_USER_WARNING ); // Failed to unlink } } } $it->next(); // Next iteration } ?> Ultimate SEO Urls 5 PRO - Multi Language Modern, Powerful SEO Urls KissMT Dynamic SEO Meta & Canonical Header Tags KissER Error Handling and Debugging KissIT Image Thumbnailer Security Pro - Querystring protection against hackers ( a KISS contribution ) If you found my post useful please click the "Like This" button to the right. Please only PM me for paid work.
GemRock Posted September 20, 2009 Posted September 20, 2009 had it not been too late last night i would have suggested a even simpler way that would kill your headache at its root: 1. below is the line in the product_thumb.php that creates the headache: if ($tn_server_cache) $filename = modify_tn_path($_GET['img'] .'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.jpg'); change it to: if ($tn_server_cache) $filename = modify_tn_path($_GET['img']); you can also kill the modify_tn_path function and hardcode your image path. theres more than one occurance of this code and theres also one for gif image, one for png image, so make the same change to all of them. 2. since the thumbs are generated "on the fly", you could safly delete all of them under the thumbnails folder and let the above modified script to recreated them with a "new" name that is the same as the products_image itself. its unnecessary and offers no extra benefit to create a thumb with that sort of stupid names. they are under different folders (images/ and images/thumbnails/), no chance of duplication or one overwrites the other. Ken commercial support - unProtected channel, not to be confused with the forum with same name - open to everyone who need some professional help: either PM/email me, or go to my website (URL can be found in my profile). over 20 years of computer programming experience.
bill110 Posted September 20, 2009 Author Posted September 20, 2009 This looks like a class: $it = new DirectoryIterator I do not have that class file. My Contributions Stylesheet With Descriptions Glassy Grey Boxtops Our Products Meta Tags On The Fly Password Protect Admin "No matter where you go....There you are" - Buccaroo Bonsai
bill110 Posted September 20, 2009 Author Posted September 20, 2009 you may need to change the image path a bit based on your definition in configure.phpKen I will check into this one also. I really appreciate you guys taking the time. I was trying to use glob() but it kept returning an empty array. My Contributions Stylesheet With Descriptions Glassy Grey Boxtops Our Products Meta Tags On The Fly Password Protect Admin "No matter where you go....There you are" - Buccaroo Bonsai
♥FWR Media Posted September 20, 2009 Posted September 20, 2009 This looks like a class:$it = new DirectoryIterator I do not have that class file. DirectoryIterator is one of the iterator classes of the SPL which are included in PHP5 as standard, these are faster than userland php functions as they expose php internals. Ultimate SEO Urls 5 PRO - Multi Language Modern, Powerful SEO Urls KissMT Dynamic SEO Meta & Canonical Header Tags KissER Error Handling and Debugging KissIT Image Thumbnailer Security Pro - Querystring protection against hackers ( a KISS contribution ) If you found my post useful please click the "Like This" button to the right. Please only PM me for paid work.
bill110 Posted September 20, 2009 Author Posted September 20, 2009 had it not been too late last night i would have suggested a even simpler way that would kill your headache at its root:1. below is the line in the product_thumb.php that creates the headache: if ($tn_server_cache) $filename = modify_tn_path($_GET['img'] .'.thumb_'.$_GET['w'].'x'.$_GET['h'].'.jpg'); change it to: if ($tn_server_cache) $filename = modify_tn_path($_GET['img']); you can also kill the modify_tn_path function and hardcode your image path. theres more than one occurance of this code and theres also one for gif image, one for png image, so make the same change to all of them. 2. since the thumbs are generated "on the fly", you could safly delete all of them under the thumbnails folder and let the above modified script to recreated them with a "new" name that is the same as the products_image itself. its unnecessary and offers no extra benefit to create a thumb with that sort of stupid names. they are under different folders (images/ and images/thumbnails/), no chance of duplication or one overwrites the other. Ken Your code works. As far as #2 goes I thought about that. I was just going to go into the script that creates them and change it there, since for my use the thumbs will always be in a sub folder. My Contributions Stylesheet With Descriptions Glassy Grey Boxtops Our Products Meta Tags On The Fly Password Protect Admin "No matter where you go....There you are" - Buccaroo Bonsai
Recommended Posts
Archived
This topic is now archived and is closed to further replies.