Created
August 18, 2022 20:20
-
-
Save YoannBuzenet/0ad6c2aa5f8f815e91282e2e26be949d to your computer and use it in GitHub Desktop.
Node endpoint to crop image with sharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fastify.post( | |
"/", | |
{ | |
schema: { | |
body: { | |
required: ["name", "credits", "language", "image"], | |
properties: { | |
name: { type: "string" }, | |
credits: { type: "string" }, | |
language: { type: "string" }, | |
x: { type: "string" }, | |
y: { type: "string" }, | |
height: { type: "string" }, | |
width: { type: "string" }, | |
}, | |
}, | |
}, | |
}, | |
async (req, reply) => { | |
console.log(" -------- REQ RECUE -----------"); | |
const data = await req.file(); | |
const buf = await consumers.buffer(data.file); | |
try { | |
// edit and save the file | |
let path = ""; | |
const didCropImage = await cropImage( | |
buf, | |
data.fields.x.value, | |
data.fields.y.value, | |
data.fields.width.value, | |
data.fields.height.value, | |
data.fields.name.value | |
); | |
if (didCropImage) { | |
// save the image | |
// if it worked, save the image record | |
const newImage = { | |
name: data.fields.name.value, | |
credits: "Artiste", | |
// credits: data.fields.credits.value, | |
// language: data.fields.language.value, | |
language: "FR", | |
path, | |
}; | |
const savedImage = await db.Image.create(newImage); | |
reply.code(200).send(savedImage); | |
} else { | |
reply.code(500).send("Image could not be created."); | |
} | |
return; | |
} catch (e) { | |
logger.log("error", "Error while creating a Image :" + e); | |
reply.code(500).send("Error when creating a image"); | |
} | |
return; | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sharp = require("sharp"); | |
const { FOLDER_IMAGE } = require("../config/consts"); | |
const cropImage = async (image, x, y, width, height, imageName) => { | |
const xNumber = parseInt(x, 10); | |
const yNumber = parseInt(y, 10); | |
const widthNumber = parseInt(width, 10); | |
const heightNumber = parseInt(height, 10); | |
sharp(image) | |
.resize(680, null) | |
.extract({ | |
left: xNumber, | |
top: yNumber, | |
width: widthNumber, | |
height: heightNumber, | |
}) | |
.toFile(`${FOLDER_IMAGE}/${imageName}.png`, function (err) { | |
// Extract a region of the input image, saving in the same format. | |
if (err) { | |
console.log("--- error while registering image", err); | |
} else { | |
console.log(" +++ it worked"); | |
} | |
}); | |
return true; | |
}; | |
module.exports = { | |
cropImage, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment