调用缓存图像时哪个更高效/更快?

发布于 2024-09-01 14:02:15 字数 656 浏览 4 评论 0原文

我用 php 做了一个图像缩放器。调整图像大小时,它会缓存具有新尺寸的新 jpg 文件。下次您调用确切的 img.php?file=hello.jpg&size=400 时,它会检查新的 jpg 是否已创建。

  1. 如果尚未创建,它将创建文件,然后打印输出(很酷)。
  2. 如果它已经存在,则不需要生成新文件,而是只调用已经缓存的文件。

我的问题是关于第二种情况。这些哪个更快?

  1. 重定向: header('Location: cache/hello_400.jpg');die();
  2. 抓取数据并打印缓存文件: $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.

  1. If it has NOT been created yet, it creates the file and then prints the output (cool).
  2. 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?

  1. redirecting: header('Location: cache/hello_400.jpg');die();
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陪你到最终 2024-09-08 14:02:15

我会选择永远不将数据打印到浏览器。这两种情况都应该对生成的图像进行永久重定向。除非图像尚不存在,否则它将在发送 Location 标头之前创建。

编辑:

只是为了清楚我所说的永久重定向的含义......

header('HTTP/1.1 301 Moved Permanently'); 
header('Location: http://path/to/image'); 

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...

header('HTTP/1.1 301 Moved Permanently'); 
header('Location: http://path/to/image'); 
李不 2024-09-08 14:02:15

也许您可以执行以下操作:

  1. 为这些图像设置一些目录。
  2. 链接到此目录中的图像 (
  3. 如果图像尚不存在,请将您的网络服务器设置为重定向到您的 php 脚本。如果在 Apache 上,一个简单的 .htaccess 就可以了。在 PHP 中,您可以通过 $_SERVER["REQUEST_URI"] 判断您
  4. 的脚本应保存并回显该图像的大小 。但仅在第一次调用)。

这样,您可以获得一些好处:

  • 图像被缓存(在代理或浏览器中),因为
  • 不必为每个请求调用 PHP 来重定向 。或输出静态数据,
  • If-modified-since 的实现和其他与缓存相关的标头留给网络服务器,
  • 并且链接看起来更好:-)

例如 .htaccess 在 /img/ressized 文件夹中。 :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /img.php [L]

Maybe you could do the following:

  1. Set some directory for these images.
  2. Link to images in this directory (<a href="/img/resizable/hello_400.jpg>).
  3. Set your webserver to redirect to your php script if the image doesn't exist yet. If you are on Apache, a simple .htaccess will do. In PHP, you have $_SERVER["REQUEST_URI"] from which you can tell which image you should resize.
  4. Your script saves and echoes the image (but is called only for the first time).

This way, your get some benefits:

  • The image is cached (in a proxy or a browser) as any other static file.
  • PHP doesn't have to be called for every request just to redirect or output statical data.
  • You leave the implementation of If-modified-since and other cache-related headers to the webserver.
  • And the links look nicer :-)

Example .htaccess in your /img/resizable folder:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /img.php [L]
半城柳色半声笛 2024-09-08 14:02:15

还有其他方法可以改进吗?

是的。

有一种方法只发送 HTTP 标头:条件获取。
您可以查看类似的脚本 http://shiftingpixel.com /2008/03/03/smart-image-resizer/,用于实现

Any other ways to improve this?

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

掩饰不了的爱 2024-09-08 14:02:15

第三个(更强大)选项:将图像缓存在数据库的二进制字段中,并在数据库中查询它。

实施所有三个解决方案并对其进行基准测试。

我猜测第一个选项(重定向)在现实世界中将是最慢的,因为它需要与第二个选项(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.

美人骨 2024-09-08 14:02:15

如果可能的话,您还可以实现一个函数来直接在 html 中设置缓存图像的 url,例如:

<img src="<?php getImageUrl('hello.jpg', 400); ?>" />

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:

<img src="<?php getImageUrl('hello.jpg', 400); ?>" />

getImageUrl() will return the url of the cached image if it exists else it will return the url to dynamically generate the resized image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文