*this is a repost...I lost the original in the great blog disaster of '06. I went looking for this info and had to pull it off of google cache, so here it is again.
Ok this one kicked my ass for a few days, I'm posting it here so maybe someone else wont have to go through it.
I was dynamically generating images from a php script using the imagejpeg() function:
-
$filename = $_GET['fileName'];
-
-
-
// Set a maximum height and width
-
$width = $_GET['width'];
-
$height = $_GET['height'];
-
-
-
//a little security
-
}
-
-
// Get new dimensions
-
-
if ($width) {
-
$width = ($height / $height_orig) * $width_orig;
-
} else {
-
$height = ($width / $width_orig) * $height_orig;
-
}
-
-
// Resample
-
$image_p = imagecreatetruecolor($width, $height);
-
$image = imagecreatefromjpeg($filename);
-
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
That worked fine, but if i tried to track the dowload using a bytesLoaded / bytesTotal combo, I was finding the bytesTotal wasn't being set. I figured that was probably a header I needed to set in php, and I was right. You need to set the size in the 'Content-Length' header.
Now my problem was how to get the size of a file you are about to output with the imagejpeg function. There were some notes in the php docs for strlen() that that function will return the size in bites if it is called on a binary object...but that went nowhere.
Turns out, what you need to do is start an output buffer using ob_start(). This will capture all the output from the script instead of outputting it to the browser. After you've created the buffer, you can call imagejpeg() and the image will be outputted to the buffer. After you've outputted the jpg to the buffer, you can call ob_get_length(); and store the size of the output buffer in a variable. After you've got the size of the buffer, you can call ob_end_clean() and discard the buffer....you'll still have access to the variable:
-
imagejpeg($image_p);
Now that you've got what you need (the total bytes of the image), you can add that to the Content-Length header, and output the jpg:
I think it looks a litle more complicated than what it is because I've broken up the code and explained it. Here is the code in it's entirety:
-
$filename = $_GET['fileName'];
-
-
-
// Set a maximum height and width
-
$width = $_GET['width'];
-
$height = $_GET['height'];
-
-
-
//a little security
-
}
-
-
// Get new dimensions
-
-
if ($width) {
-
$width = ($height / $height_orig) * $width_orig;
-
} else {
-
$height = ($width / $width_orig) * $height_orig;
-
}
-
-
// Resample
-
$image_p = imagecreatetruecolor($width, $height);
-
$image = imagecreatefromjpeg($filename);
-
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
-
-
-
imagejpeg($image_p);
-
-
-
imagejpeg($image_p);
-
-
imagedestroy($image_p);
Thanks a lot ! I was looking for a way to do this and I knew it was possible but never found it... Very helpful, yeah !
Well... I now have to apply it to my "Talk a Pic" project !