What is the output of running npx envinfo --binaries --languages --system --utilities?
System:
OS: Windows 10
CPU: (16) x64 Intel(R) Xeon(R) CPU E5-2667 v2 @ 3.30GHz
Memory: 22.53 GB / 31.93 GB
Binaries:
Node: 10.15.0 - C:\Program Files\nodejs\node.EXE
npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
What are the steps to reproduce?
Run the same image through the same sharp function. One with 1080px as maximum size and the other with 500px as the maximum size.
What is the expected behaviour?
I'm trying to let Sharp create a 'full resolution' image and a thumbnail. So I'd pass the image to my function and get two images (full-res and thumbnail).
Are you able to provide a standalone code sample, without other dependencies, that demonstrates this problem?
My current function is the following:
function resize(input, output, width, height) {
sharp(input).resize(width, height, {
fit: sharp.fit.inside,
withoutEnlargement: true
}).png({
compressionLevel: 4
}).toBuffer((err, buffer) => {
fs.writeFile(output, buffer, function (e) {
if (e) {
throw e
}
});
})
}
This function gets called as following
let dir = path.join(__dirname, '../public/images', file.filename)
let full_dir = path.join(__dirname, '../public/images/full', file.filename)
resize(file.path.toString(), full_dir.toString(), 1080, 1080)
resize(file.path.toString(), dir.toString(), 500, 500)
Are you able to provide a sample image that helps explain the problem?
This is the output image in the full-res folder (this behaviour is only when making the full-res version)

Hi, there doesn't appear to be any error handling relating to the err passed to the toBuffer callback. Perhaps add something like the following:
}).toBuffer((err, buffer) => {
+ if (!err) {
fs.writeFile(output, buffer, function (e) {
What are the values of the 4 bytes?
After adding the error handling I get this:
throw err
^
Error: VipsJpeg: Premature end of JPEG file
VipsJpeg: out of order read at line 864
vips2png: unable to write to buffer
Without the error handling, the file gets created and just has 'null' in it.
Thanks, this means the input file is corrupt. If you trust the input, you may be able to still get some form of sensible output by setting the failOnError property to false.
https://sharp.pixelplumbing.com/en/stable/api-constructor/#parameters
- sharp(input).resize(width, height, {
+ sharp(input, { failOnError: false }).resize(width, height, {
After adding the failOnError everything seems to be fine, but I'll do some more testing to see whether it comes back or not.
Most helpful comment
Thanks, this means the input file is corrupt. If you trust the input, you may be able to still get some form of sensible output by setting the
failOnErrorproperty tofalse.https://sharp.pixelplumbing.com/en/stable/api-constructor/#parameters