简体   繁体   English

缓存由PHP脚本生成的图像

[英]Caching Images generated by PHP script

Well, as given in the title, I want to cache the resource generated by my php script on the user's browser. 好了,正如标题中所述,我想在用户的浏览器中缓存由我的php脚本生成的资源。 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. 我有此.htaccess文件,其中包含这些内容。

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. 并且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;
}
}
?>

Even after this when I go to developer mode and see the network tab on google chrome, the status information is : 200 OK 即使在此之后,当我进入开发人员模式并在google chrome上看到“网络”标签时,状态信息仍为: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. 因为对于特定的URL,在此实现中,内容从未更改,因此我想缓存图像。 Any sort of ideas will be appreciated. 任何想法都将不胜感激。 Thanks. 谢谢。

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. 它将图像写入服务器,并且由于您的htaccess文件仅在文件不存在时才运行图像生成,因此它将仅写入一次文件,然后从此开始提供缓存的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM