ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940
Rep:
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")
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.
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?!?
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940
Rep:
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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.