Javascript 在Chrome中可以调整图像大小,但在Firefox中不能
我的Angular 1.5应用程序中有一个函数,如果base64编码的图像超过最大尺寸,它可以调整图像大小。这个函数在Chrome中非常有效,但在Firefox中它返回一个空字符串,而不是任何base64编码的字符串 但相关功能如下:Javascript 在Chrome中可以调整图像大小,但在Firefox中不能,javascript,angularjs,Javascript,Angularjs,我的Angular 1.5应用程序中有一个函数,如果base64编码的图像超过最大尺寸,它可以调整图像大小。这个函数在Chrome中非常有效,但在Firefox中它返回一个空字符串,而不是任何base64编码的字符串 但相关功能如下: // Image resizing $scope.resizeImage = function(base64Data, maxWidth, maxHeight) { img = document.createElement('img');
// Image resizing
$scope.resizeImage = function(base64Data, maxWidth, maxHeight) {
img = document.createElement('img');
img.src = base64Data;
height = img.height;
width = img.width;
if (width > maxWidth) {
ratio = maxWidth / width; // get ratio for scaling image
height = height * ratio; // Reset height to match scaled image
width = width * ratio; // Reset width to match scaled image
}
// Check if current height is larger than max
if (height > maxHeight) {
ratio = maxHeight / height; // get ratio for scaling image
width = width * ratio; // Reset width to match scaled image
height = height * ratio; // Reset height to match scaled image
}
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// We set the dimensions at the wanted size.
canvas.width = width;
canvas.height = height;
// We resize the image with the canvas method drawImage();
ctx.drawImage(img, 0, 0, width, height);
var dataURI = canvas.toDataURL();
return dataURI;
}
您可能需要等待
加载:
img.onload = function() {
// now your image is ready for use
};