0

这是我的代码:

//helpers.php
if (!function_exists('image_helper')) {
    function image_helper($file = NULL, $path = NULL)
    {
        $image = app('image_helper');

        if (!is_null($file) && is_null($path)) {
            return $image->generateTmp($file);
        }

        return $image;
    }
}

// ImageHelper.php
<?php

namespace App\Helpers;

use Illuminate\Http\File;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Spatie\ImageOptimizer\OptimizerChainFactory;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class ImageHelper
{
    private $fileName, $sizes;

    function __construct()
    {
        $this->fileName = md5(time() . openssl_random_pseudo_bytes(16));
    }

    public function get($query, $path, $type)
    {
        $arr = [];
        $sizes = getImageSizes($type);

        $picture = $query->picture()->first();

        if ($picture) {
            foreach ($sizes as $key => $size) {
                $url = filter_var($picture->file, FILTER_VALIDATE_URL) ?
                    $picture->file :
                    Storage::url($path . $key . '/' . $picture->file);

                $arr = array_add($arr, $key, $url);
            }
        }

        return $arr;
    }
?>

我已经在我的 composer.json 中添加了:

"autoload": {
"classmap": [
  "database/seeds",
  "database/factories",
  "app/Helpers/helpers.php"
],
"psr-4": {
  "App\\": "app/"
},
"files": [
  "app/Helpers/helpers.php"
]
},

当我运行此代码时:image_helper()->get($query, '/images');

我有ReflectionException (-1) Class image_helper does not exist

已经做了composer dump-autoloadphp artisan optimize还是一样。

以前在 Laravel 5.4 上它可以完美运行。

有什么解决办法吗?

4

1 回答 1

0

问题出在您的辅助函数中的这一行:

$image = app('image_helper');

将其更改为:

$image = app('App\Helpers\ImageHelper');

现在它的工作

于 2017-08-31T06:02:12.583 回答