1

Well, as given in the title, I want to cache the resource generated by my php script on the user's browser. I want to do so because I want to serve thumbnail images for some images from the server side dynamically. I have this .htaccess file with these contents in it.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?image_id=$1 [QSA,L]

And index.php file contains these codes.

<?php

      header('Last-Modified: ' .gmdate('D, d M Y H:i:s',time()-3600) . ' GMT');


$image_id=$_GET['image_id'];
$chunk=  explode("/", $image_id);
$destination_height=$chunk[0];
$destination_width=$chunk[1];
$image=$chunk[2];
if(file_exists($image))
{
$extension=explode(".",$image);
$extension=end($extension);
switch ($extension) {
    case "jpg":
        $source_image=  imagecreatefromjpeg($image);
        break;
case "jpeg":
        $source_image=  imagecreatefromjpeg($image);
        break;
    case "gif":
        $source_image=  imagecreatefromgif($image);
        break;
    case "png":
        $source_image=  imagecreatefrompng($image);
        break;    
    default:
        break;
}
        $source_height = imagesy($source_image);
        $source_width = imagesx($source_image);
        if($destination_height=="full")
        {
            $destination_height=$source_height;
        }
        if($destination_width=="full")
        {
            $destination_width=$source_width;
        }
        $destination_image=  imagecreatetruecolor($destination_width, $destination_height);
  $dst_x=0;
     $dst_y=0;
     $src_x=0;
  $src_y=0;
  imagecopyresized($destination_image,$source_image,$dst_x,$dst_y,$src_x,$src_y,$destination_width,$destination_height,$source_width,$source_height);
switch ($extension) {
    case "jpg":
   imagejpeg($destination_image);
   header("content-type:image/jpeg");
        break;
  case "jpeg":
   header("content-type:image/jpeg");
   imagejpeg($destination_image);
        break;
    case "gif":
   header("content-type:image/gif");
   imagegif($destination_image);
        break;
    case "png":
   header("content-type:image/png");
   imagepng($destination_image);
        break;
    default:
    break;
}
}
?>

Even after this when I go to developer mode and see the network tab on google chrome, the status information is : 200 OK
What can be done to cache all the images generated by this script? Because for a particular url, in this implementation, the content is never changed, so I want to cache the images. Any sort of ideas will be appreciated. Thanks.

Nicholas Wild
  • 629
  • 1
  • 6
  • 17

1 Answers1

0

use this

file_put_contents($filename, $imagedata);

It will write the image to the server and since your htaccess file will only run the image generating if the file doesn't exist it will only write files once and then serve the cached versions from then on.

chrislondon
  • 12,487
  • 5
  • 26
  • 65
  • What about auto deletion? Actually I don't want huge clustors of file having unnecessary files in my server.. I want them to be generated in real time – Nicholas Wild Mar 28 '13 at 18:21
  • You can't have them cached **and** generated in real-time. It's one or the other. – chrislondon Mar 28 '13 at 19:35
  • we can cache them in real-time, if you don't believe, then remove that above header line and keep these, finally I got this ready. `header("Last-Modified: ".date("D, d M Y H:i:s",time()-360000)); header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600000)); // the browser will send a $_SERVER['HTTP_IF_MODIFIED_SINCE'] if it has a cached copy if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ // if the browser has a cached version of this image, send 304 header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304); exit; }` – Nicholas Wild Mar 29 '13 at 17:00
  • Right, my example generates and caches the file in realtime too. But the thing is when a browser loads a file it either loads it from cache (whether local or on the server) or it is generated in real time but it can't be both. – chrislondon Mar 30 '13 at 15:51
  • I don't want to create useless images in my disk spaces, So I wanted to have them cached, and the earlier comment which posted, is working for me... – Nicholas Wild Apr 02 '13 at 15:53