React native-image-picker upload to laravel server code example
Example 1: Upload image laravel 6
<?php
namespace App\Http\Controllers\simple_image_upload;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Validator,Redirect,Response,File;
use App\Photo;
class ImageUploadController extends Controller
{
public function index()
{
return view('simple_image_upload.index');
}
public function store(Request $request)
{
request()->validate([
'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($files = $request->file('profile_image')) {
$destinationPath = public_path('/profile_images/');
$profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
$files->move($destinationPath, $profileImage);
$insert['image'] = "$profileImage";
$imagemodel= new Photo();
$imagemodel->photo_name="$profileImage";
$imagemodel->save();
}
return back()->with('success', 'Image Upload successfully');
}
}
Example 2: laravel load image to div on image selected via input file
<html lang="en">
<head>
<title>Change image on select new image from file input</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<input type="file" name="file" id="profile-img">
<img src="" id="profile-img-tag" width="200px" />
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#profile-img-tag').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#profile-img").change(function(){
readURL(this);
});
</script>
</body>