I have created a function which resizes the size of the crop box. The user enters height and width, clicks "set" and job is done.
$('body').on('click', '#cropImageModal .set_box_size', function () {
var width = $(this).parents('.panel-body').find('.width').val();
var height = $(this).parents('.panel-body').find('.height').val();
document.getElementById('imageToCrop').cropper.setCropBoxData({width: parseInt(width), height: parseInt(height)});
});
Except we have a problem when, which is when resize is performed for some reason crop box is scaled down depending on the size of the image.
i.e. I have an image 700x385 and I set the box to be 700x300 and click "set"... result? the result is 510x219
I have narrowed down to the issue within cropperJS and it is to do with a code on lines 2662-2667:
if (self.ready && self.cropped) {
data = {
x: cropBoxData.left - canvasData.left,
y: cropBoxData.top - canvasData.top,
width: cropBoxData.width,
height: cropBoxData.height
};
// issue is below
ratio = imageData.width / imageData.naturalWidth;
each(data, function (n, i) {
n /= ratio;
data[i] = rounded ? Math.round(n) : n;
});
}
If I remove "ratio" code above the problem is solved but now getCroppedCanvas
doesn't work properly due to the added black background to the cropped image.
By the way, same can be seen when using /, if you are to:
- click "Get Crop Box Data"
- adjust the width/height in the box where result presented
- click "Set Crop Box Data"
- The Width / Height parameters to the right of the image do not match up
I have created a function which resizes the size of the crop box. The user enters height and width, clicks "set" and job is done.
$('body').on('click', '#cropImageModal .set_box_size', function () {
var width = $(this).parents('.panel-body').find('.width').val();
var height = $(this).parents('.panel-body').find('.height').val();
document.getElementById('imageToCrop').cropper.setCropBoxData({width: parseInt(width), height: parseInt(height)});
});
Except we have a problem when, which is when resize is performed for some reason crop box is scaled down depending on the size of the image.
i.e. I have an image 700x385 and I set the box to be 700x300 and click "set"... result? the result is 510x219
I have narrowed down to the issue within cropperJS and it is to do with a code on lines 2662-2667:
if (self.ready && self.cropped) {
data = {
x: cropBoxData.left - canvasData.left,
y: cropBoxData.top - canvasData.top,
width: cropBoxData.width,
height: cropBoxData.height
};
// issue is below
ratio = imageData.width / imageData.naturalWidth;
each(data, function (n, i) {
n /= ratio;
data[i] = rounded ? Math.round(n) : n;
});
}
If I remove "ratio" code above the problem is solved but now getCroppedCanvas
doesn't work properly due to the added black background to the cropped image.
By the way, same can be seen when using http://fengyuanchen.github.io/cropperjs/, if you are to:
- click "Get Crop Box Data"
- adjust the width/height in the box where result presented
- click "Set Crop Box Data"
- The Width / Height parameters to the right of the image do not match up
- I did find a solution, i will post it later. – Vlad Vladimir Hercules Commented May 24, 2018 at 8:05
- I have added an answer to this question. – Vlad Vladimir Hercules Commented May 24, 2018 at 9:33
- Your solution works, thanks! – croppio. Commented May 24, 2018 at 20:13
1 Answer
Reset to default 6The solution was to figure out the ratio and then perform the calculation of adjusted width and height using calculated ratio:
var width = parseInt($(this).parents('.panel-body').find('.width').val());
var height = parseInt($(this).parents('.panel-body').find('.height').val());
var ratio = document.getElementById('imageToCrop').cropper.imageData.width / document.getElementById('imageToCrop').cropper.imageData.naturalWidth;
document.getElementById('imageToCrop').cropper.setCropBoxData({width: width*ratio, height: height*ratio});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745587744a4634644.html
评论列表(0条)