Search code examples
javascriptjqueryhtmlcropcroppic

Image not getting empty when cancel is clicked on file input?


Im using Croppie plugin for this crop image function. Input text field becomes empty and the image is not getting empty when a user clicks the cancel on file input. I want to remove the image too when a user clicks the cancel button on file input. enter image description here DEMO

HTML

<form action="test-image.php" id="form" method="post">
  <input type="file" id="upload" value="Choose a file">
  <div id="upload-demo"></div>
  <input type="hidden" id="imagebase64" name="imagebase64">
  <a href="#" class="upload-result">Send</a>
</form>

JS

$(document).ready(function() {
  var $uploadCrop;

  function readFile(input) {
    if (input.files && input.files[0]) {
      var reader = new FileReader();
      reader.onload = function(e) {
        $uploadCrop.croppie('bind', {
          url: e.target.result
        });
        $('.upload-demo').addClass('ready');
      }
      reader.readAsDataURL(input.files[0]);
    }
  }
  $uploadCrop = $('#upload-demo').croppie({
    viewport: {
      width: 200,
      height: 200,
      type: 'circle'
    },
    boundary: {
      width: 300,
      height: 300
    }
  });
  $('#upload').on('change', function() {
    readFile(this);
  });
  $('.upload-result').on('click', function(ev) {
    $uploadCrop.croppie('result', {
      type: 'canvas',
      size: 'original'
    }).then(function(resp) {
      $('#imagebase64').val(resp);
      $('#form').submit();
    });
  });   
});

Solution

  • Well, this might be a typical scenario, as you cannot bind click event to cancel button of file-upload as the result of the file dialog is not exposed to the browser. So what I would suggest is, write an onchange event to file upload element and check if its value is empty. If yes, then reset the croppie instance after destroying it. Here's the updated code and working Fiddle.

    HTML

    <form action="test-image.php" id="form" method="post">
      <input type="file" id="upload" value="Choose a file" onchange="clearImage(this)" />
      <div id="upload-demo"></div>
      <input type="hidden" id="imagebase64" name="imagebase64">
      <a href="#" class="upload-result">Send</a>
    </form>
    

    JS

    var $uploadCrop;
    var opts = {
      viewport: {
        width: 200,
        height: 200,
        type: 'circle'
      },
      boundary: {
        width: 300,
        height: 300
      }
    };
    
    //change function.    
    function clearImage(ctrl) {
      if ($(ctrl).val() == "") {
        $('#upload-demo').croppie('destroy');
        $uploadCrop=$('#upload-demo').croppie(opts);
      }
    }
    
    $(document).ready(function() {
      function readFile(input) {
        if (input.files && input.files[0]) {
          var reader = new FileReader();
          reader.onload = function(e) {
            $uploadCrop.croppie('bind', {
              url: e.target.result
            });
            $('.upload-demo').addClass('ready');
          }
          reader.readAsDataURL(input.files[0]);
        }
      }
    
      $uploadCrop = $('#upload-demo').croppie(opts);
    
      $('#upload').on('change', function() {
        readFile(this);
      });
      $('.upload-result').on('click', function(ev) {
        $uploadCrop.croppie('result', {
          type: 'canvas',
          size: 'original'
        }).then(function(resp) {
          $('#imagebase64').val(resp);
          $('#form').submit();
        });
      });
    
    });