[英]Convert a base64 string encoded image to a tiff format in javascript
我有一张 base64 图像,需要将其转换为 .tiff 格式。 请问有(纯)javascript 方法吗?
注意:我看到 base64 编码只是原始文件的压缩,我这样做是为了恢复原始文件,但现在必须将其转换为 tiff
function urltoFile(url, filename, mimeType){
mimeType = mimeType || (url.match(/^data:([^;]+);/)||'')[1];
return (fetch(url)
.then(function(res){return res.arrayBuffer();})
.then(function(buf){return new File([buf], filename, {type:mimeType});})
);
}
我使用的框架(CEP)允许使用以下代码将本地文件转换为 tiff 格式:
//save the file in tiff format in a specified fodler
csInterface.evalScript(`exportFile()`, function(path) {
console.log("saved at",path);
});
//in jsx
function exportFile() {
Folder((fnp = (aD = activeDocument).fullName.parent + '/folder/')).create();
aD.saveAs(File(fnp + aD.name), new TiffSaveOptions(), true, Extension.LOWERCASE);
return activeDocument.fullName;
}
//then u can get it with this code
csInterface.evalScript('app.documents[0].fullName.fsName', function(res) {
//give the location of the current document
let path = res;
path = res.substring(0, res.lastIndexOf("\\"));
let fileName = res.substring(res.lastIndexOf("\\"));
fileName = fileName.substr(1, fileName.lastIndexOf(".")) + "tif";
path += "\\folder\\" + fileName;
console.log(path);
let fileInBase64 = window.cep.fs.readFile(path, cep.encoding.Base64);
//transform the path to match the tif file
//let fileName = res.split('\\').pop().split('/').pop();
urltoFile('data:image/png;base64,'+fileInBase64.data, fileName)
.then(function(file){
console.log(file);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.