-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d10866d
commit e500fe6
Showing
9 changed files
with
455 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
apps/romainlanz.com/app/common/controllers/compute_og_image_controllers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { inject } from '@adonisjs/core'; | ||
import vine from '@vinejs/vine'; | ||
import { OgImageGeneratorService } from '#common/services/og_image_generator_service'; | ||
import type { HttpContext } from '@adonisjs/core/http'; | ||
|
||
@inject() | ||
export default class ComputeOgImageControllers { | ||
static validator = vine.compile( | ||
vine.object({ | ||
title: vine.string().trim(), | ||
subtitle: vine.string().trim(), | ||
}) | ||
); | ||
|
||
constructor(private readonly ogImageGeneratorService: OgImageGeneratorService) {} | ||
|
||
async execute({ request, response }: HttpContext) { | ||
const { title, subtitle } = await request.validateUsing(ComputeOgImageControllers.validator); | ||
|
||
const image = await this.ogImageGeneratorService.generate(title, subtitle); | ||
|
||
return response.header('Content-Type', 'image/png').stream(image); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
apps/romainlanz.com/app/common/services/og_image_generator_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { readFile } from 'node:fs/promises'; | ||
import { assertExists } from '@adonisjs/core/helpers/assert'; | ||
import app from '@adonisjs/core/services/app'; | ||
import sharp, { type Sharp } from 'sharp'; | ||
|
||
export class OgImageGeneratorService { | ||
static #template: string | null = null; | ||
|
||
async #loadTemplate() { | ||
OgImageGeneratorService.#template = await readFile(app.makePath('resources/og_template.svg'), 'utf-8'); | ||
} | ||
|
||
async generate(title: string, subtitle: string): Promise<Sharp> { | ||
if (!OgImageGeneratorService.#template) { | ||
await this.#loadTemplate(); | ||
} | ||
|
||
assertExists(OgImageGeneratorService.#template, 'Template should be loaded'); | ||
|
||
console.log(title); | ||
|
||
const [title1, title2] = title | ||
.trim() | ||
.split(/(.{0,20})(?:\s|$)/g) | ||
.filter(Boolean); | ||
|
||
const [subtitle1, subtitle2, subtitle3] = subtitle | ||
.trim() | ||
.split(/(.{0,60})(?:\s|$)/g) | ||
.filter(Boolean); | ||
|
||
const generatedSvg = OgImageGeneratorService.#template | ||
.replace('{{ title }}', title1) | ||
.replace('{{ title2 }}', title2 || '') | ||
.replace('{{ subtitle }}', subtitle1) | ||
.replace('{{ subtitle2 }}', subtitle2 || '') | ||
.replace('{{ subtitle3 }}', subtitle3 || ''); | ||
|
||
return sharp(Buffer.from(generatedSvg)) | ||
.resize(1200 * 1.1, 630 * 1.1) | ||
.png(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.