Jump to content


Photo

Caching pics


  • Please log in to reply
4 replies to this topic

#1 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 August 2006 - 09:44 AM

Hello,

Is this the right way to cache a picture, so the next time the picture is loaded it will come up faster?

<?php
//This file is called picture.php
header("Cache-Control: max-age=604800, must-revalidate"); //one week caching
header("Content-type: image/jpg");
include("image.jpg");
?>

In the page I want to displayed the cached pic called image.jpg write:
<img src="picture.php">


Or is there another way caching pics without having to create new caching files...?


Thanks alot,
Orio.
Think you're smarty?

(Gone until 20 to November)

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 August 2006 - 03:58 PM

Anyone?

I thought of making a file called imageshower.php, and it'll look like this:
<?php

//<validation> (if you have any offers to improove- be my guest)
$formats=array("jpg","gif"); //the formats I use
if(isset($_GET['pic']) || !is_file($_GET['pic']))
 {die("No direct access.");}
$explode=explode(".",$_GET['pic']);
if(!in_array($explode[count($explode)-1], $formats)
 {die("No direct access.");}
//</validation>

//<cache>
header("Cache-Control: max-age=604800, must-revalidate"); //one week caching
//</cache>

//<show image>
header("Content-type: image/jpg");
include("image.jpg");
//</show image>

?>

Then for every pic I want to cache and show, I'll use <img src="imageshower.php?pic=pic.jpg"> for example.

But I am not sure if this is right. And I have no way to check if it was cached or not...

Thanks again,
Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 August 2006 - 08:07 PM

Anyone? Please?

Orio.
Think you're smarty?

(Gone until 20 to November)

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 29 August 2006 - 08:59 PM

Can I ask why you would want to do this?  Most browsers automatically handle the cacheing of images for you.

Info: PHP Manual


#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 August 2006 - 09:06 PM

Well I didnt know that :D Thanks for telling me that  :)
Either way, is this right? That's the way to cache pics? Or is it wrong/there's a better way doing it.

Thx,
Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users