11/* eslint-disable import/no-nodejs-modules, no-restricted-globals */
22
3- import Jimp from 'jimp' ;
3+ import { Jimp , JimpMime , loadFont , measureText } from 'jimp' ;
44import { resolve } from 'path' ;
55import sharp from 'sharp' ;
66
77import { BASE_SEO_DIRECTORY , CANVAS_WIDTH , JIMP_FONTS } from './constants' ;
88import type { Fields } from '../snaps' ;
99
10- export type Font = Awaited < ReturnType < typeof Jimp . loadFont > > ;
10+ export type Font = Awaited < ReturnType < typeof loadFont > > ;
1111
1212export type Snap = Fields < Queries . Snap , 'name' > & { icon ?: string } ;
1313export type SnapWithIcon = Snap & { icon : string } ;
@@ -21,14 +21,11 @@ export type SnapWithIcon = Snap & { icon: string };
2121 */
2222export async function getFonts ( name : string , author = '' ) {
2323 for ( const { maxWidth, black, grey } of JIMP_FONTS ) {
24- const font = await Jimp . loadFont ( black ) ;
25- const width = Math . max (
26- Jimp . measureText ( font , name ) ,
27- Jimp . measureText ( font , author ) ,
28- ) ;
24+ const font = await loadFont ( black ) ;
25+ const width = Math . max ( measureText ( font , name ) , measureText ( font , author ) ) ;
2926
3027 if ( width <= maxWidth ) {
31- return Promise . all ( [ Jimp . loadFont ( black ) , Jimp . loadFont ( grey ) ] ) ;
28+ return Promise . all ( [ loadFont ( black ) , loadFont ( grey ) ] ) ;
3229 }
3330 }
3431
@@ -76,10 +73,16 @@ export function normalizeName(name: string) {
7673 * @returns The buffer of the rendered image.
7774 */
7875export async function getText ( value : string , font : Font ) {
79- const image = await Jimp . create ( 700 , 300 ) ;
80- image . print ( font , 0 , 0 , normalizeName ( value ) , 700 ) ;
76+ const image = new Jimp ( { width : 700 , height : 300 } ) ;
77+ image . print ( {
78+ font,
79+ x : 0 ,
80+ y : 0 ,
81+ text : normalizeName ( value ) ,
82+ maxWidth : 700 ,
83+ } ) ;
8184
82- return image . getBufferAsync ( Jimp . MIME_PNG ) ;
85+ return image . getBuffer ( JimpMime . png ) ;
8386}
8487
8588/**
0 commit comments