Skip to content

Commit

Permalink
fix: image text wrong padding
Browse files Browse the repository at this point in the history
  • Loading branch information
kenryu42 committed Feb 19, 2023
1 parent 130ef7e commit ee47e43
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const parseImage = async (image: string): Promise<sharp.Sharp> => {
} else {
const arrayBuffer = await getArrayBuffer(image);

return sharp(arrayBuffer, { animated: true });
return sharp(arrayBuffer);
}
};

Expand Down Expand Up @@ -182,7 +182,8 @@ const createSwapGif = async (swap: Swap, config: Config): Promise<Buffer> => {
config.apiAuth
);
const sharpImage = await parseImage(tokenData.image);
const quantityText = asset.amount ?? 0 > 1 ? `${asset.amount}` : '';
const quantityText =
(asset.amount ?? 0) > 1 ? `${asset.amount}` : '';

asset.image = tokenData.image;

Expand Down Expand Up @@ -271,29 +272,20 @@ const addQuantityToImage = async (
image: sharp.Sharp,
quantity: string
): Promise<Buffer> => {
const metadata = await image.metadata();

if (!metadata.width) {
throw log.throwMissingArgumentError('image.metadata.width', {
location: Logger.location.IMAGE_ADD_QUANTITY_TO_IMAGE
});
}

const tokenIdImage = await sharp({
const quantityImage = await sharp({
text: {
text: `<span foreground="#B0C4DE" size="medium"><b>Amount: ${quantity}</b></span>`,
font: 'Roboto',
rgba: true,
dpi: 150
}
})
.png()
.toBuffer();
const padding = quantity.toString().length * 20 + 195;
const padding = quantity.toString().length * 20 + 125;

return image
.composite([
{ input: tokenIdImage, top: 20, left: metadata.width - padding }
{ input: quantityImage, top: 20, left: IMAGE_WIDTH - padding }
])
.resize(IMAGE_WIDTH)
.png()
Expand Down Expand Up @@ -321,7 +313,6 @@ const createTextImage = async (text: string) => {
const textImage = await sharp({
text: {
text: `<span foreground="black" size="x-large"><b>${text}</b></span>`,
font: 'Roboto',
align: 'center',
rgba: true,
dpi: 150
Expand Down

0 comments on commit ee47e43

Please sign in to comment.