Laravel Backpack Image字段类型:使用原始文件名存储图像

时间:2017-10-11 18:38:56

标签: laravel backpack-for-laravel

我正在使用Laravel Backpack image Field Type文档进行图片上传。

在他们的mutator示例中,他们使用md5文件名,但是我想在存储文件时使用原始文件名。

// if a base64 was sent, store it in the db
    if (starts_with($value, 'data:image'))
    {
        // 1. Make the image
        $image = \Image::make($value);
        // 2. Generate a filename.
        $filename = md5($value.time()).'.jpg';
        // 3. Store the image on disk.
        \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
        // 4. Save the path to the database
        $this->attributes[$attribute_name] = $destination_path.'/'.$filename;
    }

我知道我需要编辑第2步,但不完全确定如何,并且我觉得我可能需要编辑第1步。

奖金问题只是为了帮助我理解:第一步中的'\'是什么“\ Image :: ...”

1 个答案:

答案 0 :(得分:0)

You may want to try using the File Facade and Input Facade https://laravel.com/api/5.4/Illuminate/Support/Facades/File.html

What I usually proceed is having a FormRequest to validate the input types (if a file with the name x or file type jpeg comes, or is required) and either work directly with the Input Facade to retrieve it, File Facade to store it around. If you're receiving the image as a base64, you may try something like:

$file = Input::file('file');
if (!File::exists(storage_path('uploads/'.$destinationPath))) {
  File::makeDirectory(storage_path('uploads/'.$destinationPath), 0755, true);
}
Storage::disk('uploads')->putFileAs($destinationPath, $file, $file->getClientOriginalName());

or even:

$data = Input::all();
Image::make(file_get_contents($data->base64_image))->save($path);

Hope this helps