Laravel背包图片字段类型:以原始文件名存储图片

问题描述:

我正在使用Laravel Backpack image Field Type文档上传图片上传。Laravel背包图片字段类型:以原始文件名存储图片

在他们的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

奖金问题只是帮助我的理解:什么是“\ “在步骤一个‘\图片:: ...’

+0

为什么?您不应该使用原始文件名称。你会遇到各种各样的问题,比如多个用户上传'test.jpg',你的服务器不喜欢的奇怪字符的名称等。 – ceejayoz

+0

基本上没有原始文件名,因为背包没有上传图像本身,但文件内容作为base64 – Danny

+0

@ceejayoz不会在这个项目中的任何多个上传,并且名称是标准字符 – cvassios

您可能需要使用文件门面和输入门面尝试 https://laravel.com/api/5.4/Illuminate/Support/Facades/File.html

我通常进行由具有FormRequest验证输入的类型(如果名称为x或文件的文件输入jpeg来,或者是必需的),并直接使用输入门面检索它,File Facade存储它。如果您收到的图像为Base64,您可以尝试类似:

$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()); 

甚至:

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

希望这有助于

+0

就可以轻松识别/查找服务器上的图像这将在我的模型中进行吗? – cvassios

+0

根据您的设计模式,您可以将它放在任何你想要的地方。从任何地方都可以访问外墙,只要您使用Illuminate \ Support \ Facades \ File;例如 – abr