SharpJS error: "Invalid SOS parameters for sequential JPEG"
Sharp: Error when processing images — VipsJpeg Invalid SOS parameters
Description: When processing images using the Sharp
library in Node.js, an error occurs during the file-saving stage.
Example Node.js code:
await Sharp(img_buffer)
.resize(width, height, { fit: "cover" })
.composite([compositeParams])
.jpeg({ mozjpeg: true })
.toFormat("jpeg", { progressive: true, quality: parseInt(quality) })
.sharpen()
.toFile(`${filePath}_${width}.jpeg`)
.catch((err) => {
console.log(err);
});
Error output in the console:
Error: VipsJpeg: Invalid SOS parameters for sequential JPEG
at Sharp.toFile (../node_modules/sharp/lib/output.js:89:19)
at transform (../utils/files.utils.js:410:6)
at async saveFilesTemporary (../utils/files.utils.js:234:11)
at async ../routes/an.router.js:1271:21
Presumably, this error occurs when processing JPEG files uploaded from Samsung devices.
Solution:
You need to pass the failOn: 'truncated'
option to Sharp. This allows Sharp to properly handle partially corrupted images.
Updated code:
await Sharp(img_buffer, { failOn: 'truncated' })
.resize(width, height, { fit: "cover" })
.composite([compositeParams])
.jpeg({ mozjpeg: true })
.toFormat("jpeg", { progressive: true, quality: parseInt(quality) })
.sharpen()
.toFile(`${filePath}_${width}.jpeg`)
.catch((err) => {
console.log(err);
});
Conclusion: Adding the failOn: 'truncated'
option enables safe processing of partially damaged images, especially those from Samsung devices, and eliminates the Invalid SOS parameters
error when saving JPEG files.