PHP 提供带有限制带宽和缓存的图像?

发布于 2025-01-06 13:01:56 字数 207 浏览 4 评论 0原文

此处是带宽限制 PHP 脚本示例的链接一个文件。我看到了一个我可以做的改进,这与我稍后会做的无关,但除此之外...您如何使用此脚本来制作另一个脚本,该脚本返回带宽受限制的图像,但永久缓存图像?形象永远不会改变。

Here is a link to an example bandwidth throttling PHP script for a file. I see an improvement I can make that's unrelated that I'll make myself later, but other than that... How could you use this script to make another script that returns an image with throttled bandwidth, but caches the images forever? The image will never change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

沦落红尘 2025-01-13 13:01:56

我认为您所要求的只是修改该脚本以处理图像并缓存内容。这应该可以做到,等待我可能遇到的任何小错误:

<?php

$file = "yourimage.jpg"; // file to be send to the client
$speed = 8.5; // 8,5 kb/s download rate limit

// if $file is coming from get, I would use this to prevent against a nullbyte attack:
$file = str_replace(chr(0), '', $file);

if (file_exists($file) && is_file($file)) {
    header("Expires: ".gmdate('D, d M Y H:i:s', time()+3600*24*3000).'GMT'); // expires in 3000 days. 
    header("Pragma: cache");
    header("Content-Type: image/jpeg"); // needs to be changed for the file type.
    header("Content-Length: ".filesize($file));
    header("Cache-Control: max-age=" . 3600*24*3000);

    flush();

    $fd = fopen($file, "r");
    while(!feof($fd)) {
         echo fread($fd, round($speed*1024));
        flush();
        sleep(1);
    }
    fclose ($fd);

}

I think all you were asking was to modify that script to work with an image and cache the content. This should do it, pending any minor errors I may have:

<?php

$file = "yourimage.jpg"; // file to be send to the client
$speed = 8.5; // 8,5 kb/s download rate limit

// if $file is coming from get, I would use this to prevent against a nullbyte attack:
$file = str_replace(chr(0), '', $file);

if (file_exists($file) && is_file($file)) {
    header("Expires: ".gmdate('D, d M Y H:i:s', time()+3600*24*3000).'GMT'); // expires in 3000 days. 
    header("Pragma: cache");
    header("Content-Type: image/jpeg"); // needs to be changed for the file type.
    header("Content-Length: ".filesize($file));
    header("Cache-Control: max-age=" . 3600*24*3000);

    flush();

    $fd = fopen($file, "r");
    while(!feof($fd)) {
         echo fread($fd, round($speed*1024));
        flush();
        sleep(1);
    }
    fclose ($fd);

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