调用缓存图像时哪个更高效/更快?
我用 php 做了一个图像缩放器。调整图像大小时,它会缓存具有新尺寸的新 jpg 文件。下次您调用确切的 img.php?file=hello.jpg&size=400 时,它会检查新的 jpg 是否已创建。
- 如果尚未创建,它将创建文件,然后打印输出(很酷)。
- 如果它已经存在,则不需要生成新文件,而是只调用已经缓存的文件。
我的问题是关于第二种情况。这些哪个更快?
- 重定向:
header('Location: cache/hello_400.jpg');die();
- 抓取数据并打印缓存文件:
$data = file_get_contents('cache/hello_400.jpg') ; header('内容类型:'.$mime); header('内容长度:'.strlen($data)); echo $data;
还有其他方法可以改进吗?
如果有人想要生成的代码,请查看: http://egobits.com/misc/img.phps
感谢大家的帮助!
i made an image resizer in php. When an image is resized, it caches a new jpg file with the new dimensions. Next time you call the exact img.php?file=hello.jpg&size=400 it checks if the new jpg has already been created.
- If it has NOT been created yet, it creates the file and then prints the output (cool).
- If it ALREADY exists, no new file needs to be generated and instead, it just calls the already cached file.
My question is regarding the second scenario. Which of these is faster?
- redirecting:
header('Location: cache/hello_400.jpg');die();
- grabbing data and printing the cached file:
$data = file_get_contents('cache/hello_400.jpg'); header('Content-type: '.$mime);
header('Content-Length: '.strlen($data));
echo $data;
Any other ways to improve this?
If someone wants the generated code, check this out:
http://egobits.com/misc/img.phps
Thanks to all for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会选择永远不将数据打印到浏览器。这两种情况都应该对生成的图像进行永久重定向。除非图像尚不存在,否则它将在发送 Location 标头之前创建。
编辑:
只是为了清楚我所说的永久重定向的含义......
I would opt for never printing the data to the browser. Both scenarios should throw a permanent redirect to the generated image. Except if the image doesn't exist yet, it is created before the Location header is sent.
Edit:
Just to be clear about what I mean by permanent redirect...
也许您可以执行以下操作:
$_SERVER["REQUEST_URI"]
判断您这样,您可以获得一些好处:
If-modified-since
的实现和其他与缓存相关的标头留给网络服务器,例如 .htaccess 在 /img/ressized 文件夹中。 :
Maybe you could do the following:
<a href="/img/resizable/hello_400.jpg>
).$_SERVER["REQUEST_URI"]
from which you can tell which image you should resize.This way, your get some benefits:
If-modified-since
and other cache-related headers to the webserver.Example .htaccess in your /img/resizable folder:
是的。
有一种方法只发送 HTTP 标头:条件获取。
您可以查看类似的脚本 http://shiftingpixel.com /2008/03/03/smart-image-resizer/,用于实现
Yes.
There is a way to send nothing but just an HTTP header: a conditional get.
You can take a look at the similar script, http://shiftingpixel.com/2008/03/03/smart-image-resizer/, for the implementation
第三个(更强大)选项:将图像缓存在数据库的二进制字段中,并在数据库中查询它。
实施所有三个解决方案并对其进行基准测试。
我猜测第一个选项(重定向)在现实世界中将是最慢的,因为它需要与第二个选项(file_get_contents)一样多的工作,但涉及第二个请求和更多的开销。
As a third (more powerful) option: cache the image in a binary field in a database, and query the database for it.
Implement all three solutions and benchmark them.
I'm going to guess that the first option (redirect) will be the slowest in the real world, because it requires just as much effort as the second option (file_get_contents), but involves a second request and more overhead.
如果可能的话,您还可以实现一个函数来直接在 html 中设置缓存图像的 url,例如:
getImageUrl() 将返回缓存图像的 url(如果存在),否则它将返回 url 以动态生成调整图像大小。
If possible in your case, you can also implement a function to directly set the url of the cached image in your html like:
getImageUrl() will return the url of the cached image if it exists else it will return the url to dynamically generate the resized image.