ก่อนอื่นให้ติดตั้ง Intervention Image ก่อน ดูวิธีการติดตั้งได้ตามลิ้งค์นี้ครับ http://image.intervention.io/getting_started/installation#laravel
ตัวอย่างโค๊ด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public function store(Request $request) { // Validate read more on validation at https://laravel.com/docs/5.4/validation $this->validate($request, [ 'name' => 'required|string|max:150', 'picture' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); // store $car = new Car; if($request->file('picture')){ //resize image $imgwidth = 300; //folder upload (public/imgpics) $folderupload = 'imgpics'; $file = $request->file('picture'); $filename = time() . $file->getClientOriginalName(); // prepend the time (integer) to the original file name $path = public_path($folderupload.'/' . $filename); // create instance of Intervention Image $img = \Image::make($file->getRealPath()); if($img->width()>$imgwidth){ // See the docs - http://image.intervention.io/api/resize // resize the image to a width of 300 and constrain aspect ratio (auto height) $img->resize($imgwidth, null, function ($constraint) { $constraint->aspectRatio(); }); } $img->save($path); // $car->picture = $filename; } $car->name = $request->input('name'); $car->save(); // redirect return redirect('cars')->with('message', "Success!"); } |
ดุเพิ่มเติม
https://laracasts.com/discuss/channels/laravel/upload-image-and-resize?page=1
https://laracasts.com/discuss/channels/general-discussion/laravel-5-image-upload-and-resize?page=1
ป้ายกำกับ:laravel