Laravel图像干预约束

时间:2018-07-12 06:39:11

标签: laravel serialization intervention

我已升级到相对平稳的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
    }
]

}

1 个答案:

答案 0 :(得分:0)

您应该知道,在本地主机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;
}