亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 將圖像上傳到 Windows 上的存儲

Laravel 將圖像上傳到 Windows 上的存儲

PHP
料青山看我應如是 2023-11-04 21:01:09
為了進行開發,我在我的 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        ]);    }我確實運行了: php artisan storage:link并且可以使用以下路徑D:\Documents\repos\projectname\storage\app。當我上傳文件時,我得到:Storage::put(  $this->getUploadPath($bathroomId, $fileName, $quality), $img->stream('jpg',100));創建圖像函數。我怎樣才能讓它在 Windows 上運行,以便我可以在本地測試我的網站?
查看完整描述

3 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

我遇到過類似的問題并通過以下方式解決了


要將文件上傳到任何路徑,請按照以下步驟操作。


創建一個新磁盤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;


查看完整回答
反對 回復 2023-11-04
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

我通常直接將圖像保存到公共文件夾這是我的項目之一的工作代碼


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

經過一些修改以適應您的代碼,它應該可以正常工作


查看完整回答
反對 回復 2023-11-04
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

我希望你在這里使用圖像干預。如果沒有,您可以運行命令:


? ? ? composer require intervention/image

這將對您的項目進行干預?,F在使用以下代碼將圖像上傳到本地。


您可以這樣調用該函數來保存圖像:


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

另請注意,Storage 的put方法適用于圖像干預輸出,而putFile方法適用于 Illuminate\Http\UploadedFile 以及 Illuminate\Http\File 和實例。


查看完整回答
反對 回復 2023-11-04
  • 3 回答
  • 0 關注
  • 195 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號