Initial commit: Plant Disease Identification app
- Next.js 16 App Router project with Tailwind CSS - Plant disease knowledge base (93 diseases, 25 plants) - Image upload with client+server preprocessing - ML inference pipeline with mock/demo fallback - Responsive results page with disease cards and treatment - Full test suite (285 passing tests)
This commit is contained in:
51
apps/web/src/lib/server/image-processing-server.ts
Normal file
51
apps/web/src/lib/server/image-processing-server.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Server-only image processing helpers.
|
||||
*
|
||||
* These functions use Node.js native modules (sharp) and must NOT be
|
||||
* imported by client components. They are used exclusively by API
|
||||
* route handlers (server-side).
|
||||
*/
|
||||
|
||||
// ─── Resize ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Server-side image resize using sharp (if available) or a fallback.
|
||||
* This is used by the upload API route.
|
||||
*
|
||||
* @param buffer - Raw image buffer
|
||||
* @param size - Target dimension
|
||||
* @returns Promise<Buffer> resized image as JPEG
|
||||
*/
|
||||
export async function resizeImageServer(
|
||||
buffer: Buffer,
|
||||
size: number,
|
||||
): Promise<Buffer> {
|
||||
try {
|
||||
const sharpModule = await import("sharp");
|
||||
return sharpModule.default(buffer)
|
||||
.resize(size, size)
|
||||
.jpeg({ quality: 95 })
|
||||
.toBuffer();
|
||||
} catch {
|
||||
// Fallback: return original buffer if sharp is not available
|
||||
// In production, sharp should be installed
|
||||
throw new Error(
|
||||
"sharp is required for server-side image resizing. Install with: npm install sharp",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── MIME Type Helpers ───────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Generate a file extension from a MIME type.
|
||||
*/
|
||||
export function mimeTypeToExtension(mimeType: string): string {
|
||||
const map: Record<string, string> = {
|
||||
"image/png": "png",
|
||||
"image/jpeg": "jpg",
|
||||
"image/jpg": "jpg",
|
||||
"image/webp": "webp",
|
||||
};
|
||||
return map[mimeType] || "jpg";
|
||||
}
|
||||
Reference in New Issue
Block a user