Skip to content

Commit

Permalink
Remove gif rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
minetoblend authored Aug 14, 2024
1 parent efda127 commit dbcf943
Showing 1 changed file with 0 additions and 91 deletions.
91 changes: 0 additions & 91 deletions src/services/cardRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import * as fs from 'fs';
import { v4 as uuid } from 'uuid';
import { Card } from '../entities/card.js';
import { Mapper } from '../entities/mapper.js';
import { GIF } from './GIF.js';
import { exec } from 'child_process';

const fontPath = 'node_modules/@expo-google-fonts/nunito-sans'

Expand All @@ -31,95 +29,6 @@ interface Options {
}

export async function renderCard(card: Card, options: Options = {}) {
try {
if (card.mapper.avatarUrl.endsWith('.gif')) {
console.log('loading gif')
const gif = GIF();
gif.load(card.mapper.avatarUrl);
await new Promise<void>((resolve, reject) => {
gif.onload = () => {
resolve();
};
gif.onerror = (e) => {
reject(e);
};
setTimeout(() => reject(), 1000)
});

console.log('loaded gif')

let images: string[] = [];

const outputDir = path.resolve(tmpdir, uuid());
fs.mkdirSync(outputDir, { recursive: true });

const avgFrameDelay = gif.frames.reduce((acc, frame) => acc + (frame as { delay: number }).delay, 0) / gif.frames.length;
const fps = Math.round(1000 / avgFrameDelay);
console.log({ fps, frames: gif.frames.length });

if(gif.frameCount > 50) throw new Error('GIF too long');
if(gif.frameCount === 1) throw new Error('GIF too short');

let i = 0;
for (const frame of gif.frames) {
const canvas = createCanvas(paddedCardSize.width, paddedCardSize.height);
const ctx = canvas.getContext('2d');

await drawCard(ctx, card, options);

ctx.save();
ctx.translate(20, 20);

ctx.save();
ctx.beginPath();
ctx.roundRect(0, 0, 256, 256 - 2, [4, 4, 0, 0]);
ctx.clip();
ctx.drawImage((frame as any).image, 0, 0, 256, 256);
ctx.restore();

ctx.restore();

const imagePath = path.resolve(outputDir, i++ + '.png');
const stream = canvas.createPNGStream();
const out = fs.createWriteStream(imagePath, { flags: 'a' });

stream.pipe(out);

await new Promise((resolve, reject) => {
out.on('finish', resolve);
out.on('error', reject);
});

images.push(imagePath);
}

const outputPath = path.resolve(tmpdir, uuid() + '.gif');
await new Promise<void>((resolve, reject) => {
exec(`ffmpeg -framerate ${fps} -i ${outputDir}/%d.png -vf palettegen ${outputDir}/palette.png`, (err) => {
if (err) {
reject(err);
} else {
exec(
`ffmpeg -framerate ${fps} -i ${outputDir}/%d.png -i ${outputDir}/palette.png -lavfi paletteuse -y ${outputPath}`,
(err) => {
if (err) {
reject(err);
} else {
resolve();
}
}
);
}
});
});

return outputPath;
}
} catch (e) {
console.error(e);
// fallback to static image
}

const canvas = createCanvas(paddedCardSize.width, paddedCardSize.height);

const ctx = canvas.getContext('2d');
Expand Down

0 comments on commit dbcf943

Please sign in to comment.