LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-08-2005, 12:15 AM   #1
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Rep: Reputation: 30
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
 
Old 07-08-2005, 05:00 AM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
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)
 
Old 07-08-2005, 05:00 AM   #3
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 32
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 )
 
Old 07-08-2005, 12:18 PM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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.
 
Old 07-08-2005, 12:51 PM   #5
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 32
then you need
while ( ($filesize > 262144) and ($qual > 0) )

otherwise you will get stuck!! because $qual=0... oh, i'll run this look again :-)
 
Old 07-08-2005, 07:12 PM   #6
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Original Poster
Rep: Reputation: 30
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
 
Old 07-09-2005, 12:46 AM   #7
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Original Poster
Rep: Reputation: 30
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

Last edited by benrose111488; 07-09-2005 at 12:56 AM.
 
Old 07-09-2005, 05:54 AM   #8
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 32
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
 
Old 07-09-2005, 04:44 PM   #9
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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).
 
Old 07-09-2005, 08:04 PM   #10
Nathanael
Member
 
Registered: May 2004
Location: Karlsruhe, Germany
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940

Rep: Reputation: 32
ops ... my bad - sorry for that.....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GNOME Taking forever to load up. waelaltaqi Linux - Software 2 11-28-2005 02:49 PM
Its taking forever to load web pages in Mandrake 10 Stevetgn Linux - Newbie 3 06-22-2004 03:58 PM
MDK10 Taking forever to boot up... Ideas? bromack Mandriva 13 04-24-2004 09:50 PM
Mandrake 9.1 taking forever to boot jcksrobbins Mandriva 6 08-31-2003 01:19 PM
applications in Gnome taking forever (minutes) to launch SYS49152 Linux - Newbie 7 05-27-2003 01:22 AM


All times are GMT -5. The time now is 12:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration