Is it possible to force an browser to cache a php image?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RottNKorpse
Forum Newbie
Posts: 10
Joined: Sun Aug 05, 2007 3:04 pm

Is it possible to force an browser to cache a php image?

Post by RottNKorpse »

Ok here is what I am wanting to do: Host an image on Photobucket, Use PHP to generate the image with a url as if it is being hosted on my site and cache the php generated image for faster loading later.

I am currently using a photobucket account to host my images so the original file I want to cache is actually at the following URL:

Code: Select all

http://i87.photobucket.com/albums/k126/ ... XviD-E.gif
I am wanting to display the image with a URL from my site so that it doesn't show the fact that it is on a photobucket account.

Code: Select all

http://wrestlemation.com/fa_Brain_Rush_ ... XviD-E.gif
As you can tell the image from the second link is being generated to pretty much pull in information from the first one and the displaying of the first one under the new URL works just fine.

To do what I've done, I used .htaccess rewriting to make the php image generation to use a standard image format url.

The php I used to generate the php image is...

Code: Select all

//Get dynamic filename from rewritten url. --- which would be : Brain_Rush_S01E01_DotNXT_DSR_XviD-E
$gif_file = "http://i87.photobucket.com/albums/k126/WrestleMation/".$_GET['file'].".gif";
 
//Load the image
    header("Content-Type: image/gif");
    echo readfile($gif_file);
    die();
Ok so the image is generated just fine with my code but the problem is that it has to reload and redownload the image every time the browser is refreshed. Is there anyway I could force all browsers to cache the php generated image so when the generated url is called the next time it pulls from cache instead of pulling from the photobucket account?
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: Is it possible to force an browser to cache a php image?

Post by BornForCode »

In this situation i am afraid that is not possible. I think that the main cause is that you are getting images on the fly from a different location. When the browser cache system checks with your site for changes it will always receive a negative answer regarding such resources, which will trigger re-download.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Is it possible to force an browser to cache a php image?

Post by Eric! »

You can't force the browser, but you can make sure you send the proper age, expires and cache control headers to encourage the client to cache and use the cache for images.

Here's a good overview
http://www.mnot.net/cache_docs/

See http://php.net/manual/en/function.header.php
Post Reply