Step#01
Create
blade page in View
image_upload.blade.php
<form class="form-horizontal" method="post" action="{{ route('add-job-application') }}" enctype="multipart/form-data">
@csrf
@csrf
<div class="col-md-6 mb-3">
<label for="validationTooltip04">Photo</label>
<input type="file" name="photo" class="form-control photo" id="photo" onchange='image(this)'>
<div class="panel-body center-block" style="margin-top: 5px;">
<img id="showPhoto" src="" style="height:200px;width:200px;border-width:0px;">
</div>
</div>
<label for="validationTooltip04">Photo</label>
<input type="file" name="photo" class="form-control photo" id="photo" onchange='image(this)'>
<div class="panel-body center-block" style="margin-top: 5px;">
<img id="showPhoto" src="" style="height:200px;width:200px;border-width:0px;">
</div>
</div>
</form >
Modal for alert
<div class="modal fade" id="error_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document" style="width: 30%;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel" style="color: red;">Warning</h4>
</div>
<div id="warning_message" class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary btn-danger" data-dismiss="modal" style="color:white;">
Cancel
</button>
</div>
</div>
</div>
</div>
aria-hidden="true">
<div class="modal-dialog" role="document" style="width: 30%;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel" style="color: red;">Warning</h4>
</div>
<div id="warning_message" class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary btn-danger" data-dismiss="modal" style="color:white;">
Cancel
</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
/*----------------Start photo show ---------------------------*/
function image(input) {
var fup = document.getElementById('photo');
var fileName = fup.value;
var extension = fileName.substring(fileName.lastIndexOf('.') + 1);
if (extension === 'jpg' || extension === 'jpeg' || extension === 'png' || extension === 'gif') {
var file_size = '';
var photo = $('.photo ').val();
if (photo) {
var file_size = $('.photo')[0].files[0].size;
}
if (file_size > 80000) {
$('.photo').val('');
$('#showPhoto').attr('src', '');
$('#warning_message').text("The photo may not be greater than 80KB");
$('#error_modal').modal(300, 'show');
return false;
}
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#showPhoto').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
} else {
$('.photo').val('');
$('#showPhoto').attr('src', '');
$('#warning_message').text("Only JPEG, JPG, PNG or GIF type images are allowed");
$('#error_modal').modal(300, 'show');
return false;
}
}
/*----------------End photo show ---------------------------*/
</script>
/*----------------Start photo show ---------------------------*/
function image(input) {
var fup = document.getElementById('photo');
var fileName = fup.value;
var extension = fileName.substring(fileName.lastIndexOf('.') + 1);
if (extension === 'jpg' || extension === 'jpeg' || extension === 'png' || extension === 'gif') {
var file_size = '';
var photo = $('.photo ').val();
if (photo) {
var file_size = $('.photo')[0].files[0].size;
}
if (file_size > 80000) {
$('.photo').val('');
$('#showPhoto').attr('src', '');
$('#warning_message').text("The photo may not be greater than 80KB");
$('#error_modal').modal(300, 'show');
return false;
}
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#showPhoto').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
} else {
$('.photo').val('');
$('#showPhoto').attr('src', '');
$('#warning_message').text("Only JPEG, JPG, PNG or GIF type images are allowed");
$('#error_modal').modal(300, 'show');
return false;
}
}
/*----------------End photo show ---------------------------*/
</script>
Step#02
Route::post('add-job-application', 'HomeController@addJobApplication')->name('add-job-application');
public
function addJobApplication(Request
$request){
$input = $request->all();
$image
= $request->file('photo');
if($image){
$imgName=md5(str_random(30).time().'_'.$request->file('photo')).'.'.$request->file('photo')->getClientOriginalExtension();
Image::make($request->file('photo'))->resize(600, 600)->save(public_path('frontEnd/uploads/applicationPhoto/') . $imgName);
$input['photo']=$imgName;
}
if($image){
$imgName=md5(str_random(30).time().'_'.$request->file('photo')).'.'.$request->file('photo')->getClientOriginalExtension();
Image::make($request->file('photo'))->resize(600, 600)->save(public_path('frontEnd/uploads/applicationPhoto/') . $imgName);
$input['photo']=$imgName;
}
JobApplication::create($input);
No comments:
Post a Comment