angular - 如何以角度调整base64图像的大小
问题描述
我尝试调整 base64 图像的大小但我没有找到答案。我尝试了画布但没有回答。我不知道为什么......这是代码
const canvas = document.createElement('canvas'),
ctx = <CanvasRenderingContext2D>canvas.getContext('2d');
canvas.width = 100;
canvas.height = 100;
const img = new Image();
img.src = this.SelectedFile.src;
ctx.drawImage(img , 0, 0, 100, 100);
this.SelectedFile.src = ctx.canvas.toDataURL();
有谁知道任何其他方式或知道问题是什么?
解决方案
在给定组件之外添加这个辅助函数:
function compressImage(src, newX, newY) {
return new Promise((res, rej) => {
const img = new Image();
img.src = src;
img.onload = () => {
const elem = document.createElement('canvas');
elem.width = newX;
elem.height = newY;
const ctx = elem.getContext('2d');
ctx.drawImage(img, 0, 0, newX, newY);
const data = ctx.canvas.toDataURL();
res(data);
}
img.onerror = error => rej(error);
})
}
然后像这样调用:
compressImage(base64, 100, 100).then(compressed => {
this.resizedBase64 = compressed;
})
推荐阅读
- symfony - 使用 OneloginSamlBundle 进行身份验证后从数据库中获取用户
- android - 如何通过 Ionic 从我的应用程序中删除用户数据?
- python - 如何从熊猫文件中检索页脚
- javascript - 尽管请求显示状态 200 且响应负载正确,但 AJAX JSONP 请求失败
- r - nlsLM maxiter 不会改变
- python - python tkinter调整图像大小
- azure-devops - Azure DevOps 工作项中日期之间的天数(按查询)
- php - Ubuntu 运行多个 .service 文件
- javascript - 如何使用带有关键字数组的正则表达式来替换?
- centos - Centos 8 上的 Mezzio 骨架应用程序