php artisan make:migration create_products_table
/** * Run the migrations. * * @return void */ public function up() { Schema::create('submissions', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('image'); $table->timestamps(); }); }
<html lang="en"> <head> <title>Laravel Image Intervention</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> </head> <body> <div class="container"> <h3 class="jumbotron">Laravel Image Manipulation </h3> <form method="post" action="{{url('products')}}" enctype="multipart/form-data"> @csrf <div class="row"> <div class="form-group"> <input type="text" name="name" class="form-control" placeholder="Name"> </div> <div class="form-group"> <input type="file" name="image" class="form-control"> </div> <div class="form-group"> <button type="submit" class="btn btn-success">Upload Image</button> </div> </div> @csrf </form> </div> </body> </html>
Route::resource('products', ProductController::class);
/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('create'); }
composer require intervention/image
'providers' => [ // ... Intervention\Image\ImageServiceProvider::class, ] Locate the aliases in config >> app.php file and register the aliases. 'aliases' => [ // ... 'Image' => Intervention\Image\Facades\Image::class, ]
//ProductController.php use App\Product; use Image; public function store(Request $request) { $image = $request->file('image'); $filename = $image->getClientOriginalName(); //Fullsize $image->move(public_path().'/full/',$filename); $image_resize = Image::make(public_path().'/full/'.$filename); $image_resize->fit(300, 300); $image_resize->save(public_path('thumbnail/' .$filename)); $product= new Product(); $product->name = $request->name; $product->image = $filename; $product->save(); return back()->with('success', 'Your product saved with image!!!'); }