test
This commit is contained in:
yjnoh 2025-06-05 17:17:40 +09:00
parent 5c1c50bd46
commit 63f12021e4
2 changed files with 33 additions and 18 deletions

View File

@ -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",

View File

@ -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)