Untitled
unknown
plain_text
10 months ago
3.0 kB
9
Indexable
const { createCanvas, loadImage, registerFont } = require('canvas');
const fs = require('fs');
// Đăng ký font tùy chỉnh
registerFont('./custom-font.otf', { family: 'CustomFont' });
async function generateMatchImage({ league, round, date, time, homeTeam, homeLogo, awayTeam, awayLogo, uniscoreLogo, outputPath }) {
const canvas = createCanvas(1200, 628);
const ctx = canvas.getContext('2d');
// Background
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Header background
ctx.fillStyle = '#F8F9FA';
ctx.fillRect(0, 0, canvas.width, 100);
// League and round text
ctx.fillStyle = '#000000';
ctx.font = 'bold 24px CustomFont'; // Sử dụng font tùy chỉnh
ctx.textAlign = 'left';
ctx.fillText(league, 20, 60);
ctx.font = 'normal 20px CustomFont';
ctx.fillText(`Round ${round}`, 200, 60);
// Date and time
ctx.font = 'bold 24px CustomFont';
ctx.textAlign = 'right';
ctx.fillText(`${date}, ${time} GMT`, canvas.width - 20, 60);
// Match separator line
ctx.strokeStyle = '#E0E0E0';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0, 100);
ctx.lineTo(canvas.width, 100);
ctx.stroke();
// Team logos and names
const homeLogoImg = await loadImage(homeLogo);
const awayLogoImg = await loadImage(awayLogo);
// Home team logo
const logoSize = 120;
ctx.drawImage(homeLogoImg, 200, 200, logoSize, logoSize);
// Away team logo
ctx.drawImage(awayLogoImg, canvas.width - 200 - logoSize, 200, logoSize, logoSize);
// Home team name
ctx.textAlign = 'center';
ctx.font = 'bold 32px CustomFont';
ctx.fillStyle = '#000000';
ctx.fillText(homeTeam, 260, 350);
// Away team name
ctx.fillText(awayTeam, canvas.width - 260, 350);
// Time in center
ctx.font = 'bold 48px CustomFont';
ctx.fillText(time, canvas.width / 2, 260);
// Add Uniscore logo
const uniscoreLogoImg = await loadImage(uniscoreLogo);
const uniscoreLogoWidth = 200; // Adjust the logo size
const uniscoreLogoHeight = 170; // Adjust the logo size proportionally
ctx.drawImage(
uniscoreLogoImg,
(canvas.width - uniscoreLogoWidth) / 2,
canvas.height - uniscoreLogoHeight - 20, // Positioned near the bottom with padding
uniscoreLogoWidth,
uniscoreLogoHeight
);
// Save image to file
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync(outputPath, buffer);
console.log(`Image generated at ${outputPath}`);
}
// Example usage
generateMatchImage({
league: 'Serie A',
round: 22,
date: 'Sat, 25 Jan 2025',
time: '19:45',
homeTeam: 'Empoli',
homeLogo: 'logo/empoli.png', // Replace with actual paths to logo files
awayTeam: 'Bologna',
awayLogo: 'logo/bologna.png', // Replace with actual paths to logo files
uniscoreLogo: 'logo/uniscore.png', // Replace with the path to the Uniscore logo
outputPath: './match_with_uniscore.png'
});
Editor is loading...
Leave a Comment