emma57573 Posted February 5, 2009 Share Posted February 5, 2009 Im missing something here Any ideas why this is failing? Im trying to look at all the images in 'imagetest' optimize them and deposit them into 'imagetest/new' What am I missing here ??? <? $path = "imagetest/"; //path to the folder $newpath = "imagetest/new/"; //path to the folder $img_gal = scandir($path); //scandir() gets all files (and file ext) in a folder and puts them into an array for($i=0;$i<count($img_gal);$i++){//loop tru the file $filename=$img_gal[$i]; $img = imagecreatefromjpeg($path.$filename); header("Content-Type: image/jpeg"); imagejpeg($img, $newpath.$filename, 80); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/ Share on other sites More sharing options...
premiso Posted February 5, 2009 Share Posted February 5, 2009 Wrong forum buddy, did you not see the: This is NOT a help forum! right below this forum? Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755479 Share on other sites More sharing options...
Daniel0 Posted February 5, 2009 Share Posted February 5, 2009 moved. Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755480 Share on other sites More sharing options...
Mark Baker Posted February 5, 2009 Share Posted February 5, 2009 So what exactly do you expect to happen when you do header("Content-Type: image/jpeg"); Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755484 Share on other sites More sharing options...
emma57573 Posted February 5, 2009 Author Share Posted February 5, 2009 So what exactly do you expect to happen when you do header("Content-Type: image/jpeg"); I left that in as it was in a snippet I found, I wasnt sure it was needed. But with or without it its still not working. Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755492 Share on other sites More sharing options...
emma57573 Posted February 5, 2009 Author Share Posted February 5, 2009 Wrong forum buddy, did you not see the: This is NOT a help forum! right below this forum? Sorry, I havnt got my brain screwed on the right way today hence why im having issues Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755496 Share on other sites More sharing options...
emma57573 Posted February 5, 2009 Author Share Posted February 5, 2009 This should help Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'imagetest/.' is not a valid JPEG file in /home/misimeuk/public_html/image_sizes.php on line 16 So its not reading the file name properly but I cant work out why. Do you think it should be in a while loop rather than a for loop. Or is it just my crappy code ??? Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755509 Share on other sites More sharing options...
emma57573 Posted February 6, 2009 Author Share Posted February 6, 2009 <? $path = "imagetest/"; //path to the folder $newpath = "imagetest/new/"; //path to the folder $files = scandir($path); //scandir() gets all files (and file ext) in a folder and puts them into an array foreach ($files as $file) { //loop tru the file $img = imagecreatefromjpeg($path.$file); header("Content-Type: image/jpeg"); imagejpeg($img, $newpath.$file, 80); } ?> Ive changed the code to the above, this manages to have an effect on one image but comes up with a memory error and the rest are unchanged. There are 6 images in the directory. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755821 Share on other sites More sharing options...
Mark Baker Posted February 6, 2009 Share Posted February 6, 2009 You're retrieving every single file/subfolder within imagetest/ This could include files that aren't jpegs, and always includes the . and .. folders..... but you're trying to treat them all as though they were jpeg files. $files = scandir($path); //scandir() gets all files (and file ext) in a folder and puts them into an array foreach ($files as $file) { //loop tru the file if (is_file($path.$file)) { // Ignore folders, symbolic links, etc if (strtolower(pathinfo($path.$file,PATHINFO_EXTENSION)) == 'jpg') { // Only process jpg files $img = imagecreatefromjpeg($path.$file); imagejpeg($img, $newpath.$file, 80); ... } } } And get rid of header("Content-Type: image/jpeg"); Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755840 Share on other sites More sharing options...
emma57573 Posted February 6, 2009 Author Share Posted February 6, 2009 Thank you so much for your help, that if statement has done the trick Quote Link to comment https://forums.phpfreaks.com/topic/143977-solved-scandir-with-a-for-loop-function-not-working/#findComment-755849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.