简体   繁体   English

使用PHP缓存生成的图像

[英]Caching generated images with PHP

I am trying to cache images which have been generated. 我正在尝试缓存已生成的图像。 You create a image by accessing the file by resize.php?width=x&height=y. 您可以通过resize.php?width = x&height = y访问该文件来创建图像。 If the image of that width and height does not exist I use imagemagick to create it. 如果该宽度和高度的图像不存在,我使用imagemagick来创建它。 However if it does exist it is served to the visitor. 但是,如果确实存在,则会向访问者提供服务。

The !file_exists($name) check works fine so processing is not done when its not needed. !file_exists($ name)检查工作正常,因此在不需要时不进行处理。 However the images still take a while to load. 但是图像仍需要一段时间才能加载。

Is my approach of readfile incorrect or have I set headers incorrectly? 我的readfile方法是不正确还是我设置了错误的标题?

if (!file_exists($name)) {
 //image processing here
}
header("Content-Type: image/png");
header("Expires: Sat, 25 Jul 2020 10:00:00 GMT");
readfile($name);

Thanks. 谢谢。

If i had to do this, i'd proccess the image if it does not exist save it in some web accessible folder. 如果我必须这样做,我将处理该图像,如果它不存在保存在一些Web可访问文件夹中。 If the file exists just redirect to it header( 'Location: http://www.yoursite.com/path/to/existing/file.png ' ) 如果文件存在只是重定向到它的标题('Location: http//www.yoursite.com/path/to/existing/file.png ')

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

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