2013-03-28 35 views
0

好吧,正如标题中给出的那样,我想要将用我的php脚本生成的资源缓存在用户的浏览器上。我想这样做,因为我想动态地为服务器端的一些图像提供缩略图图像。我有这个.htaccess文件,其中包含这些内容。由PHP脚本生成的缓存图片

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

而index.php文件包含这些代码。

<?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; 
} 
} 
?> 

即使在此之后,当我去到开发人员模式,看看谷歌浏览器的网络选项卡中,状态信息是:200 OK
可以做些什么来缓存此脚本生成的所有图像?因为对于特定的url,在这个实现中,内容永远不会改变,所以我想缓存图像。 任何形式的想法将不胜感激。谢谢。

回答

0

使用本

file_put_contents($filename, $imagedata); 

它将图像写入服务器和因为你的htaccess文件将只运行图像生成,如果该文件不存在,只会写一次文件,然后服务于缓存从那时起版本。

+0

什么是自动删除?其实我不希望在我的服务器中有大量的不必要文件clustors ..我希望它们能够实时生成 –

+0

你不能让它们实时生成缓存的**和**。它是一个或另一个。 – chrislondon

+0

我们可以实时缓存它们,如果你不相信,那么删除上面的标题行并保留这些,最后我准备好了。 (“Last-Modified:”.date(“D,d M Y H:i:s”,time() - 360000)); ('Expires:'.gmdate('D,d M Y H:i:s \ G \ M \ T',time()+ 3600000)); //如果浏览器具有此映像的缓存版本,则浏览器将发送一个$ _SERVER ['HTTP_IF_MODIFIED_SINCE'],如果它具有缓存副本 if(isset($ _ SERVER ['HTTP_IF_MODIFIED_SINCE'])){ //如果浏览器具有此映像的缓存版本,发送304 header('Last-Modified:'。$ _ SERVER ['HTTP_IF_MODIFIED_SINCE'],true,304); \t exit; }' –