目前,我有2个上传表单和2个功能,uploadImage();
和uploadAlbum();
。我一直在想是否可以删除单个图像表单并在两种情况下都使用多图像表单。如果在多图像形式中仅选择1张图像,则将上传一张图像;如果上传多于1张图像,则将上传相册。
这将使上传视图看起来更好,因为它将没有2个相同的上传表单,并且只需要在后端使用1个功能即可根据图像数量确定是单张图片还是相册上传。
我确实看不到它的任何缺点,但是我想确保在重新编写代码之前。
我的上传视图:
<form class='uploadForm' action="{{ route('imageUpload') }}" method="POST" enctype="multipart/form-data">
<label for="name">Image Name</label>
<input class='input' type="text" name="name" placeholder="Image Name">
<label for="description">Image Description</label>
<input class='input' type="text" name="description" placeholder="Description">
<input type="file" name="image"> {{ csrf_field() }}
<button class='Submit' type="submit" name="submit">UPLOAD</button>
</form>
<form class='uploadForm' action="{{ route('albumUpload') }}" method="POST" enctype="multipart/form-data">
<label for="albumName">Album Name</label>
<input class='input' type="text" name="albumName" placeholder="Album Name">
<label for="albumDescription">Image Description</label>
<input class='input' type="text" name="albumDescription" placeholder="Description">
<input type="file" name='files[]' multiple> {{ csrf_field() }}
<button class='Submit' type="submit" name="submit">UPLOAD</button>
</form>
我的uploadImage()
和uploadeAlbum()
函数:
public function uploadAlbum(Request $request){
$name = $request['albumName'];
$description = $request['albumDescription'];
$tag = $request['tags'];
$userId = auth()->user()->id;
$files = $request->file('files');
$path = 'storage/uploads/albums/'.$name;
$fileOriginalName = $files[0]->getClientOriginalName();
$fileName = pathinfo($fileOriginalName, PATHINFO_FILENAME);
$extension = $files[0]->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
$fileNameToStore = str_replace(' ', '', $fileNameToStore);
$album = new Album();
$album->name = $name;
$album->description = $description;
$album->user_id = $userId;
$album->thumbnail = $fileNameToStore;
$album->save();
$album->tags()->attach($tag);
if(!File::exists($path)) {
File::makeDirectory(public_path($path));
}
if (is_array($files) || is_object($files)){
foreach ($files as $file){
$fileOriginalName = $file->getClientOriginalName();
$fileName = pathinfo($fileOriginalName, PATHINFO_FILENAME);
$extension = $file->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
$fileNameToStore = str_replace(' ', '', $fileNameToStore);
$file->storeAs('public/uploads/albums/'.$name, $fileNameToStore);
$file->storeAs('public/uploads/albums/'.$name.'/thumbnails/', $fileNameToStore);
$thumbnailImage = InterventionImage::make('storage/uploads/albums/'.$name.'/thumbnails/'.$fileNameToStore)->fit(400, 400, function ($constraint) {
$constraint->upsize();
});
$thumbnailImage->save();
$albumImage = new AlbumImage();
$albumImage->file_name = $fileNameToStore;
$albumImage->album_id = $album->id;
$albumImage->save();
}
}
return redirect()->route('albums');
}
public function uploadImage(Request $request){
$this->validate($request, [
'name' => 'required|max:120',
'description' => 'max:120|nullable',
'image' => 'required'
]);
$name = $request['name'];
$description = $request['description'];
$tag = $request['tags'];
$userId = auth()->user()->id;
$file = $request->file('image')->getClientOriginalName();
$fileName = pathinfo($file, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
$fileNameToStore = str_replace(' ', '', $fileNameToStore);
$request->file('image')->storeAs('public/uploads/images/',$fileNameToStore);
$request->file('image')->storeAs('public/uploads/images/thumbnails/',$fileNameToStore);
$request->file('image')->storeAs('public/uploads/images/specificImages/',$fileNameToStore);
$request->file('image')->storeAs('public/uploads/images/miniImages/',$fileNameToStore);
$thumbnail = InterventionImage::make('storage/uploads/images/thumbnails/'.$fileNameToStore )->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$thumbnail->save();
$specificImage = InterventionImage::make('storage/uploads/images/specificImages/'.$fileNameToStore )->resize(2000, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$specificImage->save();
$miniImage = InterventionImage::make('storage/uploads/images/miniImages/'.$fileNameToStore )->fit(200, 200, function ($constraint) {
$constraint->upsize();
});
$miniImage->save();
$image = new Image();
$image->name = $name;
$image->description = $description;
$image->user_id = $userId;
$image->file_name = $fileNameToStore;
$image->save();
$image->tags()->attach($tag);
return redirect()->route('home');
}
答案 0 :(得分:1)
这当然是可能的。您将必须使用允许多个
的字段<input type="file" name="files[]" multiple />
提交表单时,您可以检查$_POST['files']
数组是否仅包含一个文件。如果是这样,则可以使用单个文件(图像)的逻辑,如果包含更多文件,则可以使用多个文件(相册)的逻辑。
完成此工作后,您还可以合并大部分逻辑并将其拆分为多个功能。将用foreach
来调用。