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

Discussion in 'PHP' started by RottNKorpse, Jul 4, 2009.

  1. #1
    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:
    http://i87.photobucket.com/albums/k126/WrestleMation/Brain_Rush_S01E01_DotNXT_DSR_XviD-E.gif
    Code (markup):
    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.
    http://wrestlemation.com/fa_Brain_Rush_S01E01_DotNXT_DSR_XviD-E.gif
    Code (markup):
    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...
    //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();
    Code (markup):
    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?
     
    RottNKorpse, Jul 4, 2009 IP
  2. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    why you do that?

    by doing this:
    echo readfile($gif_file);

    you are spending more bandwidth, than if you host the images on your server

    you can redirect the image to photobucket, that way photobucket send the date headers and the browser load the images from the cache
    $gif_file = "http://i87.photobucket.com/albums/k126/WrestleMation/".$_GET['file'].".gif";
    header("Location: $gif_file");
    die();
    Code (markup):
    or you can get the last modified date of the file with curl and send it your self
     
    emed, Jul 5, 2009 IP
  3. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think some .htaccess! :D :)
     
    Wrighty, Jul 5, 2009 IP