Skip to content

Commit 4320505

Browse files
committed
feat(SLB-354): support png transparency
1 parent 0c151dd commit 4320505

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

packages/npm/@amazeelabs/image/src/server.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createHash } from 'node:crypto';
22
import { createWriteStream } from 'node:fs';
33
import { mkdir, mkdtemp, open, readFile, writeFile } from 'node:fs/promises';
44
import { tmpdir } from 'node:os';
5-
import { sep } from 'node:path';
5+
import { extname, sep } from 'node:path';
66
import { Readable } from 'node:stream';
77
import { finished } from 'node:stream/promises';
88

@@ -56,9 +56,10 @@ async function createDerivative(
5656
) {
5757
const outputDir = getSettings().outputDir;
5858
const outputPath = getSettings().outputPath;
59+
const ext = extname(filename);
5960
const fn = `${createHash('md5')
6061
.update(JSON.stringify({ ...target, filename, focalPoint }))
61-
.digest('hex')}.jpg`;
62+
.digest('hex')}.${ext === '.png' ? 'png' : 'jpg'}`;
6263

6364
const derivative = `${outputDir}/${fn}`;
6465
await mkdir(outputDir, { recursive: true });
@@ -81,7 +82,9 @@ async function createDerivative(
8182
fit: 'cover',
8283
position: sharp.strategy.attention,
8384
});
84-
const resized = await pipeline.toFormat('jpg').toBuffer();
85+
const resized = await pipeline
86+
.toFormat(ext === '.png' ? 'png' : 'jpg')
87+
.toBuffer();
8588
await writeFile(derivative, resized);
8689
}
8790
return `${outputPath}/${fn}`;
21.9 KB
Loading

packages/npm/@amazeelabs/image/test/src/pages/client.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export default function Index() {
3232
/>
3333
<h2>Remote file</h2>
3434
<Image src="http://localhost:9999/goats.jpg" width={500} />
35+
36+
<h2>PNG</h2>
37+
<Image src="http://localhost:9999/logo.png" width={500} />
3538
</ClientImageSettings>
3639
);
3740
}

packages/npm/@amazeelabs/image/test/src/pages/server.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export default function Index() {
3131
/>
3232
<h2>Remote file</h2>
3333
<Image src="http://localhost:9999/goats.jpg" width={500} />
34+
<h2>PNG</h2>
35+
<Image src="/logo.png" width={500} />
3436
</ImageSettings>
3537
);
3638
}

0 commit comments

Comments
 (0)