我实施了干预套餐,以动态调整网站中的图片大小。它工作得很好,我很满意。以下是我如何做的一个例子:
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);
}
}
然后是噩梦。浏览器报告说,干预处理的图像现在被截断&#34;并且不会显示。关闭中间件可以毫无问题地显示图像。
据我所知,从HTMLminify类&#39;代码是它修改了从视图生成的输出并删除了空格,并且没有看到它如何干扰图像的任何原因。
任何想法,伙计们?
先谢谢。
答案 0 :(得分:0)
行。我通过排除缩小图像来找到问题的解决方法。显然,干预建议通过路由实现也只是因为它是通过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;
}
}
}