admin管理员组文章数量:1429636
I am using sharp server-side to prepare pictures to be served in a webapp. The current objective is to load a picture (BMP format), to load it in nodejs with sharp, to convert it in PNG, to resize it (scale down) and save it back to disk. The code is the following :
if(resize_pictures){
(...)
console.log('Reducing image size ... ');
fs.readdirSync(input_folder).forEach(file => {
tmp_input_path = path.join(input_folder, file)
tmp_output_path = path.join(tmp_folder_reduced, file)
//Resize
sharp(tmp_input_path)
.png() // Convert to png
.resize(target_width,null)
.flatten()
.toFile(tmp_output_path,
function(err){
if(err){
console.log("Error at reducing size / converting picture : ")
console.log(err)
console.log(tmp_input_path);
console.log(tmp_output_path);
return;
}
})
})
console.log('Image reduction pleted.');
I'm getting this error :
Reducing image size ...
Image reduction pleted.
Error at reducing size / converting picture :
[Error: Input file contains unsupported image format]
/home/user/<folder>/16c93ac9f297376b1b44eeeecff141b1f59a239d.bmp
/home/user/<folder>/TMP/16c93ac9f297376b1b44eeeecff141b1f59a239d.bmp
Output folder stays empty.
I don't really get why : the paths are correct, and so can be accessed. Pictures are stored on disk, paths are directly puted server-side (so no encoding problems, as I could have seen somewhere else concerning this problem).
Would someone have an idea or a solution ?
I am using sharp server-side to prepare pictures to be served in a webapp. The current objective is to load a picture (BMP format), to load it in nodejs with sharp, to convert it in PNG, to resize it (scale down) and save it back to disk. The code is the following :
if(resize_pictures){
(...)
console.log('Reducing image size ... ');
fs.readdirSync(input_folder).forEach(file => {
tmp_input_path = path.join(input_folder, file)
tmp_output_path = path.join(tmp_folder_reduced, file)
//Resize
sharp(tmp_input_path)
.png() // Convert to png
.resize(target_width,null)
.flatten()
.toFile(tmp_output_path,
function(err){
if(err){
console.log("Error at reducing size / converting picture : ")
console.log(err)
console.log(tmp_input_path);
console.log(tmp_output_path);
return;
}
})
})
console.log('Image reduction pleted.');
I'm getting this error :
Reducing image size ...
Image reduction pleted.
Error at reducing size / converting picture :
[Error: Input file contains unsupported image format]
/home/user/<folder>/16c93ac9f297376b1b44eeeecff141b1f59a239d.bmp
/home/user/<folder>/TMP/16c93ac9f297376b1b44eeeecff141b1f59a239d.bmp
Output folder stays empty.
I don't really get why : the paths are correct, and so can be accessed. Pictures are stored on disk, paths are directly puted server-side (so no encoding problems, as I could have seen somewhere else concerning this problem).
Would someone have an idea or a solution ?
Share Improve this question asked May 24, 2019 at 9:21 ZettaCirclZettaCircl 8731 gold badge10 silver badges16 bronze badges1 Answer
Reset to default 2It seems that sharp can't handle BMP pictures. (See : https://github./lovell/sharp/issues/1255 )
So I switch to Jimp (See : https://www.npmjs./package/jimp) :
console.log('Reducing image size ... ');
fs.readdirSync(input_folder).forEach(file => {
let tmp_input_path = path.join(input_folder, file)
let tmp_file = file.substr(0, file.lastIndexOf(".")) + ".png";
let tmp_output_path = path.join(tmp_folder_reduced, tmp_file)
if(fs.existsSync(tmp_input_path)){
console.log("File exist ! ")
}
//Resize
Jimp.read(tmp_input_path)
.then(image => {
image
.resize(target_width, Jimp.AUTO)
.write(tmp_output_path)
})
.catch(err => {
console.log("Error at reducing size / converting picture : ")
console.log(err)
console.log(tmp_input_path);
console.log(tmp_output_path);
});
本文标签:
版权声明:本文标题:javascript - NodeJS and Sharp, BMP to PNG Error: Input file contains unsupported image format - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745419939a2657855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论