import { NextRequest, NextResponse } from 'next/server'; import { processImageWithSmartCrop } from '@mintel/image-processor'; export async function GET(request: NextRequest) { const { searchParams } = new URL(request.url); const url = searchParams.get('url'); let width = parseInt(searchParams.get('w') || '800'); let height = parseInt(searchParams.get('h') || '600'); let q = parseInt(searchParams.get('q') || '80'); if (!url) { return NextResponse.json({ error: 'Missing url parameter' }, { status: 400 }); } try { // 1. Fetch image from original URL const response = await fetch(url); if (!response.ok) { return NextResponse.json({ error: 'Failed to fetch original image' }, { status: response.status }); } const arrayBuffer = await response.arrayBuffer(); const buffer = Buffer.from(arrayBuffer); // 2. Process image with Face-API and Sharp const processedBuffer = await processImageWithSmartCrop(buffer, { width, height, format: 'webp', quality: q, }); // 3. Return the processed image return new NextResponse(new Uint8Array(processedBuffer), { status: 200, headers: { 'Content-Type': 'image/webp', 'Cache-Control': 'public, max-age=31536000, immutable', }, }); } catch (error) { console.error('Image Processing Error:', error); return NextResponse.json({ error: 'Failed to process image' }, { status: 500 }); } }