Конвертирую картинку
JPG формата в
WEBP формат.
Поставил
npmjs модуль
https://www.npmjs.com/package/webp-converter на node 18.
Записал
jpg картинку в виде текста в формате
base64 в переменную
[[FILE_CONTENT]]
И использую из офиц доков такой пример для:
Convert base64 image to webpbase64 image
const webp=require('webp-converter');
const path=[[FILE_CONTENT]]
function get_webpbase64(path) {
fs.readFile(path, function (error, data) {
if (error) {
throw error;
} else {
let buf = Buffer.from(data);
let dataBase64 = Buffer.from(buf).toString('base64');
// base64str of image
// base64str image type jpg,png ...
//option: options and quality,it should be given between 0 to 100
let result = webp.str2webpstr(dataBase64,"jpg","-q 80");
result.then(function(result) {
// you access the value from the promise here
console.log(result)
});
}
});
}
// use the default temp path for conversion
get_webpbase64("./nodejs_logo.jpg")
// use the custom temp path for conversion
get_webpbase64("./nodejs_logo.jpg","/home/user/Desktop/webp/temp")
Но данный код выдает ошибку:
ReferenceError: fs is not defined