Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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_Http_Image_Resize - Fatal编程技术网

Php 动态调整图像缓存大小

Php 动态调整图像缓存大小,php,http,image,resize,Php,Http,Image,Resize,我使用简单的PHP代码(zend framework)动态调整jpg图像的大小 问题是我总是以HTTP200状态结束,而不是304状态,并允许浏览器缓存图像 我无法获取apache标头,函数\u存在('apache\u请求\u标头')为false,并且在服务器变量中我只有 'HTTP_ACCEPT' => 'image/png,image/*;q=0.8,*/*;q=0.5', 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*

我使用简单的PHP代码(zend framework)动态调整jpg图像的大小

问题是我总是以HTTP200状态结束,而不是304状态,并允许浏览器缓存图像

我无法获取apache标头,
函数\u存在('apache\u请求\u标头')
为false,并且在服务器变量中我只有

 'HTTP_ACCEPT' => 'image/png,image/*;q=0.8,*/*;q=0.5',
  'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
  'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
  'HTTP_CACHE_CONTROL' => 'max-age=0',
  'HTTP_CONNECTION' => 'keep-alive',
  'HTTP_COOKIE' => '***',
  'HTTP_HOST' => 'automobi.li',
  'HTTP_KEEP_ALIVE' => '300',
  'HTTP_REFERER' => 'http://automobi.li/oglas/Opel+Astra/2',
  'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 FirePHP/0.4',
我要寄

    $lastModified = filemtime($path);
  $etag = md5_file($path);

  $this->getResponse()->setHeader('Content-type', 'image/jpeg');
  $this->getResponse()->setHeader('Cache-Control', 'public');
  $this->getResponse()->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $lastModified) . ' GMT');
  $this->getResponse()->setHeader('Cache-Control', 'max-age=86400, must-revalidate');
  $this->getResponse()->setHeader('Expires', gmdate('D, d M Y H:i:s', time() + 86400 ) . ' GMT');
  $this->getResponse()->setHeader('ETag', $etag);
我希望HTTP\u IF\u修改了服务器变量中的\u,或者类似的东西,以便我可以这样做

  if ($this->getRequest()->getHeader('IF_MODIFIED_SINCE') == $lastModified) {
$this->getResponse()->setHttpResponseCode(304);

  } else {
   $w = (int) $this->_getParam('w');
   $h = (int) $this->_getParam('h');
   $image->resize($path, $w, $h);   
  }
有什么想法吗?

最后一次修改的值,如果修改了,因为不需要比较值是否相等。它们都代表着时代。如果上次修改的时间大于if-Modified-Since时间(因此上次修改在if-Modified-Since之后花费时间),则满足条件并发送304

因此,您需要解析If-Modified-Since值(try),并在上次修改时间大于If-Modified-Since时间时比较这些值:

固定

$this->getResponse()->setHeader('Content-type', 'image/jpeg');
    $this->getResponse()->setHeader('Expires', '', true);
    $this->getResponse()->setHeader('Cache-Control', 'public', true);
    $this->getResponse()->setHeader('Cache-Control', 'max-age=3800');
    $this->getResponse()->setHeader('Pragma', '', true);
    $this->getResponse()->setHeader('ETag', $etag);

    if ($etag == $this->getRequest()->getHeader('If-None-Match')) {
        $this->getResponse()->setHttpResponseCode(304);
    } else {
        $w = (int) $this->_getParam('w');
        $h = (int) $this->_getParam('h');
        $image->resize($path, $w, $h);
    }
多亏了

当我加上

$this->getResponse()->setHeader('Expires', '', true);
    $this->getResponse()->setHeader('Cache-Control', 'public', true);
    $this->getResponse()->setHeader('Cache-Control', 'max-age=3800');
    $this->getResponse()->setHeader('Pragma', '', true);
如果请求中没有出现匹配头,这有助于我将其与etag匹配

@秋葵哦,我很明白:)


感谢所有试图提供帮助的人。

这段代码可能会有所帮助,它一直对我很有用,我尝试将缓存控制集成到Zend Framework中动态生成的图像中:

