pixel-forge
Version:
A comprehensive generator for social media previews, favicons, and visual assets across all platforms
111 lines (110 loc) • 4.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TwitterGenerator = void 0;
const path_1 = __importDefault(require("path"));
const image_processor_1 = require("../../core/image-processor");
class TwitterGenerator {
constructor(sourceImage, config) {
this.config = config;
this.sourceImage = sourceImage;
}
/**
* Generate Twitter-optimized images
*/
async generate(options = {}) {
const { includeStandard = true, includeSquare = false, title, description, template = 'basic' } = options;
if (includeStandard) {
await this.generateStandardImage(title, description, template);
}
if (includeSquare) {
await this.generateSquareImage(title, description, template);
}
}
/**
* Generate standard Twitter image (1200x675)
*/
async generateStandardImage(title, description, template) {
const processor = new image_processor_1.ImageProcessor(this.sourceImage);
const outputPath = path_1.default.join(this.config.output.path, 'twitter-card.png');
const socialFile = await processor.createSocialPreview({
width: image_processor_1.ImageSizes.social.twitter.width,
height: image_processor_1.ImageSizes.social.twitter.height,
title,
description,
template,
background: this.config.backgroundColor
});
const finalProcessor = new image_processor_1.ImageProcessor(socialFile);
await finalProcessor.save(outputPath);
await processor.cleanup();
}
/**
* Generate square Twitter image (1200x1200)
*/
async generateSquareImage(title, description, template) {
const processor = new image_processor_1.ImageProcessor(this.sourceImage);
const outputPath = path_1.default.join(this.config.output.path, 'twitter-square.png');
const socialFile = await processor.createSocialPreview({
width: image_processor_1.ImageSizes.social.twitterSquare.width,
height: image_processor_1.ImageSizes.social.twitterSquare.height,
title,
description,
template,
background: this.config.backgroundColor
});
const finalProcessor = new image_processor_1.ImageProcessor(socialFile);
await finalProcessor.save(outputPath);
await processor.cleanup();
}
/**
* Get HTML meta tags for Twitter
*/
getMetaTags(cardType = 'summary_large_image') {
const prefix = this.config.output.prefix || '/';
const image = cardType === 'summary' ? 'twitter-square.png' : 'twitter.png';
const size = cardType === 'summary' ? image_processor_1.ImageSizes.social.twitterSquare : image_processor_1.ImageSizes.social.twitter;
return [
`<meta name="twitter:card" content="${cardType}">`,
`<meta name="twitter:image" content="${prefix}${image}">`,
`<meta name="twitter:image:width" content="${size.width}">`,
`<meta name="twitter:image:height" content="${size.height}">`,
`<meta name="twitter:title" content="${this.config.appName}">`,
`<meta name="twitter:description" content="${this.config.description || ''}">`,
`<meta name="twitter:site" content="">`, // Can be configured later
`<meta name="twitter:creator" content="">`, // Can be configured later
];
}
/**
* Get Next.js metadata configuration for Twitter
*/
getNextMetadata(cardType = 'summary_large_image') {
const prefix = this.config.output.prefix || '/';
const image = cardType === 'summary' ? 'twitter-square.png' : 'twitter.png';
const size = cardType === 'summary' ? image_processor_1.ImageSizes.social.twitterSquare : image_processor_1.ImageSizes.social.twitter;
return {
twitter: {
card: cardType,
title: this.config.appName,
description: this.config.description,
images: [
{
url: `${prefix}${image}`,
width: size.width,
height: size.height,
alt: this.config.appName,
}
],
},
};
}
/**
* Get list of generated files
*/
getGeneratedFiles() {
return ['twitter.png', 'twitter-square.png'];
}
}
exports.TwitterGenerator = TwitterGenerator;