2015-11-28 41 views
0

我正在实施干预包以动态调整我的网站中的图像大小。它工作得很好,我很满意。下面是我如何做到这一点的例子:HTML缩小干扰干预包

Route::get('images/ads/{width}-{ad_image}-{permalink}.{format}', function($width, $image, $permalink, $format) 
{ 
    $img = Image::make($image->path) 
     ->resize($width, null, function($constraint){ 
      $constraint->aspectRatio(); 
     }); 
    return $img->response($format); 
}); 

最近,我想使我的网站的加载速度通过一个中间件自动压缩我的看法:

class HTMLminify 
{ 
    public function handle($request, Closure $next) { 
     $response = $next($request); 
     $content = $response->getContent(); 

     $search = array(
      '/\>[^\S ]+/s', // strip whitespaces after tags, except space 
      '/[^\S ]+\</s', // strip whitespaces before tags, except space 
      '/(\s)+/s'  // shorten multiple whitespace sequences 
     ); 

     $replace = array(
      '>', 
      '<', 
      '\\1' 
     ); 

     $buffer = preg_replace($search, $replace, $content); 
     return $response->setContent($buffer); 
    } 
} 

随后赶来的噩梦。浏览者报告,Intervention处理的图像现在被“截断”,不会显示。关闭中间件可以毫无问题地显示图像。

据我所知,HTMLminify类的代码是它修改了从视图生成的输出并删除了空格,并且没有看到任何可能干扰图像的原因。

任何想法,家伙?

谢谢先进。

回答

0

好的。我通过排除缩小图像来找到解决该问题的解决方法。显然,Intervention推荐的通过路由实现也是通过中间件,因为它是通过Route :: class处理的。

class HTMLminify 
{ 
    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) { 
     $response = $next($request); 
     $content = $response->getContent(); 

     if(!str_contains($response->headers->get('Content-Type'), 'image/')) 
     { 
      $search = array(
       '/\>[^\S ]+/s', // strip whitespaces after tags, except space 
       '/[^\S ]+\</s', // strip whitespaces before tags, except space 
       '/(\s)+/s'  // shorten multiple whitespace sequences 
      ); 

      $replace = array(
       '>', 
       '<', 
       '\\1' 
      ); 
      $buffer = preg_replace($search, $replace, $content); 
      return $response->setContent($buffer); 
     } else { 
      return $response; 
     } 
    } 
}