简体   繁体   English

在上传之前使用Jcrop和文件输入来裁剪图像

[英]Using Jcrop with file-input to crop image before upload

In my rails app, I am using jcrop with krajee's bootstrap-fileinput . 在我的rails应用程序中,我将jcrop与krajee的bootstrap-fileinput结合使用 I attach fileinput to my form and then I try to use jcrop on the img element. 我将fileinput附加到表单上,然后尝试在img元素上使用jcrop。 When I do this, the image preview disappears and in it's place an icon for a broken image. 当我这样做时,图像预览消失,并在其中放置了损坏图像的图标。 I'm not sure what's going on. 我不确定发生了什么。

  var btnCust = '<button type="button" class="btn btn-default crop-image" title="Add picture tags">' +
      '<i class="glyphicon glyphicon-tag"></i>' +
      '</button>';

  var previewCust = '<div class="file-preview-frame" id="{previewId}" data-fileindex="{fileindex}" data-template="{template}">' +
              '<div class="kv-file-content">' +
              '<img src="{data}" class="kv-preview-data file-preview-image cropbox" title="{caption}" alt="{caption}">' +
              '</div>' +
              '{footer}' +
              '</div>';

  $('#update-profile-photo-input').fileinput({
    showClose: false,
    browseLabel: '',
    removeLabel: '',
    showCaption: false,
    defaultPreviewContent: "<img src='" + $('.profile-image').attr('src') + "' alt='Your Avatar' class='cropbox'>",
    layoutTemplates: {main2: '{preview} ' +  btnCust + ' {remove} {browse}', footer: ''},
    previewTemplates: {image: previewCust}
  });

  $('.crop-image').on('click', function () {
    $('.cropbox').Jcrop({
      aspectRatio: 1,
      setSelect: [0, 0, 175, 175],
      onSelect: update,
      onChange: update
    })
  });

  function update(coords) {
      $('#user_crop_x').val(coords.x);
      $('#user_crop_y').val(coords.y);
      $('#user_crop_w').val(coords.w);
      return $('#user_crop_h').val(coords.h);
  }

Below is part of the html that is created by file-input. 以下是文件输入创建的html的一部分。

html HTML

<div class="kv-file-content">
  <img src="blob:https%3A//my-app-werfds.c9.io/11176113-aab3-477c-bf1e-cf21a9c54cab" class="kv-preview-data file-preview-image cropbox" title="IMG_0315.JPG" alt="IMG_0315.JPG">
</div>

Is there something weird about the preview image that jcrop is having a problem with? jcrop遇到问题的预览图像有些奇怪吗? In my console I get an error that says GET blob:https%3A//my-app-werfds.c9.io/11176113-aab3-477c-bf1e-cf21a9c54cab 404 (not found) 在我的控制台中,我收到一条错误消息GET blob:https%3A//my-app-werfds.c9.io/11176113-aab3-477c-bf1e-cf21a9c54cab 404 (not found)

I wasn't sure what else to include. 我不确定还包括什么。 I didn't include the form because I'm not trying to submit it yet. 我没有包含该表单,因为我尚未尝试提交该表单。 I'm just having a problem with jcrop working on the preview img. 我只是在预览img上使用jcrop遇到问题。

Here is a jsfiddle (it was hard for me to implement all the above code but this is basically what is happening) 这是一个jsfiddle (对我来说,很难实现以上所有代码,但这基本上是正在发生的事情)

Was unable to locate if or where Blob URL is revoked, though was able to create a workaround by creating a <canvas> element, using .toDataURL() and replacing src of .file-preview-image with data URI of existing image before calling .Jcrop() 无法定位是否撤消Blob URL ,尽管能够通过创建<canvas>元素,使用.toDataURL()并在调用之前将.file-preview-image src替换为现有图像的data URI来创建解决方法.Jcrop()

 $('.crop-image').on('click', function () {
    var img = $('.file-preview-image');
    var canvas = $("<canvas>")[0];
    canvas.width = img[0].naturalWidth;
    canvas.height = img[0].naturalHeight;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img[0], 0, 0);
    var url = canvas.toDataURL();
    img.attr("src", url).Jcrop()
 });

jsfiddle https://jsfiddle.net/bea09mz9/3/ jsfiddle https://jsfiddle.net/bea09mz9/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM