Guest User

Crop image by percentage in sharp

a guest
Nov 7th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. router.use('/', (req, res) => {
  2.   const parameters = {
  3.     left: 0.5, //procent wycięcia od góry
  4.     top: 0.2, //procent wycięcia z lewej
  5.     size: 0.5 //szerokość i wysokość zdjęcia brane z szerokości oryginalnego ()
  6.   }
  7.   const fileStream = this.s3.getObject({
  8.     Bucket: '<my-bucket-name>',
  9.     Key: '<photo-key>'
  10.   }).createReadStream();
  11.  
  12.   let imageInfo;
  13.  
  14.   const transformer = sharp().on('info', info => {
  15.     imageInfo = info;
  16.   })
  17.  
  18.   const cropTransformer = sharp().extract({
  19.     left: parameters.left * imageInfo.width,
  20.     top: parameters.top * imageInfo.height,
  21.     width: parameters.size * imageInfo.width,
  22.     height: parameters.size * imageInfo.width
  23.   });
  24.  
  25.   return fileStream.pipe(transformer).pipe(cropTransformer).pipe(res);
  26. })
Add Comment
Please, Sign In to add comment