第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Laravel 將圖像上傳到 Windows 上的存儲(chǔ)

Laravel 將圖像上傳到 Windows 上的存儲(chǔ)

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

3 回答

?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

我遇到過(guò)類(lèi)似的問(wèn)題并通過(guò)以下方式解決了


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


創(chuàng)建一個(gè)新磁盤(pán)config/filesystem.php并將其指向您喜歡的任何路徑,例如D:/test或其他


'disks' => [


    // ...


     'archive' => [

            'driver' => 'local',

            'root' => 'D:/test',

         ],


    // ...


請(qǐng)記住archive磁盤(pán)名稱(chēng),您可以將其調(diào)整為任何名稱(chēng)。之后做config:cache


然后將文件上傳到指定目錄執(zhí)行類(lèi)似的操作


  $request->file("image_file_identifier")->storeAs('SubFolderName', 'fileName', 'Disk');

例如


  $request->file("image_file")->storeAs('images', 'aa.jpg', 'archive');

現(xiàn)在要獲取文件,請(qǐng)使用以下代碼


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


查看完整回答
反對(duì) 回復(fù) 2023-11-04
?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊

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


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

經(jīng)過(guò)一些修改以適應(yīng)您的代碼,它應(yīng)該可以正常工作


查看完整回答
反對(duì) 回復(fù) 2023-11-04
?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

我希望你在這里使用圖像干預(yù)。如果沒(méi)有,您可以運(yùn)行命令:


? ? ? composer require intervention/image

這將對(duì)您的項(xiàng)目進(jìn)行干預(yù)。現(xiàn)在使用以下代碼將圖像上傳到本地。


您可以這樣調(diào)用該函數(shù)來(lái)保存圖像:


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

? ? ?}

}

如果您無(wú)論如何想使用 Storage Facade,我已經(jīng)在使用 Storage::put() 之前使用 Image->resize()->encode() 修改了您的代碼。請(qǐng)看看下面的代碼是否有效。(抱歉,我沒(méi)時(shí)間測(cè)試)


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


}

使用來(lái)源:Intervention repo,Github

另請(qǐng)注意,Storage 的put方法適用于圖像干預(yù)輸出,而putFile方法適用于 Illuminate\Http\UploadedFile 以及 Illuminate\Http\File 和實(shí)例。


查看完整回答
反對(duì) 回復(fù) 2023-11-04
  • 3 回答
  • 0 關(guān)注
  • 247 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)