Skip to content

Commit 2c24269

Browse files
committed
fix resolution option for PNG export
1 parent 2cf57e0 commit 2c24269

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
99
==================
1010
### Changed
1111
### Added
12+
* (Actually) added `resolution` option for `canvas.toBuffer("image/png")` and
13+
`canvas.createPNGStream()`. This was documented since 2.0.0 but not working.
1214
### Fixed
1315
* PDF metadata (added in 2.3.0) wasn't being set with `canvas.createPDFStream()`
1416
* Fix custom "inspect" function deprecation warnings (#1326)
@@ -172,8 +174,8 @@ context.quality = 'best' // new
172174
* Support for `canvas.toBuffer("image/jpeg")`
173175
* Unified configuration options for `canvas.toBuffer()`, `canvas.pngStream()`
174176
and `canvas.jpegStream()`
175-
* Added `resolution` option for `canvas.toBuffer("image/png")` and
176-
`canvas.createPNGStream()`
177+
* ~~Added `resolution` option for `canvas.toBuffer("image/png")` and
178+
`canvas.createPNGStream()`~~ this was not working
177179
* Support for `canvas.toDataURI("image/jpeg")` (sync)
178180
* Support for `img.src = <url>` to match browsers
179181
* Support reading data URL on `img.src`

src/Canvas.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ static void parsePNGArgs(Local<Value> arg, PngClosure& pngargs) {
250250
if (val <= 9) pngargs.compressionLevel = val;
251251
}
252252

253+
Local<Value> rez = obj->Get(Nan::New("resolution").ToLocalChecked());
254+
if (rez->IsUint32()) {
255+
uint32_t val = Nan::To<uint32_t>(rez).FromMaybe(0);
256+
if (val > 0) pngargs.resolution = val;
257+
}
258+
253259
Local<Value> filters = obj->Get(Nan::New("filters").ToLocalChecked());
254260
if (filters->IsUint32()) pngargs.filters = Nan::To<uint32_t>(filters).FromMaybe(0);
255261

test/canvas.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,13 @@ describe('Canvas', function () {
533533
it('Canvas#toBuffer("image/png", {resolution: 96})', function () {
534534
const buf = createCanvas(200, 200).toBuffer('image/png', {resolution: 96});
535535
// 3780 ppm ~= 96 ppi
536+
let foundpHYs = false;
536537
for (let i = 0; i < buf.length - 12; i++) {
537538
if (buf[i] === 0x70 &&
538539
buf[i + 1] === 0x48 &&
539540
buf[i + 2] === 0x59 &&
540541
buf[i + 3] === 0x73) { // pHYs
542+
foundpHYs = true;
541543
assert.equal(buf[i + 4], 0);
542544
assert.equal(buf[i + 5], 0);
543545
assert.equal(buf[i + 6], 0x0e);
@@ -548,6 +550,7 @@ describe('Canvas', function () {
548550
assert.equal(buf[i + 11], 0xc4); // y
549551
}
550552
}
553+
assert.ok(foundpHYs, "missing pHYs header");
551554
})
552555

553556
it('Canvas#toBuffer("image/png", {compressionLevel: 5})', function () {

0 commit comments

Comments
 (0)