LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP image-creation while loop taking forever (https://www.linuxquestions.org/questions/programming-9/php-image-creation-while-loop-taking-forever-341146/)

benrose111488 07-07-2005 11:15 PM

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:

$im imagecreatefromstring ($fileContent);
imagejpeg($im$newfilename100);
imageDestroy($im);
$filesize=filesize($newfilename);
$qual="90";
while (
$filesize "262144")
{
$im imagecreatefromstring ($fileContent);
imagejpeg($im$newfilename$qual);
imageDestroy($im);
$qual=$qual-10;
$filesize=filesize($newfilename);


when I commented out the while loop, it took only 3 seconds to execute, generating a 1.3 meg file at blazing speeds

any idea why the rest of this code is taking so long to execute??

Thanks in advance

keefaz 07-08-2005 04:00 AM

You don't need to destroy the image in each loop iteration...
try :
PHP Code:

$im imagecreatefromstring ($fileContent);
imagejpeg($im$newfilename100);
$qual=90;

while (
filesize($newfilename) > 262144) {
    
imagejpeg($im$newfilename$qual);
    
$qual=$qual-10;
}
imageDestroy($im); 

Also, do no test $filesize > "262144", do
$filesize > 262144 instead
(eg without quotes on the value)

Nathanael 07-08-2005 04:00 AM

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 )

CroMagnon 07-08-2005 11:18 AM

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")
try:
Code:

while ( ($filesize > 262144) and ($qual >= 0) )
It's up to you if you want to try something else like resizing if the file size is still too large, but if $qual is 0, you've already got the best compression you're going to get with that data.

Nathanael 07-08-2005 11:51 AM

then you need
while ( ($filesize > 262144) and ($qual > 0) )

otherwise you will get stuck!! because $qual=0... oh, i'll run this look again :-)

benrose111488 07-08-2005 06:12 PM

In regards to the infinate loop going on, that seems to be exactly the problem.

I've altered my code to read:

PHP Code:

$im imagecreatefromstring ($fileContent);
imagejpeg($im$newfilename100);
$qual=90;
while (
filesize($newfilename) > 262144 && $qual >= 0)
{
$sizecheck filesize($newfilename);
echo(
"$sizecheck<BR>");
imagejpeg($im$newfilename$qual);
$qual=$qual-10;
}
imageDestroy($im); 

however, this simply outputs:

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

benrose111488 07-08-2005 11:46 PM

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:

$im imagecreatefromstring ($fileContent);
imagejpeg($im$newfilename100);
$qual=90;
$yes=filesize($newfilename);
while (
$yes 262144 && $qual >= 0)
{
$im imagecreatefromstring ($fileContent);
imagejpeg($im$newfilename$qual);
$qual=$qual-10;
$yes filesize($newfilename);
echo(
"$yes<BR>");
if(
$yes 262144)
{
unlink($newfilename);}
}
imageDestroy($im); 

output:
1447995
407864
326983
270390
237399


:-D:-D:-D

thanks very very much to all those who contributed

Nathanael 07-09-2005 04:54 AM

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:

while ($yes 262144 || $qual 0

meaning: either filesize below 256K OR quality = 0
oterewise your quality will ALWAYS be -10 and your filesize could end up i-dont-know-how-small KB

CroMagnon 07-09-2005 03:44 PM

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).

Nathanael 07-09-2005 07:04 PM

ops ... my bad - sorry for that.....


All times are GMT -5. The time now is 07:08 AM.