javascript - JCrop and Ajax Image upload and crop (laravel 5, Jquery) -


i building , image upload , crop feature on website. work flow follows

1.upload button pressed. 2.a modal opens upload box

code modal:

<div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel"> <div class="modal-dialog" role="document">     <div class="modal-content">         <div class="modal-header">             <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">&times;</span></button>             <h4 class="modal-title" id="mymodallabel">upload profile picture</h4>         </div>         <div class="modal-body">             <div id="load" class="display-none" style="width:32px;margin:150px auto;"><img src="img/loading2.gif"></div>             <div class="upload-container">              {!! form::open(['file' => true, 'method' => 'post', 'id' => 'profile-image-upload']) !!}              <div class="alert alert-danger display-none error">                 <p>file must image</p>             </div>              <div class="form-group">                 {!! form::label('upload', 'upload photo') !!}                 {!! form::file('upload', ['id' => 'file-select', 'class' => 'form-control upload-box']) !!}             </div>              {!! form::close() !!}             </div>             <div id="image-box" class="image display-none" style="text-align:center;">                 <img id="large-image" src="" style="max-width:100%;max-height:500px;display:inline-block;">             </div>         </div>         <div class="modal-footer">             <button type="button" class="btn btn-default close-button" data-dismiss="modal">close</button>             <button type="button" class="btn btn-primary">save changes</button>         </div>     </div> </div> 

  1. click browse button , select photo, instantly triggers ajax request upload image , return path it.

server side code:

route::post('upload-profile-pic', function(){  $input['file'] = request::file('upload'); $rules = ['file' => 'mimes:jpg,png,gif,jpeg'];  $validator = validator::make(     $input,     $rules );  if ($validator->fails())     return 'false';  $identifier = str_random(20); $image = image::make(request::file('upload')); $image->encode('png')->save(public_path(). '/profile-images/temp/' . $identifier . '.png');  return $identifier; }); 
  1. on ajax success load resulting image div , show in modal.

javascript (jquery):

    $('input[type=file]').change(function () {          $('#load').show();          var formdata = new formdata($('#profile-image-upload')[0])          $('.upload-container').hide();          $.ajax({             type: 'post',             url: 'upload-profile-pic',             data: formdata,             cache: false,             contenttype: false,             processdata: false,             success: function (data) {                 $('#load').hide();                 console.log("success");                 if (data != 'false')                     console.log(data)                 $("#large-image").attr('src', '/profile-images/temp/' + data + '.png');                 $('.image').show();                 if (data == 'false')                     $('.upload-container').show();                 $('.error').show();             },             error: function (data) {                 $('#load').hide();                 console.log("error");                 console.log(data);             }         });     });      $('#mymodal').on('hidden.bs.modal', function () {         $('.error').hide();         $('.upload-container').show();         $('.image').hide();         $('#profile-image-upload').trigger("reset");     })      $('.close-button').on('click', function () {         $('.error').hide();         $('.upload-container').show();         $('.image').hide();         $('#profile-image-upload').trigger("reset");     }); 

i have 2 functions reset modal if canceled, hides image , shows upload box.

this works should problem want apply jcrop image generated. have tried many things

in ajax success funtion added this

$("#large-image").attr('src', '/profile-images/temp/' + data + '.png').jcrop(); 

the above works first time if modal closed , user tries again doesn't replace old image new one.

i tried adding

.done(fucntion(){     $("#large-image").jcrop( }); 

this same last option, works first time doesn't work after that.

i have tried

var image = $("#large-image"); 

then adding ajax success

image.jcrop() 

and adding closing functions

image.destroy() 

this same last time wors first time , detroy() throws error in console.

javascript isn't strong point , i'm quite stuck on now, can help?

resolved using code below:

$("#large-image").attr('src', '/profile-images/temp/' + data['identifier'] + '.png').jcrop({}, function () {       jcrop_api = this;       $('.modal-dialog').animate({width: ($('#large-image').width() + 50)}); }); 

then when closing modal call

jcrop_api.destroy() 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -