Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  196] [ 6]  / answers: 1 / hits: 8193  / 5 Years ago, fri, may 24, 2019, 12:00:00

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 completed.');


enter



I'm getting this error :



Reducing image size ... 
Image reduction completed.
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 computed server-side (so no encoding problems, as I could have seen somewhere else concerning this problem).



Would someone have an idea or a solution ?


More From » node.js

 Answers
1

It seems that sharp can't handle BMP pictures. (See : https://github.com/lovell/sharp/issues/1255 )



So I switch to Jimp (See : https://www.npmjs.com/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);
});

[#7528] Wednesday, May 22, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kourtney

Total Points: 368
Total Questions: 103
Total Answers: 85

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
kourtney questions
Sun, Oct 4, 20, 00:00, 4 Years ago
Tue, Oct 29, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Fri, Mar 1, 19, 00:00, 5 Years ago
;