javascript - 我可以处理 Base64 编码图像的水印吗?

标签 javascript image-processing watermark transloadit

我想处理图像并通过使用 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/

相关文章:

algorithm - 是否存在可以抵抗图像处理的数字图像隐写术算法?

html - 如何在网页背景中保留水印?

PHPExcel - 为 Excel .xls 生成的文件制作水印

javascript - 如何从母版javascript调用子页面的javascript函数

javascript - 如何在 React 中显示保存为 blob 的图像

javascript - 如何将选择的选项显示为按钮?

python - OpenCV 点目标检测未找到所有目标,发现圆圈偏移

javascript - 使用 Javascript 防止多个表单提交

python - 在opencv和python中复制图像的一部分

ios - iRecognise 人的扫描图像中的眼睛