473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

expires header on php generated image

103 100+
I am trying to make my php-generated images cache. Here is a summary of the script with the headers. It doesn't seem to cache though. Do I need to do something special to make sure it caches?

Expand|Select|Wrap|Line Numbers
  1. //summarised
  2.  
  3. //resize...
  4. $file= $_GET['file'];
  5. $o= imagecreatefromgif($file);
  6. $i= imagecreatetruecolor($w,$h);
  7. imagecopyresampled($i,$o,0,0,0,0,$w,$h,$wo,$ho);
  8.  
  9. //output
  10. header('Content-type: image/gif');
  11. header('Expires: '.date(DATE_RFC1123,time()+2592000));
  12. imagegif($i);
  13. imagedestroy($i);
  14.  
  15.  
Here are the response headers (using firebug). The server clearly gets the expires header. But I still have the overhead showing in firebug.

Date Tue, 29 Jul 2008 13:06:36 GMT
Server Apache/2.0.55 (Ubuntu) PHP/5.1.2
X-Powered-By PHP/5.1.2
Expires Thu, 28 Aug 2008 14:06:36 BST
Content-Length 2182
Keep-Alive timeout=15, max=95
Connection Keep-Alive
Content-Type image/gif
Jul 29 '08 #1
2 5102
pbmods
5,821 Expert 4TB
Heya, Henry.

I hate to just drop an article on you, but I have to run out the door in a sec. In the meantime, perhaps this article will have some information that you might find useful (http://www.mnot.net/cache_docs/).

And I am now subscribed to this thread (:
Jul 29 '08 #2
henryrhenryr
103 100+
I hate to just drop an article on you,
That's fine! I'd seen this one but it seems like it was one of those occasions where I just didn't type the right thing into google yesterday!

I just found this:
http://php.dtbaker.com.au/post/cachi..._php_imagejpeg

The solution is to use the last-modified header based on the filemtime() for the input file. The article has a solution if you have no input file too. The browser now caches the images. I hope it will refresh when they are updated!

Expand|Select|Wrap|Line Numbers
  1. //summarised
  2. //two additions - check last-modified header and exit if same as 
  3. //filemtime(), also set last-modified header
  4.  
  5. //resize...
  6. $file= $_GET['file'];
  7.  
  8. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==filemtime($file)) {
  9.   header('Last-modified: '.date(DATERFC_1123,filemtime($file)),true,304);
  10.   exit;
  11. }
  12.  
  13. $o= imagecreatefromgif($file);
  14. $i= imagecreatetruecolor($w,$h);
  15. imagecopyresampled($i,$o,0,0,0,0,$w,$h,$wo,$ho);
  16.  
  17. //output
  18. header('Content-type: image/gif');
  19. header('Expires: '.date(DATE_RFC1123,time()+2592000));
  20. header('Last-modified: '.date(DATE_RFC1123,filemtime($file);
  21. imagegif($i);
  22. imagedestroy($i);
  23.  
  24.  
Jul 30 '08 #3

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

Similar topics

1
by: sunil | last post by:
Iam trying to implement sorting. I know how to display a button in the header of an datagrid. How can i display an image with down arrow and uparrow along the side of button on the header. Any...
2
by: Olav Tollefsen | last post by:
I have som code that dynamically generates an image and writes it back to the browser from an .aspx codebehind file like this: System.Drawing.Image image; image = GenerateImage();...
0
by: eff_kay | last post by:
How can I use an image as background image for header of a datagrid Stuck!!!!! eff_kay
0
by: Les Caudle | last post by:
Anyone have some sample code for a database generated menu based on a DataList (or other control). that would also generate the javascript necessary for a rollover to a 2nd image when the mouse...
4
by: Ersin Gençtürk | last post by:
Hi , I am creating dynamic images (for example writing text into a bitmap) and saving it to the output stream with gdi library in .net but I can't adjust image quality. The best parameters...
3
by: VP | last post by:
I would like to specify 'Expires' header for a js file. Can you please let me know where & how to specify that. Thanks.
10
by: dave Cheseldine | last post by:
Hi I want to stop a gd generated image from being cached. It is called into index.php file as follows: <image src = "http://www.daveches.co.uk/importimage/importimage.php?x=$randomNumber"...
4
by: Jim Michaels | last post by:
<?php include 'dbinc.php'; $result = mysql_query("SELECT image,mime_type FROM photos WHERE image_id=".intval($_GET), $link) or die("MyErr:".mysql_error()); if ($row = mysql_fetch_assoc($result))...
2
by: helveticus | last post by:
I'm having difficulties setting the expires header for images and css files in my compression HttpModule. Compression is carried out by context_BeginRequest and works fine (code not shown)....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.