图像大小调整在Chrome中有效,但在Firefox中则不然
Image resizing working in Chrome, but not in Firefox
我的 Angular 1.5 应用程序中有一个函数,如果 base64 编码的图像超过最大大小,可以调整其大小。这个函数在Chrome中效果很好,但在Firefox中它返回一个空字符串,而不是任何base64编码的内容。
我已经把它打包在 Plunker 的 Angular 应用程序中,但这里有相关的功能:
// 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>
:
img.onload = function() {
// now your image is ready for use
};
相关文章:
- 滚动在Chrome中有效,但在Firefox或IE中无效
- jQuery自动完成在Firefox中运行良好,但在Chrome中则不然
- 为什么这个按钮在IE中有效,但在Firefox中无效
- HTML选择,在DOM中选择了正确的选项,但在firefox中显示了错误的项目
- Ajax调用在InternetExplorer中冻结UI,但在firefox中运行良好
- Javascript下载在firefox中停止,但在Chrome中有效
- 以下颜色更改功能在IE9和Firefox中运行良好,但在早期的IE或Chrome中则不然
- JavaScript函数在Safari中有效,但在Firefox中找不到
- Javascript'元素'在ie中未定义,但在chrome和firefox中运行良好
- javascript代码,用于删除firefox中的空白,但在IE和chrome中无法正常工作
- jQuery代码在Firefox中工作,但在IE中失败
- 在Firefox中工作正常,但在Chrome中不能
- Javascript键码在Firefox中不起作用,但在chrome中运行良好
- 输入类型的图像在Internet Explorer中不起作用,但在Chrome和Firefox中起作用
- 聚合物准备在IE11上启动,但在Firefox或Chrome上不启动
- 使用量角器双击在chrome中效果良好,但在firefox中不起作用
- Jquery.hide()和.show()在firefox上运行速度较慢,但在chrome上运行良好
- 禁用字段在IE中有效,但在Firefox中无效
- 在getJSON之后构建HTML在Safari中有效,但在Chrome或Firefox中无效
- 为什么对img标记的前向引用在Chrome上失败,但在FireFox和IE上有效