如何使用jquery裁剪图像?

时间:2017-04-25 14:29:22

标签: javascript jquery html5 image

我可以在没有画布的情况下裁剪图像吗?如何在不调整图像大小的情况下裁剪图像?像这张真实的照片是133px x 133px

Large ImageSmall Image

Large Image成为Small Image

这是我的代码 https://jsfiddle.net/w26La1u6/2/

$(document).on('change', '#files', function(event) {
        var files = event.target.files; // FileList object
        var output = document.getElementById("list");

        for (var i = 0, f; f = files[i]; i++) {
                if (f.type.match('image.*')) {
                        if (this.files[0].size < 12097152) {
                                var reader = new FileReader();
                                reader.onload = (function(theFile) {
                                        return function(e) {
                                                var span = document.createElement('span');
                                                span.innerHTML = ['<img class="thumbnail" src="', e.target.result,
                                                        '" title="', escape(theFile.name), '"/>'
                                                ].join('');
                                                output.insertBefore(span, null);
                                        };
                                })(f);

                                $('#clear, #list').show();
                                reader.readAsDataURL(f);
                        } else {
                                alert("Image Size is too big. Minimum size is 2MB.");
                                $(this).val("");
                        }
                }
        }
})

但是如果没有其他方法可以在没有画布的情况下进行裁剪,请告诉我如何使用画布裁剪

编辑

<input id="uploadImage" type="file" accept="image/*" capture="camera" />
<img id="imgDisplay" src="http://placehold.it/300x200" alt="Not a kitten" />


var Resample = (function (canvas) {

    // (C) WebReflection Mit Style License

  function Resample(img, width, height, onresample) {

    var load = typeof img == "string",
    i = load || img;

    if (load) {
     i = new Image;
     // with propers callbacks
     i.onload = onload;
     i.onerror = onerror;
    }

    i._onresample = onresample;
    i._width = width;
    i._height = height;

    load ? (i.src = img) : onload.call(img);
   }

   function onerror() {
    throw ("not found: " + this.src);
   }

   function onload() {
    var
     img = this,
     width = img._width,
     height = img._height,
     onresample = img._onresample
    ;

    // Altered section - crop prior to resizing
    var imgRatio = img.width / img.height;
    var desiredRatio = width / height;
    var cropWidth, cropHeight;
    if (desiredRatio < imgRatio) {
      cropHeight = img.height;
      cropWidth = img.height * desiredRatio;
    } else {
      cropWidth = img.width;
      cropHeight = img.width / desiredRatio;
    }

    delete img._onresample;
    delete img._width;
    delete img._height;

    canvas.width = width;
    canvas.height = height;

    context.drawImage(
     // original image
     img,
     // starting x point
     0,
     // starting y point
     0,
     // crop width
     cropWidth,
     // crop height
     cropHeight,
     // destination x point
     0,
     // destination y point
     0,
     // destination width
     width,
     // destination height
     height
    );

    onresample(canvas.toDataURL("image/png"));
   }

    var context = canvas.getContext("2d"),
  round = Math.round;

  return Resample;

}(
 this.document.createElement("canvas"))
);

var newCropWidth = 133;
var newCropHeight = 133;

function loadImage(data) {
  document.querySelector('#imgDisplay').src = data;
}
function handleFileSelect(evt) {
  if (evt.target.files.length === 1) {
    var picFile = evt.target.files[0];

    if (picFile.type.match('image.*')) {
      var fileTracker = new FileReader;
      fileTracker.onload = function() {
        Resample(
         this.result,
         newCropWidth,
         newCropHeight,
         loadImage
       );
      }
      fileTracker.readAsDataURL(picFile);
    }
  }
}

document.querySelector('#uploadImage').addEventListener('change', handleFileSelect, false);

1 个答案:

答案 0 :(得分:1)

有很多方法可以使用jQuery UI,但您可以使用其他人已经创建的插件,例如Cropper