Recording dynamic titles

I have a dynamically generated php image that I need to write to a file so I can call later. My problem is that I need this image to include the appropriate expiration headers in it. There are a huge number of them and their headers change individually for each file. The htaccess control is not an option.

I can write expiration headers if I output the image directly to the browser using this: header ("Content-Type: image / jpeg"); header ('Expires: "'. gmdate (" D, d MYH: i: s ", $ expirationDate). '"'); imagepng ($ image, NULL);

Or I can write the image to a file to be used later: imagepng ($ image, $ filepath)

But I can't for the life of me figure out how to combine the two and write the image to a file including its expiration headers. How are you going to write an image file with an expires header?

+2


a source to share


3 answers


I think the best is on the server, just like you, for example:

header("Content-Type: image/jpeg"); 
header('Expires: "' . gmdate("D, d M Y H:i:s",
  $expirationDate) . '"'); 
imagepng($image, NULL);

      

You are of course using php to serve a static file, but the expire header is going to restrict retry requests.

Update. Since it $image

is a generated file, it generates and saves an image on the first request, and then outputs it. For additional requests, simply display the already generated image. Basically, headers expire

manage the browser cache, while you need to implement some kind of caching on the server so that you don't generate the same result multiple times.

So you're looking at two different types of caching . You can do them in the same script, with a combination of the two scripts - really whatever you want.



If you cannot set the standard expire header with apache (which as you say you cannot as it changes) I believe this is your best (if not only) choice.

Of course the tricky and tricky way :

  • Configure mod_rewrite to send requests for no images to your php script.
  • Add session id to image request (hence unique to browser).
  • Have the php script send the expire header and image content.
  • Whether to bind a php script to a real static image with a session specific image name.

Or something like that. I would just serve them all using php.

Update. Or use mod_asis

from VolkerK 's excellent answer .

+1


a source


If you really want to store both headers and content in files on the server, you can use mod_asis :

In the server config file, associate files with a send-as-is handler, for example.
AddHandler send-as-is asis


The contents of any .asis file will be sent to the Apache client with little or no change. Specifically, the HTTP headers are obtained from the file itself according to mod_cgi rules, so the asis file must contain valid headers and can also use the CGI Status: header to define the HTTP response code.


Then your php script will write both headers and content to files, which are processed as send-as-is

apache webserver.

+1


a source


Perhaps all you have to do is for sure ... nothing but writing the image data to disk.
Depending on the web server, you use some caching mechanisms working out of the box for static files (which you create with a php script).

If you are using apache httpd take a look at http://httpd.apache.org/docs/2.2/mod/core.html#fileetag and http://httpd.apache.org/docs/2.2/caching.html . By default, httpd also sends the last modified header and supports If-Modified-Since request headers.
When your php script modifies the image files, the ETag will change and / or the If-Modified-Since condition will be met and httpd will send the data. Otherwise, it would send a "nothing changed" response to the client.

0


a source







All Articles