note 55824 deleted from ref.image by tularis
| From: | tularis@php.net | Date: | Sun, 29 Jun 2008 21:05:46 +0000 |
| Subject: | note 55824 deleted from ref.image by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-140937@lists.php.net to get a copy of this message | ||
Note Submitter: Mark Rosenthal <mbr at arlsoft dot com>
----
In AmX's example of how to generate headers to prevent the browser
from trying to cache a script-generated image, he wrote: "I did not
yet find a way to get the filesize of a GD made image, so I assume you
save it to disk, get the filesize and read it again."
That's not necessary. Here's AmX's example, reworked to output the
correct "Content-Length: " header without the intermediate step of
storing the image in a file:
// The following code assumes that $gd has been set to the GD
// resource of the image we want to output.
// Turn on output buffering
ob_start();
// Output will now go to a buffer rather than the browser.
imagejpeg($gd);
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: image/jpeg");
// Tell the browser the number of bytes that have been
// written to the buffer.
header("Content-Length: " . ob_get_length());
// Now send the buffer's contents to the browser and turn off
// output buffering.
ob_end_flush();
// Remember to free the memory used by the GD resource, or the
// sever will eventually crash.
imagedestroy($gd);
// Headers and image data have been sent. We're done.
exit;