我想处理图像并通过使用 transloadit 添加水印来确保其安全。但我的图像来源是 base64 编码的字符串,而不是要求用户将其上传到表单。我可以传递这个base64编码的字符串来形成并处理它以获得水印吗?
感谢任何帮助。谢谢!
最佳答案
似乎您需要将base64数据uri转换为Blob对象,然后手动将blob设置为FormData中的参数,如this SO question中建议的
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
然后使用以下方法进行设置:
var dataURL = canvas.toDataURL('image/jpeg', 0.5);
var blob = dataURItoBlob(dataURL);
var fd = new FormData(document.forms[0]);
fd.append("canvasImage", blob);
代码归功于@Stoive
关于javascript - 我可以处理 Base64 编码图像的水印吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34041968/