我正在使用克罗珀JS来裁剪我的图像。我能够获得我的画布的宽度和高度,但是,我需要知道裁剪图像的协调(X和Y)。
这是我的密码-
(function () {
//getting ratio
function getImageRatio(sourceCanvas) {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var width = sourceCanvas.width;
var height = sourceCanvas.height;
canvas.width = width;
canvas.height = height;
context.beginPath();
context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI);
context.strokeStyle = 'rgba(0,0,0,0)';
context.stroke();
context.clip();
context.drawImage(sourceCanvas, 0, 0, width, height);
return canvas;
}
window.addEventListener('DOMContentLoaded', function () {
var image = document.getElementById('image');
var button = document.getElementById('button');
var result = document.getElementById('result');
var croppable = false;
var cropper = new Cropper(image, {
aspectRatio: 1,
viewMode: 1,
built: function () {
croppable = true;
}
});
button.onclick = function () {
var croppedCanvas;
var roundedCanvas;
var roundedImage;
if (!croppable) {
return;
}
// Crop
croppedCanvas = cropper.getCroppedCanvas();
console.log(getImageRatio(croppedCanvas));
};
});
})();任何想法,如何获得裁剪图像的坐标,以便我可以裁剪这个图像的PHP。
发布于 2016-08-17 05:44:31
因此,如果我正确理解,您正在寻找方法 getData。
它将返回这些属性,如文档所述:
x: the offset left of the cropped area
y: the offset top of the cropped area
width: the width of the cropped area
height: the height of the cropped area
rotate: the rotated degrees of the image
scaleX: the scaling factor to apply on the abscissa of the image
scaleY: the scaling factor to apply on the ordinate of the image不过,我建议您看一下doc:https://github.com/fengyuanchen/cropper#getcroppedcanvasoptions这一节
特别是在这一部分:
然后,您可以直接将画布显示为图像,或者使用HTMLCanvasElement.toDataURL获取数据URL,或者使用HTMLCanvasElement.toBlob获取blob,如果浏览器支持这些API,则使用FormData将其上传到服务器。
如果您不能使用toBlob方法,因为某些浏览器限制(您可以始终添加一个多边形填充),您可以使用画布上的toDataUrl (IE9+)获取图像。并将其发送到已经裁剪的后端服务。为此,您需要这样做:
var formData = new FormData();
formData.append('image', canvas.toDataURL());您可以在caniuse中检查formData浏览器支持。
希望你觉得这有帮助!
https://stackoverflow.com/questions/38584590
复制相似问题