question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to crop with coordinates provided from the front-end?

See original GitHub issue

I am using a cropping tool on the front-end with react.js called react-avatar-editor. It can crop the image on the front-end, but long story short, I will be cropping and manipulating very large images so I need to do it on the server. The package provides certain coordinates and parameters below which I can use on the server.

Is it easy to achieve accurate cropping with sharp package when I have a list of coordinates that I am provided below?

The ‘x’ and ‘y’ coordinates tell me at which point to start cropping from bottom left corner and ‘height’ and ‘width’ parameters tell me how far the top right corner is where to end the cropping.

Any help would be greatly appreciated!

{
   height: 1,
   width: 0.5979381443298969,
   x: 0.24318114874815838,
   y: 0
}

To give more context on numbers above, let’s say we have a 400px (h) x 600px (w) image. ‘x’ and ‘y’ tell me to start cropping at bottom left proportionally so around 125px (24%) to the right and 0px (0%) from the bottom. the height tells me to crop until to the very top of the image 400px (1 in height) and 0.59 (or 59%) to the right which around 300px.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
lovellcommented, Apr 25, 2017

Hello, perhaps try something like the following (untested):

const image = sharp(input);
image.metadata()
  .then(metadata => {
    // perform your calculations based on metadata.width and metadata.height
    const left = ?, top = ?, width = ?, height = ?;
    return image
      .extract({ left, top, width, height })
      .toBuffer();
  })
  .then(data => {
    // data is a Buffer containing your extracted image
  });
2reactions
markgohocommented, Aug 10, 2018

@simeyla in 2015 a new version of Javascript came out that allows you to create objects with that syntax. If the property name and variable being set as the value have the same name you can write it without duplicating the value each time.

{ 
  top: top, 
  left: left 
}

is exactly equivalent to

{
  top,
  left
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get Coordinate of Cropped Image by CropperJS?
I am using cropper JS to crop my image ...
Read more >
5 Ways to Crop Images in HTML/CSS - Cloudinary
You can crop images using plain HTML5 and CSS code, without using JavaScript or any other scripting language. We'll show several techniques for...
Read more >
How to crop an image in CSS - Educative.io
Using object-fit. The object-fit CSS property can be used to easily crop images. It can have five values, but cover is the most...
Read more >
Crop image based on coordinates from a "-trim +repage" of a ...
One way of doing this task would be to have a script that takes an input image as the argument: 1. Copy the...
Read more >
Service to crop images using coordinates - Transloadit
Crop an image based on cropping coordinates · 1. Handle uploads · 2. Crop images to 90×60 starting at 30×30 from the top...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found