note 55824 added to ref.image

From: Date: Sun, 14 Aug 2005 18:48:18 +0000
Subject: note 55824 added to ref.image
Groups: php.notes 
Request: Send a blank email to php-notes+get-93647@lists.php.net to get a copy of this message
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; ---- Manual Page -- http://www.php.net/manual/en/ref.image.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55824 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55824&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55824&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55824&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55824&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55824&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55824&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55824&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93647) next »