Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.             //saat klik tombol tambah foto
  2.             $('.btn-img').click(function(){
  3.                 $('.input-img').click();
  4.             });
  5.  
  6.             // function untuk ambil foto dengan pluigins readfile
  7.             function readFile(input) {
  8.                 if (input.files && input.files[0]) {
  9.                     var reader = new FileReader();                 
  10.                     reader.onload = function (e) {                     
  11.                         $('.crop').addClass('ready');
  12.                         $('.boat-img').css('display','none');
  13.  
  14.                         // ini ulang plugin dengan metode bind
  15.                         $uploadCrop.croppie('bind', {
  16.                             url: e.target.result
  17.                         }).then(function(){
  18.                             console.log('jQuery bind complete');
  19.                         });                    
  20.                     }                  
  21.                     reader.readAsDataURL(input.files[0]);
  22.                 }
  23.                 else {
  24.                     swal("Sorry - you're browser doesn't support the FileReader API");
  25.                 }
  26.             }
  27.             // initialize plugin upload foto croppie
  28.             $uploadCrop = $('.crop').croppie({
  29.                 enableExif: true,
  30.                 viewport: {
  31.                     width: $('.card-image').width(),
  32.                     height: $('.card-image').width() * 2/3,
  33.                     type : 'square'
  34.                 },
  35.                  boundary: {
  36.                     width: $('.card-image').width(),
  37.                     height: $('.card-image').width()
  38.                 }
  39.             })
  40.             ;
  41.             // saat gambar sudah dipilih
  42.             $('.input-img').change(function(event){
  43.                 readFile(this) 
  44.             })
  45.             // saat menekan tombol simpan
  46.             $('.btn-save-img').click(function(){
  47.                 $uploadCrop.croppie('result',{
  48.                     type: 'base64',
  49.                     size: {width:600,height:400}
  50.                 }).then(function (resp) {                      
  51.                     var data_id = <?php echo $boat->id_boat ?>;
  52.                     var base_url = '<?php echo base_url() ?>';      
  53.                     $.ajax({
  54.                         url: ""+base_url+"admin/boat/upload_image/"+data_id+"",
  55.                         dataType: 'json',
  56.                         type: "POST",
  57.                         data:{'userfile':resp,'id_boat': data_id},
  58.                         beforeSend: function() {
  59.                             $('.btn-img').css('display','none');
  60.                             $('.crop').removeClass('ready');
  61.                             $('.loader').fadeIn();                         
  62.                         },                 
  63.                         success: function (data) {
  64.                             $('.btn-img').fadeIn();
  65.                             $('.loader').fadeOut();
  66.                             $('.boat-img').attr('src',resp);
  67.                             $('.boat-img').css('display','block');
  68.                              Materialize.toast(data.status, 4000);
  69.                         },
  70.                         error: function (xhr, status, err) {
  71.                             // console.log(xhr);
  72.                             // console.log(status);
  73.                             // console.log(err);
  74.                         },
  75.                     });        
  76.                 });
  77.             })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement