UNPKG

@nanggo/social-preview

Version:

Generate beautiful social media preview images from any URL

103 lines (102 loc) 5.08 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.processImageForTemplate = processImageForTemplate; const validators_1 = require("../utils/validators"); const logger_1 = require("../utils/logger"); const image_security_1 = require("../utils/image-security"); const metadata_extractor_1 = require("./metadata-extractor"); const image_generator_1 = require("./image-generator"); // Pre-load image security module at module level for performance const imageSecurityPromise = Promise.resolve().then(() => __importStar(require('../utils/image-security'))); async function processImageForTemplate(metadata, template, width, height, options) { // Check if template wants no background image if (template.layout.imagePosition === 'none') { // Use transparent canvas if template requires it, otherwise use blank canvas if (template.imageProcessing?.requiresTransparentCanvas) { return (0, validators_1.createTransparentCanvas)(width, height); } return await (0, image_generator_1.createBlankCanvas)(width, height, options); } // If no image available, handle based on template configuration if (!metadata.image) { // Use transparent canvas if template requires it for custom backgrounds if (template.imageProcessing?.requiresTransparentCanvas) { return (0, validators_1.createTransparentCanvas)(width, height); } return await (0, image_generator_1.createBlankCanvas)(width, height, options); } // Process background image with template-specific effects try { const imageBuffer = await (0, metadata_extractor_1.fetchImage)(metadata.image, options.security); // Use withSecureSharp for automatic pool management const { withSecureSharp } = await imageSecurityPromise; return await withSecureSharp(imageBuffer, async (secureImage) => { let processedImage = (0, image_security_1.secureResize)(secureImage, width, height, { fit: 'cover', position: 'center', }); // Apply template-specific effects in optimized pipeline const blurRadius = template.effects?.blur?.radius ?? 0; const brightnessValue = template.imageProcessing?.brightness ?? 1.0; const saturationValue = template.imageProcessing?.saturation; // Apply blur first if needed if (blurRadius > 0) { processedImage = processedImage.blur(blurRadius); } // Apply brightness and saturation together for efficiency if (brightnessValue !== 1.0 || saturationValue !== undefined) { const modulateOptions = {}; if (brightnessValue !== 1.0) { modulateOptions.brightness = brightnessValue; } if (saturationValue !== undefined) { modulateOptions.saturation = saturationValue; } processedImage = processedImage.modulate(modulateOptions); } return processedImage; }); } catch (fetchError) { // If image fetch fails, create appropriate canvas based on template configuration (0, logger_1.logImageFetchError)(metadata.image, fetchError instanceof Error ? fetchError : new Error(String(fetchError))); // Use transparent canvas if template requires it for custom backgrounds if (template.imageProcessing?.requiresTransparentCanvas) { return (0, validators_1.createTransparentCanvas)(width, height); } return await (0, image_generator_1.createBlankCanvas)(width, height, options); } }