diff --git a/package.json b/package.json index 792ddde9..14b1299b 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "framer-motion": "^11.2.13", "fs": "^0.0.1-security", "iron-session": "^8.0.2", + "jimp": "^1.6.0", "js-cookie": "^3.0.5", "mathjs": "^13.0.2", "mssql": "^11.0.1", diff --git a/src/app/api/image/canvas/route.js b/src/app/api/image/canvas/route.js index 77d1c95c..317c84a4 100644 --- a/src/app/api/image/canvas/route.js +++ b/src/app/api/image/canvas/route.js @@ -2,6 +2,8 @@ import { NextResponse } from 'next/server' import { S3Client, PutObjectCommand, DeleteObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3' import sharp from 'sharp' import { v4 as uuidv4 } from 'uuid' +import { Jimp } from 'jimp' + const Bucket = process.env.AMPLIFY_BUCKET const s3 = new S3Client({ region: process.env.AWS_REGION, @@ -33,27 +35,39 @@ const cropImage = async (Key, width, height, left, top) => { }), ) + const image = await Jimp.read(Body) + image.autocrop({ tolerance: 0.0002, leaveBorder: 10 }) + const editedBuffer = await image.getBufferAsync(Jimp.MIME_PNG) + + return editedBuffer + // Convert stream to buffer - const chunks = [] - for await (const chunk of Body) { - chunks.push(chunk) - } - const imageBuffer = Buffer.concat(chunks) + // const chunks = [] + // for await (const chunk of Body) { + // chunks.push(chunk) + // } + // const imageBuffer = Buffer.concat(chunks) + + // const image = await Jimp.read(Body) + + // if (!checkResult) { + // processedImage = await image.toBuffer() + // } let processedImage - if (!checkResult) { - processedImage = await sharp(imageBuffer).toBuffer() - } else { - processedImage = await sharp(imageBuffer) - .extract({ - width: parseInt(width), - height: parseInt(height), - left: parseInt(left), - top: parseInt(top), - }) - .png() - .toBuffer() - } + // if (!checkResult) { + // processedImage = await sharp(imageBuffer).toBuffer() + // } else { + // processedImage = await sharp(imageBuffer) + // .extract({ + // width: parseInt(width), + // height: parseInt(height), + // left: parseInt(left), + // top: parseInt(top), + // }) + // .png() + // .toBuffer() + // } return processedImage } catch (error) { console.error('Error processing image:', error)