【问题标题】:Caching Images generated by PHP script缓存 PHP 脚本生成的图像
【发布时间】:2013-03-19 07:27:48
【问题描述】:

嗯,如标题所示,我想将我的 php 脚本生成的资源缓存在用户的浏览器上。我想这样做是因为我想从服务器端动态地为一些图像提供缩略图。我有这个.htaccess 文件,里面有这些内容。

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,在这个实现中,内容是永远不会改变的,所以我想缓存图像。 任何类型的想法将不胜感激。谢谢。

【问题讨论】:

    标签: php image caching browser-cache


    【解决方案1】:

    使用这个

    file_put_contents($filename, $imagedata);
    

    它将图像写入服务器,并且由于您的 htaccess 文件只会在文件不存在时运行图像生成,因此它只会写入一次文件,然后从那时起提供缓存版本。

    【讨论】:

    • 自动删除呢?实际上我不希望在我的服务器中有大量文件包含不必要的文件。我希望它们是实时生成的
    • 您不能将它们缓存实时生成。这是一个或另一个。
    • 我们可以实时缓存它们,如果你不相信,然后删除上面的标题行并保留这些,最后我准备好了。 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; }
    • 对,我的示例也实时生成和缓存文件。但问题是,当浏览器加载文件时,它要么从缓存中加载(无论是本地的还是服务器上的),或者是实时生成的,但不能两者兼而有之。
    • 我不想在我的磁盘空间中创建无用的图像,所以我想缓存它们,并且之前发布的评论对我有用...
    猜你喜欢
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多