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.

Possibility for variable aspect ratio locking (i.e. min + max aspect ratio)?

See original GitHub issue

Hi there, thanks so much for this library! Just threw it into a project and got up and running nice and easily (just had to tweak some CSS things to ensure vertical images don’t overflow the cropping area).

I have a use case I’d like to implement somehow. For example, I want to allow people to add poster images to my app - in this case, a poster can be of multiple sizes, as some countries / cinemas choose to use a slightly different aspect ratio for their posters. It’s not major differences, but the differences do exist. I’d like to allow cropping between the minimum aspect ratio and the maximum aspect ratio within those sizes.

My aspect ratios might look like this:

  {
    minAspect: 0.66,
    maxAspect: 0.75,
  }

I think this is not currently possible with react-image-crop. Is this something you think could be implemented? I’m happy to try take a look at it myself as well and make a PR.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
samstandingcommented, Oct 1, 2020

I was able to get this working by changing my onChange to this:

const onChange = (c, percentCrop) => {
   const newCrop = crop.unit === '%' ? percentCrop : c;
    if (!maxAspect || !minAspect) setCrop(newCrop);
    else if (c.width / c.height > maxAspect) {
      setCrop({ ...newCrop, height: newCrop.width / maxAspect });
    } else if (newCrop.width / newCrop.height < minAspect) {
      setCrop({ ...newCrop, height: newCrop.width / minAspect });
    } else setCrop(newCrop);
  };
1reaction
DominicTobiascommented, Oct 5, 2020

I was able to get this working by changing my onChange to this:

const onChange = (c, percentCrop) => {
   const newCrop = crop.unit === '%' ? percentCrop : c;
    if (!maxAspect || !minAspect) setCrop(newCrop);
    else if (c.width / c.height > maxAspect) {
      setCrop({ ...newCrop, height: newCrop.width / maxAspect });
    } else if (newCrop.width / newCrop.height < minAspect) {
      setCrop({ ...newCrop, height: newCrop.width / minAspect });
    } else setCrop(newCrop);
  };

Cool maybe I’ll incorporate this logic

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lock Aspect Ratio - Medium
“Lock Aspect Ratio” is the possibility to preserve the relative width and height when you resize an image or graphics shape.
Read more >
aspect-ratio - CSS-Tricks
The CSS property aspect-ratio lets you create boxes that maintain proportional dimensions where the height and width of a box are calculated ...
Read more >
device-aspect-ratio - CSS: Cascading Style Sheets | MDN
The device-aspect-ratio CSS media feature can be used to test the width-to-height aspect ratio of an output device.
Read more >
Ligand Aspect Ratio as a Decisive Factor for the Self ...
It is possible to control the geometry and the composition of metallasupramolecular assemblies via the aspect ratio of their ligands.
Read more >
Apply width and height attributes to allow responsive image ...
So the browsers were not able to calculate the aspect ratio of images and ... 2350w" type="image/jpeg" sizes="(min-width: 1290px) 1290px, ...
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