PHP image-creation while loop taking forever
hello... I'm trying to down the quality of a JPEG image file if it's above 256k, however the following code takes literally an hour to execute... on an extremely fast machine.
PHP Code:
any idea why the rest of this code is taking so long to execute?? Thanks in advance |
You don't need to destroy the image in each loop iteration...
try : PHP Code:
$filesize > 262144 instead (eg without quotes on the value) |
i think you might have an endless loop there!
because you are always working with $im = imagecreatefromstring ($fileContent); and $fileContent does not seem to schange!! meaning: starting off with a size of 512KB+ (anything above 512KB) you runn into the look that continues until the size is smaller than 256KB, you half filesize but will end with more than 256KB (because you are starting with mor than 512KB) and you need to run the loop again, resetting $im to what it was in the first round!! (if i got that right that is :-P ) |
You might want to add a check on $qual as well... if imagejpeg() with $qual==0 produces a file larger than 256k (with the above correction of removing the quotes), you will be in an endless loop.
Instead of: Code:
while ($filesize > "262144") Code:
while ( ($filesize > 262144) and ($qual >= 0) ) |
then you need
while ( ($filesize > 262144) and ($qual > 0) ) otherwise you will get stuck!! because $qual=0... oh, i'll run this look again :-) |
In regards to the infinate loop going on, that seems to be exactly the problem.
I've altered my code to read: PHP Code:
1447995 1447995 1447995 1447995 1447995 1447995 1447995 1447995 1447995 1447995 it seems the file size isn't going down when I rewrite the file based upon a lower quality. Going back to what Nathanael said, It may be the $fileContent that's causing this problem. I'm stumped for solutions except making the file originally at 100% quality, assigning that a temporary filename, then making all of the new files based upon the previous just-made file. Then, once two files are existant, deleting the first, hopefully larger, file. Does that sound like it would work? Does anyone have any ideas other than this?!? thanks again |
oop cancel that.... I got it working now
the new code reads as follows... if you know any ways to streamline this (it's really rough) please let me know PHP Code:
1447995 407864 326983 270390 237399 :-D:-D:-D thanks very very much to all those who contributed |
your loop does not stop once the filesize is below 256K but when filesize below 256K AND the quality is below 0 !!
you might want to change your loop to read either: PHP Code:
oterewise your quality will ALWAYS be -10 and your filesize could end up i-dont-know-how-small KB |
Nathanael, I think you have your logic confused.
If either side of the && expression is false, the entire expression is false, which exits the loop. So if the size is too large but qual has sunk to -10, the loop exits regardless. If the size goes below the threshold before qual reaches zero, it still quits, because the && expression evaluates false. Remember that the while loop runs only as long as the expression is true. With your || expression, the condition is not false until BOTH halves are false, so the size could be lower than the threshold, but it will continue through the loop until qual reaches -10 (effectively making each picture the very worst quality). |
ops ... my bad - sorry for that.....
|
All times are GMT -5. The time now is 07:08 AM. |