![](/img/trans.png)
[英]Can i apoint the src img attribute to a blob file or a base64 encoded image?
[英]Can I process base64 encoded image for watermark?
我想处理图像并使用transloadit向其添加水印以使其安全。 但是我拥有的图像源是base64编码的字符串,而不是要求用户将其上传到表单。 我可以传递此base64编码的字符串来形成和处理它以获得水印吗?
任何帮助表示赞赏。 谢谢!
似乎您需要将base64数据uri转换为Blob对象,然后手动将blob设置为FormData中的参数,如该SO问题中所建议的那样
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以获取代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.