对于开发,我正在laravel项目的Windows上工作。 我正在尝试使文件上传在本地工作。
我的上传代码:
public function addPicture(Request $request, $id)
{
$bathroom = Bathroom::withTrashed()->findOrFail($id);
$validatedData = Validator::make($request->all(), [
'afbeelding' => 'required|image|dimensions:min_width=400,min_height=400',
]);
if($validatedData->fails())
{
return Response()->json([
"success" => false,
"errors" => $validatedData->errors()
]);
}
if ($file = $request->file('afbeelding')) {
$img = Image::make($file);
$img->resize(3000, 3000, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$img->stream();
$uid = Str::uuid();
$fileName = Str::slug($bathroom->name . $uid).'.jpg';
$this->createImage($img, 3000, "high", $bathroom->id, $fileName);
$this->createImage($img, 1000, "med", $bathroom->id, $fileName);
$this->createImage($img, 700, "thumb", $bathroom->id, $fileName);
$this->createImage($img, 400, "small", $bathroom->id, $fileName);
$picture = new Picture();
$picture->url = '-';
$picture->priority = '99';
$picture->alt = Str::limit($bathroom->description,100);
$picture->margin = 0;
$picture->path = $fileName;
$picture->bathroom_id = $id;
$picture->save();
return Response()->json([
"success" => true,
"image" => asset('/storage/img/bathroom/'.$id.'/small/'.$fileName),
"id" => $picture->id
]);
}
return Response()->json([
"success" => false,
"image" => ''
]);
}
public function createImage($img, $size, $quality, $bathroomId, $fileName){
$img->resize($size, $size, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
Storage::put( $this->getUploadPath($bathroomId, $fileName, $quality), $img->stream('jpg',100));
}
public function getUploadPath($bathroom_id, $filename, $quality = 'high'){
$returnPath = asset('/storage/img/bathroom/'.$bathroom_id.'/'.$quality.'/'.$filename);
echo $returnPath;
}
我确实运行过:php artisan storage:link
并且以下路径可用D:\Documents\repos\projectname\storage\app
。当我上传文件时,我得到:
“消息”:“打开(D:\ Documents \ repos \ projectname \ storage \ app \): 无法打开流:没有此类文件或目录”, “ exception”:“ ErrorException”, “ file”:“ D:\ Documents \ repos \ projectname \ vendor \ league \ flysystem \ src \ Adapter \ Local.php”, “行”:157,
然后在日志中:
"file": "D:\\Documents\\repos\\projectname\\app\\Http\\Controllers\\Admin\\BathroomController.php",
"line": 141, .
指向下一行。
Storage::put( $this->getUploadPath($bathroomId, $fileName, $quality), $img->stream('jpg',100));
createImage函数的。 如何使其在Windows上运行,以便可以在本地测试我的网站?
答案 0 :(得分:0)
我通常直接将图像保存到公用文件夹,这是我的一个项目中的有效代码
$category = new Categories;
//get icon path and moving it
$iconName = time().'.'.request()->icon->getClientOriginalExtension();
// the icon in the line above indicates to the name of the icon's field in
//front-end
$icon_path = '/public/category/icon/'.$iconName;
//actually moving the icon to its destination
request()->icon->move(public_path('/category/icon/'), $iconName);
$category->icon = $icon_path;
//save the image path to db
$category->save();
return redirect('/category/index')->with('success' , 'Category Stored Successfully');
进行一些修改以适合您的代码,它应该可以正常工作
答案 1 :(得分:0)
我已经遇到了类似的问题,并通过以下方式解决了
要将文件上传到任何路径,请按照以下步骤操作。
在config/filesystem.php
中创建一个新磁盘,并将其指向您喜欢的任何路径,例如D:/test
或其他任何路径
'disks' => [
// ...
'archive' => [
'driver' => 'local',
'root' => 'D:/test',
],
// ...
记住archive
是您可以将其调整为任何名称的磁盘名称。
之后,config:cache
然后将文件上传到指定的目录中
$request->file("image_file_identifier")->storeAs('SubFolderName', 'fileName', 'Disk');
例如
$request->file("image_file")->storeAs('images', 'aa.jpg', 'archive');
现在使用以下代码获取文件
$path = 'D:\test\images\aa.jpg';
if (!\File::exists($path)) {
abort(404);
}
$file = \File::get($path);
$type = \File::mimeType($path);
$response = \Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
答案 2 :(得分:0)
希望您在这里使用Image Intervention。如果没有,您可以运行以下命令:
composer require intervention/image
这将对您的项目进行干预。现在,使用以下代码在本地上传图片。
您可以调用该函数以这种方式保存图像:
if ($request->hasFile('image')) {
$image = $request->file('image');
//Define upload path
$destinationPath = public_path('/storage/img/bathroom/');//this will be created inside public folder
$filename = round(microtime(true) * 1000) . "." .$image->getClientOriginalExtension();
//upload file
$this->uploadFile($image,$destinationPath,$filename,300,300);
//put your code to Save In Database
//just save the $filename in the db.
//put your return statements here
}
public function uploadFile($file,$destinationPath,$filename,$height=null,$width=null){
//create folder if does not exist
if (!File::exists($destinationPath)){
File::makeDirectory($destinationPath, 0777, true, true);
}
//Resize the image before upload using Image Intervention
if($height!=null && $width!=null){
$image_resize = Image::make($file->getRealPath());
$image_resize->resize($width, $height);
//Upload the resized image to the project path
$image_resize->save($destinationPath.$filename);
}else{
//upload the original image without resize.
$file->move($destinationPath,$filename);
}
}
如果您仍然想使用Storage Facade,在使用Storage :: put()之前,我已经通过使用Image-> resize()-> encode()修改了您的代码。请查看以下代码是否有效。 (抱歉,我没有时间去测试)
public function addPicture(Request $request, $id)
{
$bathroom = Bathroom::withTrashed()->findOrFail($id);
$validatedData = Validator::make($request->all(), [
'afbeelding' =>'required|image|dimensions:min_width=400,min_height=400']);
if($validatedData->fails())
{
return Response()->json([
"success" => false,
"errors" => $validatedData->errors()
]);
}
if ($file = $request->file('afbeelding')) {
$img = Image::make($file);
$img->resize(3000, 3000, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
//Encode the image if you want to use Storage::put(),this is important step
$img->encode('jpg',100);//default quality is 90,i passed 100
$uid = Str::uuid();
$fileName = Str::slug($bathroom->name . $uid).'.jpg';
$this->createImage($img, 3000, "high", $bathroom->id, $fileName);
$this->createImage($img, 1000, "med", $bathroom->id, $fileName);
$this->createImage($img, 700, "thumb", $bathroom->id, $fileName);
$this->createImage($img, 400, "small", $bathroom->id, $fileName);
$picture = new Picture();
$picture->url = '-';
$picture->priority = '99';
$picture->alt = Str::limit($bathroom->description,100);
$picture->margin = 0;
$picture->path = $fileName;
$picture->bathroom_id = $id;
$picture->save();
return Response()->json([
"success" => true,
"image" => asset('/storage/img/bathroom/'.$id.'/small/'.$fileName),
"id" => $picture->id
]);
}
return Response()->json([
"success" => false,
"image" => ''
]);
}
public function createImage($img, $size, $quality, $bathroomId,$fileName){
$img->resize($size, $size, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
Storage::put( $this->getUploadPath($bathroomId, $fileName, $quality), $img);
}
public function getUploadPath($bathroom_id, $filename, $quality ='high'){
$returnPath = asset('/storage/img/bathroom/'.$bathroom_id.'/'.$quality.'/'.$filename);
return $returnPath; //changed echo to return
}
使用的来源:Intervention repo,Github
另外,请注意,存储的 put 方法与图像干预输出一起使用,而 putFile 方法与这两种方法都可以Illuminate \ Http \ UploadedFile以及Illuminate \ Http \ File和实例。
答案 3 :(得分:-1)
我推荐使用laravel Storage中的功能,任何上传都很简单