if ($mod_since = $this->getRequest()->getHeader('If-Modified-Since')) {

    $request_modified = explode(';', $mod_since);
    $request_modified = strtotime($request_modified[ 0 ]);

}
if ($this->getFiletime($path, $filename) > 0 && $this->getFiletime($path, $filename) <= $request_modified) {

    // Image has not changed since last call, force Browser to reload from cache.
    header('HTTP/1.1 304 Not Modified');
    exit();

} else {

    // Image has changed or browser has no cache
    $mimetype = $this->getMIMEType($path, $filename);
    $format = substr(strstr($mimetype, "/"), 1);
    $this->getResponse()->setHeader('Content-type', $mimetype);
    $expires = 60 * 60 * 24 * 3;
    $exp_gmt = gmdate("D, d M Y H:i:s", time() + $expires) . " GMT";
    $mod_gmt = gmdate("D, d M Y H:i:s", $this->getFiletime($path, $filename)) . " GMT";

    $imagedata = $this->_processor->processImage($path, $filename, $w, $h, $crop, $format);

    // Send Headers for Browser Caching Control
    $this->getResponse()->setHeader('Expires', $exp_gmt, true);
    $this->getResponse()->setHeader('Last-Modified', $mod_gmt, true);
    $this->getResponse()->setHeader('Cache-Control', 'public, max-age=' . $expires, true);
    $this->getResponse()->setHeader('Pragma', '!invalid', true);
    $this->getResponse()->setHeader('Content-Length', strlen($imagedata), true);
    $this->getResponse()->setHeader('ETag', md5($imagedata), true);

    // Save image in Zend Framework's server side Cache or get it from there
    $cache_id = str_replace(".", '', $path . "_" . $filename . "_" . $w . "_" . $h . "_" . $crop . "_" . $format);

    if (!$result = $this->_cache->load($cache_id)) {
        $result = $imagedata;
        $this->_cache->save($result, $cache_id, array('image', $path . '_' . str_replace(".", '', $filename)));
    }
    echo $result;
}
if($mod\u-since=$this->getRequest()->getHeader('if-Modified-since')){
$request_modified=explode(“;”,$mod_-since);
$request_modified=strottime($request_modified[0]);
}
如果($this->getFiletime($path,$filename)>0&&$this->getFiletime($path,$filename)getMIMEType($path,$filename);
$format=substr(strstrstr($mimetype,“/”),1);
$this->getResponse()->setHeader('Content-type',$mimetype);
$expires=60*60*24*3;
$exp_gmt=gmdate(“D,dmy H:i:s”,time()+$expires)。“gmt”;
$mod_gmt=gmdate(“D,dmy H:i:s,$this->getFiletime($path,$filename))“gmt”;
$imagedata=$this->\u处理器->processImage($path、$filename、$w、$h、$crop、$format);
//发送浏览器缓存控件的标题
$this->getResponse()->setHeader('Expires',$exp\u gmt,true);
$this->getResponse()->setHeader('Last-Modified',$mod_gmt,true);
$this->getResponse()->setHeader('Cache-Control','public,max-age='。$expires,true);
$this->getResponse()->setHeader('Pragma','!invalid',true);
$this->getResponse()->setHeader('Content-Length',strlen($imagedata),true);
$this->getResponse()->setHeader('ETag',md5($imagedata),true);
//将映像保存在Zend Framework的服务器端缓存中或从那里获取
$cache_id=str_replace(“.”,“$path.”,“$filename.”,“$w.”,“$h.”,“$crop.”,“$format”);
如果(!$result=$this->\u缓存->加载($cache\u id)){
$result=$imagedata;
$this->_cache->save($result,$cache_id,array($image',$path'.'.'.'.str_replace(“.”,'.$filename));
}
回声$结果;
}

谢谢,我知道,我写得很快,也不关心细节。如果你查看$\u服务器变量,你会发现如果没有设置\u MODIFIED\u SINCE,正如我已经写的,apache\u请求\u头没有定义函数,这是主要问题,我没有这些数据!所以我正在寻找解决方案,如何确定它。如果
$\u服务器['IF\u MODIFIED\u SINCE']
未设置,则客户端未发送“If Modified Since”标头字段,您无法使用它来确定客户端的表示形式是否仍然是新的。确定,但使用什么替代方法?必须有某种方法来确定客户端缓存。@auto:您就是不能这样做。丢失该信息可能有多种原因。例如充足,因为这是该资源的第一个请求,或者客户端不支持缓存。@Gumbo我不同意。1.即使不是第一个请求,也会发生这种情况。2.客户端支持缓存,因为它缓存静态资源(其他图像、js和css)。
if ($mod_since = $this->getRequest()->getHeader('If-Modified-Since')) {

    $request_modified = explode(';', $mod_since);
    $request_modified = strtotime($request_modified[ 0 ]);

}
if ($this->getFiletime($path, $filename) > 0 && $this->getFiletime($path, $filename) <= $request_modified) {

    // Image has not changed since last call, force Browser to reload from cache.
    header('HTTP/1.1 304 Not Modified');
    exit();

} else {

    // Image has changed or browser has no cache
    $mimetype = $this->getMIMEType($path, $filename);
    $format = substr(strstr($mimetype, "/"), 1);
    $this->getResponse()->setHeader('Content-type', $mimetype);
    $expires = 60 * 60 * 24 * 3;
    $exp_gmt = gmdate("D, d M Y H:i:s", time() + $expires) . " GMT";
    $mod_gmt = gmdate("D, d M Y H:i:s", $this->getFiletime($path, $filename)) . " GMT";

    $imagedata = $this->_processor->processImage($path, $filename, $w, $h, $crop, $format);

    // Send Headers for Browser Caching Control
    $this->getResponse()->setHeader('Expires', $exp_gmt, true);
    $this->getResponse()->setHeader('Last-Modified', $mod_gmt, true);
    $this->getResponse()->setHeader('Cache-Control', 'public, max-age=' . $expires, true);
    $this->getResponse()->setHeader('Pragma', '!invalid', true);
    $this->getResponse()->setHeader('Content-Length', strlen($imagedata), true);
    $this->getResponse()->setHeader('ETag', md5($imagedata), true);

    // Save image in Zend Framework's server side Cache or get it from there
    $cache_id = str_replace(".", '', $path . "_" . $filename . "_" . $w . "_" . $h . "_" . $crop . "_" . $format);

    if (!$result = $this->_cache->load($cache_id)) {
        $result = $imagedata;
        $this->_cache->save($result, $cache_id, array('image', $path . '_' . str_replace(".", '', $filename)));
    }
    echo $result;
}