By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

php GD makeing thumbnails to a certain size

P: 15
heya guys, im trying to take a variable input file and make a thumbnail so my gallery pageloads are lower. i want all the thumbnails to end up around 15ish kb, i know my current settings dont display that - im still testing.
it works on smaller files 250k, resizes to about 20ish k and 170k to about 3k, but i cant get it to resize bigger images and the while loop wont stop when the image reaches a certain size, i dont know where to go... pls help me debug this bugger ;)

Expand|Select|Wrap|Line Numbers
  1. $target_size=90000; // 7.5KB
  2. $initial_size=$target_path.$newfile;
  3. $filesize=filesize($initial_size);
  4. $file=$target_path.$newfile;
  5. $x=0;
  6. $reduction=($filesize*5)/100;
  7. $size = 0.9;
  8. while($filesize>$target_size){
  9.     $x++;
  10.     $save = $target_path."thumb_".$newname.".jpg";
  11.     $file = $file;
  12.     header('Content-type: image/jpeg') ; 
  13.     list($width, $height) = getimagesize($file) ; 
  14.     $modwidth = $width * $size; 
  15.     $modheight = $height * $size; 
  16.     $tn = imagecreatetruecolor($modwidth, $modheight) ; 
  17.     $image = imagecreatefromjpeg($file) ; 
  18.     imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
  19.     imagejpeg($tn, $save, 90) ;
  20.     $file = $target_path."thumb_".$newname.".jpg"; 
  21.     $filesize=$filesize-$reduction;
  22.     $size=$size-0.05;
  23.     }
  24. echo "resized $x times.";
Apr 27 '07 #1
Share this Question
Share on Google+
1 Reply


pbmods
Expert 5K+
P: 5,821
In a situation like this, I like to write all my variables to a file so I can keep track of what's going on.

Basically, you want to take advantage of the fwrite function. fwrite will write to the file directly and immediately so you don't have to worry about whether you will see output if you stop the browser from trying to load the page.

Naturally, you'll probably want to eventually write your own Debugger object, but for now, try something like this:

[php]
$reduction=($filesize*5)/100;
$size = 0.9;

// [DBuG]
$debugFile = fopen('/path/to/debug/file.txt');

while($filesize>$target_size){
$x++;
.
.
.

// // [DBuG]
fwrite($debugFile, <<<EOF
AFTER RUN #$x:
\$filesize = $filesize
(etc.)


EOF;

$size=$size-0.05;
}
echo "resized $x times.";


// [DBuG]
fclose($debugFile);
[/php]

After you're pretty sure your loop is definitely infinite, stop your browser (and kill the php process), then open up your debug file and take a look at what wasn't changing.

Of course, this won't fix your problem, but it should help give you a better idea of exactly what is going on.
Apr 28 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.