Laravel 5 HTML缩小会干扰干预包
我正在实施干预软件包来动态调整我站点中的图像大小。它运行得很好,我很满意。以下是我如何做到这一点的示例:Laravel 5 HTML缩小会干扰干预包,laravel-5,minify,intervention,Laravel 5,Minify,Intervention,我正在实施干预软件包来动态调整我站点中的图像大小。它运行得很好,我很满意。以下是我如何做到这一点的示例: Route::get('images/ads/{width}-{ad_image}-{permalink}.{format}', function($width, $image, $permalink, $format) { $img = Image::make($image->path) ->resize($width, null, function($
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);
}
}
类HTMLminify
{
公共函数句柄($request,Closure$next){
$response=$next($request);
$content=$response->getContent();
$search=array(
'/\>[^\S]+/S',//除去标记后的空格,空格除外
“/[^\S]+\”,
“好的。我找到了一个解决问题的方法,将图像排除在缩小范围之外。显然,干预建议的通过路由的实现也要通过中间件,因为它是通过Route::类处理的
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;
}
}
}
类HTMLminify
{
/**
*处理传入的请求。
*
*@param\light\Http\Request$Request
*@param\Closure$next
*@返回混合
*/
公共函数句柄($request,Closure$next){
$response=$next($request);
$content=$response->getContent();
如果(!str_包含($response->headers->get('Content-Type'),'image/'))
{
$search=array(
'/\>[^\S]+/S',//除去标记后的空格,空格除外
“/[^\S]+\”,
'