如何缓存用PHP动态生成的图像? [英] How to Cache Images Dynamically-Generated with PHP?

查看:120
本文介绍了如何缓存用PHP动态生成的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网站上,我使用PHP的ImageMagick扩展动态生成图像。我使用.htaccess文件将以.jpg结尾的文件重写为用于动态生成的php文件:

  RewriteRule ^( [^ _] +)。jpg index.php?filename = $ 1 

然后我创建图像为JPG格式的文件:

  $ img-> setImageFormat('jpeg'); 

在回显图片之前,我发送了以下标题:

  header(Content-Type:image / jpeg); 
标题('过期:星期一,2027年7月26日05:00:00 GMT');
标题(Pragma:cache);
头(Cache-Control:max-age = 290304001);

然而,使用Chrome的缓存?扩展名,我可以看到动态生成的图像被缓存 。此外,尽管已将标头设置为 Content-Type:image / jpeg ,Google Chrome浏览器的控制台却说 Resource解释为Document,但是以MIME类型image / jpeg 传输c $ c $。

所以我试着用.htaccess缓存,使用以下内容:

 < filesMatch\。(jpg)$> 
Header set Cache-Controlmax-age = 290304001,public
< / filesMatch>

但是,没有工作。



所以我试着在.htaccess条件中添加 php 扩展名:

 < filesMatch\。(jpg | php)$> 
Header set Cache-Controlmax-age = 290304001,public
< / filesMatch>

finally 起作用并且图像被缓存。所以我放心,我得到它的工作,但这种反复试验方法让我感到不安,我不明白为什么PHP缓存头不起作用。

问题:1)通过.htaccess文件进行缓存是否缓存用PHP动态生成的图像缓存的正确方法?



2)为什么PHP缓存头被无效


3)为什么是当标题设置为 image / jpeg ?时,Chrome的控制台会将资源解释为文档但用MIME类型image / jpeg p>

4)是否有更好的方式来缓存PHP动态生成的图像(也许使用纯PHP而不带.htaccess)?


$ b $ p

header(Last-Modified:Fri)

,2016年5月13日13:16:19 GMT);



日期格式如 RFC2616 。要以这种格式生成日期,您可以使用\DateTime :: RSS常数,例如
$ b echo(new \DateTime()) - >格式(\DateTime :: RSS);



echo date(\DateTime :: RSS) code>



当然,每个请求都不应该改变日期。




On a site, I am dynamically-generating images using PHP's ImageMagick extension. I'm using the .htaccess file to rewrite files ending in .jpg to a php file for dynamic generation:

RewriteRule ^([^_]+).jpg index.php?filename=$1

I am then creating images as JPG inside the PHP file:

$img->setImageFormat('jpeg');

Before echoing the image, I am sending out the following headers:

header("Content-Type: image/jpeg");
header('Expires: Mon, 26 Jul 2027 05:00:00 GMT');
header("Pragma: cache");
header("Cache-Control: max-age=290304001");

However, using Chrome's "Is it Cached?" extension, I can see that the dynamically-generated images are not being cached. Furthermore, Google Chrome's console is saying "Resource interpreted as Document but transferred with MIME type image/jpeg" despite having set the header as Content-Type: image/jpeg.

So I tried caching with .htaccess, using the following:

<filesMatch "\.(jpg)$">
Header set Cache-Control "max-age=290304001, public"
</filesMatch>

But that also did not work.

So I then tried adding the php extension to the .htaccess condition:

<filesMatch "\.(jpg|php)$">
Header set Cache-Control "max-age=290304001, public"
</filesMatch>

And that finally worked and the images got cached. So I'm relieved I got it to work, but this trial-and-error approach leaves me uneasy and I don't understand why the PHP cache headers didn't work.

Questions:

1) Is doing the caching via the .htaccess file the correct way to cache images dynamically-generated with PHP?

2) Why are the cache headers being sent out by PHP having no effect?

3) Why is Chrome's console saying "Resource interpreted as Document but transferred with MIME type image/jpeg" when the header has been set to image/jpeg?

4) Is there a better way of caching dynamically-generated images with PHP (perhaps using pure PHP without .htaccess)?

解决方案

Try adding the Last-Modified header:

header("Last-Modified: Fri, 13 May 2016 13:16:19 GMT");

The format of the date as in RFC2616. To generate date in this format you can use \DateTime::RSS constant, e.g.

echo (new \DateTime())->format(\DateTime::RSS);

or

echo date(\DateTime::RSS);

Of course the date shouldn't change with each request.

Hope this helps!

这篇关于如何缓存用PHP动态生成的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