mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 07:15:08 +01:00
Allow PNG and WebP tile-based output in addition to JPEG (#622)
This commit is contained in:
committed by
Lovell Fuller
parent
6b426014ad
commit
bc84d1e47a
@@ -128,6 +128,22 @@ describe('Tile', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Valid formats pass', function () {
|
||||
['jpeg', 'png', 'webp'].forEach(function (format) {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp().toFormat(format).tile();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid formats fail', function () {
|
||||
['tiff', 'raw'].forEach(function (format) {
|
||||
assert.throws(function () {
|
||||
sharp().toFormat(format).tile();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Prevent larger overlap than default size', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tile({overlap: 257});
|
||||
@@ -224,6 +240,111 @@ describe('Tile', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout with jpeg format', function (done) {
|
||||
const directory = fixtures.path('output.jpg.google.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.jpeg({ quality: 1 })
|
||||
.tile({
|
||||
layout: 'google'
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
const sample = path.join(directory, '0', '0', '0.jpg');
|
||||
sharp(sample).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', metadata.format);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual(true, metadata.width === 256);
|
||||
assert.strictEqual(true, metadata.height === 256);
|
||||
fs.stat(sample, function (err, stat) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, stat.size < 2000);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout with png format', function (done) {
|
||||
const directory = fixtures.path('output.png.google.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.png({ compressionLevel: 1 })
|
||||
.tile({
|
||||
layout: 'google'
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
const sample = path.join(directory, '0', '0', '0.png');
|
||||
sharp(sample).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', metadata.format);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual(true, metadata.width === 256);
|
||||
assert.strictEqual(true, metadata.height === 256);
|
||||
fs.stat(sample, function (err, stat) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, stat.size > 44000);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout with webp format', function (done) {
|
||||
const directory = fixtures.path('output.webp.google.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.webp({ quality: 1 })
|
||||
.tile({
|
||||
layout: 'google'
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
const sample = path.join(directory, '0', '0', '0.webp');
|
||||
sharp(sample).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('webp', metadata.format);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual(true, metadata.width === 256);
|
||||
assert.strictEqual(true, metadata.height === 256);
|
||||
fs.stat(sample, function (err, stat) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, stat.size < 2000);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Write to ZIP container using file extension', function (done) {
|
||||
const container = fixtures.path('output.dz.container.zip');
|
||||
const extractTo = fixtures.path('output.dz.container');
|
||||
|
||||
Reference in New Issue
Block a user