|
|
|
|
|
|
|
|
import theme from 'shared/theme'; |
|
|
import { btoa } from 'b2a'; |
|
|
import { stringify } from 'query-string'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const WIDTH = 1500; |
|
|
const IMGIX_TEXT_ENDPOINT = 'https://assets.imgix.net/~text'; |
|
|
|
|
|
const TITLE_PARAMS = { |
|
|
w: WIDTH * 0.8, |
|
|
h: 270, |
|
|
txtsize: 56, |
|
|
txtlead: 16, |
|
|
txtfont: 'Helvetica,Bold', |
|
|
txtalign: 'left,middle', |
|
|
txtcolor: 'ffffff', |
|
|
|
|
|
txtclip: 'end,ellipsis', |
|
|
}; |
|
|
|
|
|
const FOOTER_PARAMS = { |
|
|
h: 64, |
|
|
txtsize: 36, |
|
|
txtcolor: theme.brand.default.replace('#', ''), |
|
|
txtalign: 'right,middle', |
|
|
txtfont: 'Helvetica,Bold', |
|
|
}; |
|
|
|
|
|
const BACKGROUND_URL = `https://spectrum.imgix.net/default_images/twitter-share-card.png`; |
|
|
|
|
|
type GetMetaImageInput = { |
|
|
title: string, |
|
|
footer: string, |
|
|
}; |
|
|
|
|
|
const generateImageFromText = ({ |
|
|
title, |
|
|
footer, |
|
|
}: GetMetaImageInput): ?string => { |
|
|
const base64title = btoa(title); |
|
|
const base64footer = btoa(footer); |
|
|
if (!base64title || !base64footer) return null; |
|
|
|
|
|
const titleUrl = `${IMGIX_TEXT_ENDPOINT}?${stringify( |
|
|
{ ...TITLE_PARAMS, txt64: base64title.replace(/=/g, '') }, |
|
|
{ encode: false } |
|
|
)}`; |
|
|
const footerUrl = `${IMGIX_TEXT_ENDPOINT}?${stringify( |
|
|
{ ...FOOTER_PARAMS, txt64: base64footer.replace(/=/g, '') }, |
|
|
{ encode: false } |
|
|
)}`; |
|
|
|
|
|
const base64titleurl = btoa(titleUrl); |
|
|
const base64footerurl = btoa(footerUrl); |
|
|
|
|
|
if (!base64titleurl || !base64footerurl) return null; |
|
|
|
|
|
const BACKGROUND_PARAMS = { |
|
|
w: WIDTH, |
|
|
bm: 'normal', |
|
|
by: 170, |
|
|
bx: 170, |
|
|
markalign: 'left,bottom', |
|
|
markpad: 24, |
|
|
markx: 140, |
|
|
blend64: btoa(titleUrl).replace(/=/g, ''), |
|
|
mark64: btoa(footerUrl).replace(/=/g, ''), |
|
|
}; |
|
|
|
|
|
return `${BACKGROUND_URL}?${stringify(BACKGROUND_PARAMS, { encode: false })}`; |
|
|
}; |
|
|
|
|
|
export default generateImageFromText; |
|
|
|