laravel - Laravel 图像干预约束
问题描述
我已经升级到相对流畅的 laravel 5.5。
但我现在的图像功能出现错误。我不知道为什么
这是它在前端的使用:http ://website.com/media/1000x300/5/filename.jpg
/* ////////////////////// IMAGES //////////////////////// */
Route::get( '/media/{size}/{crop}/{name}', function ( $size = null, $crop = null, $name = null ) {
if ( ! is_null( $size ) and ! is_null( $name ) and ! is_null( $crop ) ) {
$hours = 48;
if ( $size == 'admin' ) {
$cache_length = 0;
$size = "36x36";
$name = 'cache/' . $name;
} else {
$cache_length = $hours * 60;
}
$ext = strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
$size = explode( 'x', $size );
switch ( $crop ) {
/*///////////////////////// no crop and change ratio */
case "0":
$cache_image = Image::cache( function ( $image ) use ( $size, $name, $ext ) {
return $image->make( getcwd() . '/uploads/' . $name )->resize( $size[0], $size[1] )->encode( $ext, ( $ext == 'png' ) ? null : 85 );
}, $cache_length );
break;
/*///////////////////////// crop - NO upsize */
default:
case "1":
$cache_image = Image::cache( function ( $image ) use ( $size, $name, $ext ) {
return $image->make( getcwd() . '/uploads/' . $name )->fit( $size[0], $size[1], function ( $constraint ) {
$constraint->upsize();
} )->encode( $ext, ( $ext == 'png' ) ? null : 85 );
}, $cache_length );
break;
/*///////////////////////// crop - WITH upsize */
case "2":
$cache_image = Image::cache( function ( $image ) use ( $size, $name, $ext ) {
return $image->make( getcwd() . '/uploads/' . $name )->fit( $size[0], $size[1], function ( $constraint ) {
//$constraint->upsize();
} )->encode( $ext, ( $ext == 'png' ) ? null : 85 );
}, $cache_length );
break;
/*///////////////////////// No crop & add borders */
case "3":
$cache_image = Image::cache( function ( $image ) use ( $size, $name, $ext ) {
$image->make( getcwd() . '/uploads/' . $name )->resize( $size[0], $size[1], function ( $constraint ) {
$constraint->aspectRatio();
$constraint->upsize();
} );
$image->resizeCanvas( $size[0], $size[1], 'center', false, [
255,
255,
255,
0.0,
] )->encode( $ext, ( $ext == 'png' ) ? null : 85 );
return $image;
}, $cache_length );
break;
/*///////////////////////// No crop */
case "4":
$cache_image = Image::cache( function ( $image ) use ( $size, $name, $ext ) {
$image->make( getcwd() . '/uploads/' . $name )->resize( $size[0], $size[1], function ( $constraint ) {
$constraint->aspectRatio();
$constraint->upsize();
} )->encode( $ext, ( $ext == 'png' ) ? null : 85 );
//$image->resizeCanvas($size[0], $size[1], 'center', false, array(255, 255, 255, 0.0));
return $image;
}, $cache_length );
break;
/*///////////////////////// No crop & add borders but allow upsize */
case "5":
$cache_image = Image::cache( function ( $image ) use ( $size, $name, $ext ) {
$image->make( getcwd() . '/uploads/' . $name )->resize( $size[0], $size[1], function ( $constraint ) {
$constraint->aspectRatio();
//$constraint->upsize();
} );
$image->resizeCanvas( $size[0], $size[1], 'center', false, [
255,
255,
255,
0.0,
] )->encode( $ext, ( $ext == 'png' ) ? null : 85 );
return $image;
}, $cache_length );
break;
}
switch ( $ext ) {
case "jpeg":
return Response::make( $cache_image, 200, [
'Content-Type' => 'image/jpeg',
'Content-Disposition' => 'inline',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'public',
'Etag' => md5( $cache_image ),
] )->setMaxAge( 604800 )->setPublic()->setTtl( ( 60 * 30 ) );
break;
case "jpg":
return Response::make( $cache_image, 200, [
'Content-Type' => 'image/jpg',
'Content-Disposition' => 'inline',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'public',
'Etag' => md5( $cache_image ),
] )->setMaxAge( 604800 )->setPublic()->setTtl( ( 60 * 30 ) );
break;
case "png":
return Response::make( $cache_image, 200, [
'Content-Type' => 'image/png',
'Content-Disposition' => 'inline',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'public',
'Etag' => md5( $cache_image ),
] )->setMaxAge( 604800 )->setPublic()->setTtl( ( 60 * 30 ) );
break;
}
} else {
abort( 404 );
}
} );
但我真的需要那个功能。我不知道我做错了什么或我需要改变什么?这是我得到的错误
(1/1) ErrorException 闭包序列化失败:在抽象语法树中找不到闭包。
in SerializableClosure.php line 116
at HandleExceptions->handleError(1024, 'Serialization of closure failed: The closure was not found within the abstract syntax tree.', ...\\vendor\\jeremeamia\\SuperClosure\\src\\SerializableClosure.php', 116, array('e' => object(ClosureAnalysisException)))
at trigger_error('Serialization of closure failed: The closure was not found within the abstract syntax tree.', 1024)
in SerializableClosure.php line 116
at SerializableClosure->serialize()
at serialize(array(array('name' => 'make', 'arguments' => array('...\\public_html/uploads/banner_img.jpg')), array('name' => 'resize', 'arguments' => array('1000', '300', object(SerializableClosure))), array('name' => 'resizeCanvas', 'arguments' => array('1000', '300', 'center', false, array(255, 255, 255, 0.0))), array('name' => 'encode', 'arguments' => array('jpg', 85))))
in ImageCache.php line 164
at ImageCache->checksum()
in ImageCache.php line 296
at ImageCache->get(2880, false)
in ImageManager.php line 90
at ImageManager->cache(object(Closure), 2880)
in Facade.php line 221
这是我的作曲家文件
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.5.*",
"laravelcollective/html": "5.5.*",
"laravel/scout": "2.0.*",
"nesbot/carbon": "1.*",
"laracasts/flash": "dev-master",
"cviebrock/eloquent-sluggable": "^4.1",
"intervention/image": "2.4.2",
"intervention/imagecache": "2.3.3",
"maatwebsite/excel": "3.0.*",
"roumen/sitemap": "2.6.*",
"spatie/laravel-activitylog": "1.*",
"spatie/laravel-analytics": "3.*",
"spatie/laravel-newsletter": "3.*",
"khill/lavacharts": "~3.0",
"zizaco/entrust": "5.2.x-dev",
"kalnoy/nestedset": "4.3",
"devfactory/minify": "1.0.7",
"yocmen/html-minify": "dev-Laravel-5.4",
"fedeisas/laravel-mail-css-inliner": "2.2",
"graham-campbell/security": "^4.0.1",
"graham-campbell/markdown": "^9.0",
"graham-campbell/exceptions": "^10.1",
"hesto/multi-auth": "dev-master",
"lukepolo/laracart": "1.4.*",
"laravel/cashier": "~7.0"
},
"not used": {},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.7",
"symfony/css-selector": "3.1.*",
"symfony/dom-crawler": "3.1.*"
},
"autoload": {
"classmap": [
"database/"
],
"psr-4": {
"baselara\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist"
},
"repositories": [
{
"type": "composer",
"url": "https://packagist.org"
},
{
"packagist": false
}
]
}
解决方案
您应该知道,在 localhost Laravel 中使用public
文件夹和内部服务器,例如您的主机 laravel 不能使用它,并且 laravel 未知,您必须将上传文件夹定义config.php
为:
'public_path' => 'C:\xampp\htdocs\YOUR_WEB_APP\public', //in localhost
'public_path' => '/home/YOUR_WEB_APP/public_html', //in host
然后使用它并上传图像,将其调整到文件夹中:
public function uploadImage($file, $filename)
{
$year = Carbon::now()->year;
$imagePath = "/uploads/post_images/" . $year . "/";
$public_html = config('app.public_path');
$data = $file->move($public_html . $imagePath, $filename);
$sizes = ['300', '600', '900'];
$url['images'] = $this->resize($data->getRealPath(), $sizes, $imagePath, $filename);
$url['thumbnail'] = $url['images'][$sizes[0]];
return $url;
}
推荐阅读
- maven - WildFly Maven 插件 - 设置空白上下文根
- python - 我正在寻找叠加两张图像,其中一张是透明的
- sql - 如何解决错误组功能不允许
- python - 如何在基于密度的聚类中获取属于其聚类的文档?
- objective-c - 无法购买。联系开发商以获取更多信息订阅优惠
- python - 将 scipy 与复杂值函数结合使用时,如何避免多次调用慢速函数?
- sockets - 谁能告诉我如何在 Wireshark 上激活环回?
- flutter - 我应该在一页/屏幕中使用多个块在 Flutter 中加载不同的状态吗?
- python - pip 安装了 biopython,但它不会编译 Bio 模块
- python - 如何在 Python 中为 Linux 创建 URL 快捷方式?