djpic Posted March 8, 2008 Share Posted March 8, 2008 <? $ImageFolderPath = "rawphotos/"; $Folder = opendir($ImageFolderPath); $LoopCount = 0; while (false !== ($File = readdir($Folder))) { $FullImagePath = "$ImageFolderPath$File"; echo "$FullImagePath\n"; $image = imagecreatefromjpeg($FullImagePath); $width = imagesx($image); $height = imagesy($image); $new_width = 300; $new_height = 300; $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $Destination = "test/$LoopCount.jpg"; imagejpeg($image_resized, $Destination, 100); imagedestroy($image_resized); $LoopCount++; } closedir($Folder);?> This script only goes through the first two files in the directory and then stops. Only rights one file to the test directory (it perfect size and such) but only echos the filename of the second one and never continues the loop? Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/ Share on other sites More sharing options...
djpic Posted March 8, 2008 Author Share Posted March 8, 2008 Ok, Now I have rewritten the code. It processes more of them now, the first 15 then stops. I made a function instead of placing it into the loop itself. Is there any settings that need to be changed in the GD? <?php function CreateImage($ImagePath, $Count) { $image = imagecreatefromjpeg($ImagePath); $width = imagesx($image); $height = imagesy($image); $new_width = 300; $new_height = 300; $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $Destination = "test/$Count.jpg"; imagejpeg($image_resized, $Destination, 100); imagedestroy($image_resized); } $ImageFolderPath = "rawphotos/"; $Folder = opendir($ImageFolderPath); $LoopCount = 0; while (false !== ($File = readdir($Folder))) { $FullImagePath = "$ImageFolderPath$File"; echo "$FullImagePath\n"; CreateImage($FullImagePath, $LoopCount); $LoopCount++; } closedir($Folder); ?> Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486761 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 Add error_reporting(E_ALL); to the top while testing, and if that still doesn't put out an error, check your webserver's error log. I suspect you'll find the script is timing out. If you see an error about execution time or something, try adding set_time_limit(0) to the top of your script so that the script can run as long as it needs to. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486773 Share on other sites More sharing options...
djpic Posted March 8, 2008 Author Share Posted March 8, 2008 lol..that is what I am looking into right now. I tripled the amount of memory already. I will try to put that at the beginning of the code and see what happens. UPDATE: I changed the php.ini to allow more script executing time and it did complete more photos. Actually finished all 105 of them. I am going to try putting the settings back to defaults and then try the code you gave me. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486779 Share on other sites More sharing options...
djpic Posted March 8, 2008 Author Share Posted March 8, 2008 Yep, that worked. Thanks. Learned a little more about PHP today! Quick question though. Is my script right, is there anything I can do for it use less memory? Here is my up-to-date script: <?php set_time_limit(0); function CreateImage($ImagePath, $Count) { $image = imagecreatefromjpeg($ImagePath); $width = imagesx($image); $height = imagesy($image); $new_width = 300; $new_height = 300; $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $Destination = "test/$Count.jpg"; imagejpeg($image_resized, $Destination, 100); imagedestroy($image_resized); imagedestroy($image); } $ImageFolderPath = "/home/photoshop/rawphotos/"; $Folder = opendir($ImageFolderPath); $LoopCount = 0; while (false != ($File = readdir($Folder))) { $FullImagePath = "$ImageFolderPath$File"; if ($FullImagePath != "$ImageFolderPath..") { CreateImage($FullImagePath, $LoopCount); echo "$FullImagePath\n"; } $LoopCount++; } closedir($Folder); echo "$LoopCount"; ?> Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486783 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 How big is your biggest picture? When the function execution ends, the memory held by the GD things should be realeased, but during the function call, the GD stuff is of course going to takea bit of memory if it's large images. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486793 Share on other sites More sharing options...
djpic Posted March 8, 2008 Author Share Posted March 8, 2008 Ok, say I compress a 5MB photo. The function will use ~5MB of memory but once the function is complete, everything that was held in the memory during that function will be erased then it will move on to the next photo. Therefore, if every file is 5MB the script will only ever use a max of roughly 5MB? I did add the: imagedestroy() function at the end of the function, was that needed or does that help in any way? BTW, the photos are going to be coming from a 12 Megapixel camera and a 6 Megapixel camera. I have the PHP memory limit set to 64MB. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486796 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 I'm not quite sure whether or not when a function ends if the memory tied up by those resources is released. I would imagine it is, but let me test it really quick. EDIT <?php function test() { $a = str_repeat("aaaaaaaaaa", 100000); echo memory_get_usage() . PHP_EOL; return; } echo memory_get_usage() . PHP_EOL; test(); echo memory_get_usage() . PHP_EOL; ?> This out puts (for me): 51456 1051808 51792 That shows me that the resources are indeed freed. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486801 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 (Grrr couldn't edit my other post) To answer your question: Ok, say I compress a 5MB photo. The function will use ~5MB of memory but once the function is complete, everything that was held in the memory during that function will be erased then it will move on to the next photo. Therefore, if every file is 5MB the script will only ever use a max of roughly 5MB? I did add the: That is correct. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486814 Share on other sites More sharing options...
djpic Posted March 8, 2008 Author Share Posted March 8, 2008 Ok that is awesome...thanks a billion! So did I need those imagedestroy() commands there do you think or should I leave them there just for good measure? Also, did realize that php had a function to get the memory usage..do you know if that is in bytes or KB? I will use that t/m and see exactly how much the script is actually taking up. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486817 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 The imagedestroy()s are technically not needed, but you can leave them just for good measure. What does "t/m" mean? Anyway, it's measured in bytes. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486820 Share on other sites More sharing options...
djpic Posted March 8, 2008 Author Share Posted March 8, 2008 t/m is short of tomorrow. Thanks for the help, I think I am going to call it a night. Good Night to you as well. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486823 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 No problem, and I never would have realized it meant tomorrow lol..... Night. Link to comment https://forums.phpfreaks.com/topic/95020-solved-help-with-this-code-thought-it-would-work-but-for-some-reason-it-is-not-workin/#findComment-486824 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.