Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP使用带宽限制和缓存提供图像?_Php_Image_Caching_Bandwidth_Bandwidth Throttling - Fatal编程技术网

PHP使用带宽限制和缓存提供图像?

PHP使用带宽限制和缓存提供图像?,php,image,caching,bandwidth,bandwidth-throttling,Php,Image,Caching,Bandwidth,Bandwidth Throttling,是指向文件的带宽限制PHP脚本示例的链接。我看到了一个我可以做的改进,这与我以后会做的无关,但除此之外。。。如何使用此脚本生成另一个脚本,该脚本返回带宽受限的图像,但永久缓存图像?图像永远不会更改。我认为您所要求的只是修改脚本以处理图像并缓存内容。这应该可以做到,在我可能有任何小错误之前: <?php $file = "yourimage.jpg"; // file to be send to the client $speed = 8.5; // 8,5 kb/s download r

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

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

<?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);

}