Skip to content

Commit

Permalink
Fix support for JPEG & WebP (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis authored Apr 18, 2024
1 parent a969471 commit e66e580
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-chicken-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro-og-canvas': minor
---

Switches to using the “full” `canvaskit-wasm` build to generate images. This fixes support for rendering as JPEG or WEBP instead of the default PNG.
11 changes: 11 additions & 0 deletions .changeset/three-pigs-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'astro-og-canvas': minor
---

Updates `canvaskit-wasm` to the latest release

**Note:** pnpm users may need to manually update in their project too:

```sh
pnpm i canvaskit-wasm@^0.39.1
```
31 changes: 31 additions & 0 deletions demo/src/pages/formats/[path].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { OGImageRoute } from 'astro-og-canvas';

export const { getStaticPaths, GET } = OGImageRoute({
param: 'path',
pages: {
'png.md': {
title: 'Test image (PNG)',
description: 'Renders an Open Graph image to a PNG file',
format: 'PNG',
},
'jpeg.md': {
title: 'Test image (JPEG)',
description: 'Renders an Open Graph image to a JPEG file',
format: 'JPEG',
},
'webp.md': {
title: 'Test image (WEBP)',
description: 'Renders an Open Graph image to a WEBP file',
format: 'WEBP',
},
},
getSlug(path, { format }) {
const ext = format.toLowerCase();
path = path.replace(/^\/src\/pages\//, '');
path = path.replace(/\.[^.]*$/, '') + '.' + ext;
return path;
},
getImageOptions: (_path, page) => ({
...page,
}),
});
17 changes: 17 additions & 0 deletions demo/src/pages/formats/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Different formats
description: Test page demonstrating that images render correctly in different formats
layout: ../../layouts/Layout.astro
---

# Astro OpenGraph Images with CanvasKit

This test renders the same image to different formats.

![Example PNG](/formats/png.png)

![Example JPEG](/formats/jpeg.jpeg)

![Example WEBP](/formats/webp.webp)

- [Home](/)
1 change: 1 addition & 0 deletions demo/src/pages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ This test project uses `astro-og-canvas` to generate OpenGraph images from code.
- [Font Tests](/font-test)
- [Local font test](/local-font-test)
- [Background image tests](/background-test)
- [Different formats](/formats)
32 changes: 24 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/astro-og-canvas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"canvaskit-wasm": "^0.37.0",
"canvaskit-wasm": "^0.39.1",
"deterministic-object-hash": "^2.0.2",
"entities": "^4.4.0"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/astro-og-canvas/src/assetLoaders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { FontMgr } from 'canvaskit-wasm';
import init from 'canvaskit-wasm';
import init, { type FontMgr } from 'canvaskit-wasm/full';
import fs from 'node:fs/promises';
import { createRequire } from 'node:module';
import { shorthash } from './shorthash';
Expand All @@ -11,7 +10,7 @@ const error = (...args: any[]) => console.error('[astro-og-canvas]', ...args);
/** CanvasKit singleton. */
export const CanvasKitPromise = init({
// TODO: Figure how to reliably resolve this without depending on Node.
locateFile: (file) => resolve(`canvaskit-wasm/bin/${file}`),
locateFile: (file) => resolve(`canvaskit-wasm/bin/full/${file}`),
});

class FontManager {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro-og-canvas/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CanvasKit } from 'canvaskit-wasm';
import type { CanvasKit } from 'canvaskit-wasm/full';

export type RGBColor = [r: number, g: number, b: number];
export type XYWH = [x: number, y: number, w: number, h: number];
Expand Down
5 changes: 5 additions & 0 deletions test/astro-auto-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ test('it should have built the index page correctly', () => {
assert.match(page, /<img src="\/og\/index.png" alt="Example image">/);
});

test('it should have rendered a JPEG correctly', () => {
const buff = loadImage('/formats/jpeg.jpeg');
assert.not.equal(buff.length, 0);
})

test.run();

0 comments on commit e66e580

Please sign in to comment.