如何使用PHPGD实现缓存

如何使用PHPGD实现缓存,php,gd,Php,Gd,我想缓存我的图库的图像。使用GD生成每个页面加载的图像都会占用大量内存,因此我计划为使用GD生成的php脚本生成的图像创建一个缓存图像。创建缓存的最佳方法是什么?将其保存到磁盘。Web服务器将负责缓存。您考虑过使用吗?它有大量的图像生成和缓存选项 $mime_type = "image/png"; $extension = ".png"; $cache_folder = "cache"; $hash = md5($unique . $things . $for . $each . $image

我想缓存我的图库的图像。使用GD生成每个页面加载的图像都会占用大量内存,因此我计划为使用GD生成的php脚本生成的图像创建一个缓存图像。创建缓存的最佳方法是什么?

将其保存到磁盘。Web服务器将负责缓存。

您考虑过使用吗?它有大量的图像生成和缓存选项

$mime_type = "image/png";
$extension = ".png";
$cache_folder = "cache";

$hash = md5($unique . $things . $for . $each . $image);
$cache_filename = $cache_folder . '/' . $hash . $extension;

//Is already it cached?
if($file = @fopen($cache_filename,'rb')) {  
    header('Content-type: ' . $mime_type);
    while(!feof($file)) { print(($buffer = fread($file,4096))); }
    fclose($file);
    exit;
} else {
//Generage a new image and save it
   imagepng($image,$cache_filename); //Saves it to the given folder

//Send image
   header('Content-type: ' . $mime_type);
   imagepng($image);
}

我认为从缓存读取文件时不需要进行任何迭代,只需调用readfile()即可。例如:

if (file_exists($image_path)) {
    // send the cached image to the output buffer (browser)
    readfile($image_path);
}else{
    // create a new image using GD functions
...
全文如下:


我通常在哈希表中找到缓存。Web服务器也负责哈希吗?你能解释一下或举个例子说明Web服务器是如何处理缓存的吗。它被设计为缓存,因为它支持HTTP服务器和HTTP协议缓存。它设法计算一个散列。