Files
clube67_newwhats.local/clube67/newwhats.local/plugins/SmartVision/backend/Optimizer.js
T
VPS 4 Deploy Agent 2f8c04a0a7
continuous-integration/webhook Falha no deploy de clube67_newwhats.local (VPS 4)
chore(ops): restore all source files in newwhats.clube67.com
2026-05-18 03:28:29 +02:00

60 lines
2.4 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageOptimizer = void 0;
const sharp_1 = __importDefault(require("sharp"));
class ImageOptimizer {
/**
* Optimizes an image: converts to WebP, strips metadata,
* and applies a 1:1 Smart Crop if needed.
*/
static async optimize(buffer, category = 'media', options = {}) {
try {
let pipeline = (0, sharp_1.default)(buffer);
const metadata = await pipeline.metadata();
if (!metadata.format)
return { buffer, mimetype: 'application/octet-stream' };
// 1. Identify Target Size
// Default sizes based on category
let targetSize = options.size || 1024;
if (category === 'partners' || category === 'users')
targetSize = 400;
if (category === 'benefits')
targetSize = 800;
// 2. Smart Crop 1:1 if aspect=1:1 is requested or implied by category
const shouldCrop = options.aspect === '1:1' || ['partners', 'users'].includes(category);
if (shouldCrop) {
pipeline = pipeline.resize({
width: targetSize,
height: targetSize,
fit: sharp_1.default.fit.cover,
position: sharp_1.default.strategy.entropy // Smart focal point detection
});
}
else {
// Resize without cropping if it's too large
pipeline = pipeline.resize({
width: targetSize,
withoutEnlargement: true,
fit: sharp_1.default.fit.inside
});
}
// 3. Convert to WebP & Strip Metadata
const optimizedBuffer = await pipeline
.webp({ quality: 85, effort: 4 })
.toBuffer();
return {
buffer: optimizedBuffer,
mimetype: 'image/webp'
};
}
catch (err) {
console.error('[SmartVision] Optimization failed:', err);
return { buffer, mimetype: 'image/jpeg' }; // Fallback
}
}
}
exports.ImageOptimizer = ImageOptimizer;
//# sourceMappingURL=Optimizer.js.map