diff --git a/lib/output.js b/lib/output.js index 2268ab3d..146f2a65 100644 --- a/lib/output.js +++ b/lib/output.js @@ -29,7 +29,7 @@ const formats = new Map([ ['jxl', 'jxl'] ]); -const jp2Regex = /\.jp[2x]|j2[kc]$/i; +const jp2Regex = /\.(jp[2x]|j2[kc])$/i; const errJp2Save = () => new Error('JP2 output requires libvips with support for OpenJPEG'); @@ -75,7 +75,7 @@ function toFile (fileOut, callback) { err = new Error('Missing output file path'); } else if (is.string(this.options.input.file) && path.resolve(this.options.input.file) === path.resolve(fileOut)) { err = new Error('Cannot use same file for input and output'); - } else if (jp2Regex.test(fileOut) && !this.constructor.format.jp2k.output.file) { + } else if (jp2Regex.test(path.extname(fileOut)) && !this.constructor.format.jp2k.output.file) { err = errJp2Save(); } if (err) { diff --git a/test/unit/jp2.js b/test/unit/jp2.js index 79efac6d..c70f1543 100644 --- a/test/unit/jp2.js +++ b/test/unit/jp2.js @@ -25,6 +25,13 @@ describe('JP2 output', () => { /JP2 output requires libvips with support for OpenJPEG/ ) ); + + it('File with JP2-like suffix should not fail due to missing OpenJPEG', () => { + const output = fixtures.path('output.failj2c'); + return assert.doesNotReject( + async () => sharp(fixtures.inputPngWithOneColor).toFile(output) + ); + }); } else { it('JP2 Buffer to PNG Buffer', () => { sharp(fs.readFileSync(fixtures.inputJp2))