blueimp/JavaScript-Load-Image

aspectRatio depending on image orientation

DJWassink opened this issue · 3 comments

I was wondering if its possible to crop an image by a certain aspectRatio depending on the fact if an image is made in portrait or in landscape mode.

For example in landscape we might want 16/9 but in portrait maybe 2/3.

Currently this is possible by first loading the image once and checking the width/height and depending on that set the aspectRatio in a second loadImage. This however feels a bit odd and since the data is already know while applying the aspectRatio it could be nice to implement this in the library itself.

I did fork this repo and added my own (possible dirty) option to achieve what I need:

https://github.com/DJWassink/JavaScript-Load-Image/blob/master/js/load-image-scale.js#L77-L93

Hi @DJWassink!

You don't have to modify the library or call loadImage twice to achieve this effect, as the scaling API already covers your use case:

loadImage(file).then(function (data) {
  if (data.originalWidth > data.originalHeight) {
    // landscape mode
    var scaledImage = loadImage.scale(data.image, { maxWidth: 600 })
    // ...
  } else {
    // portrait mode
    // ...
  }
})

Wow that's actually pretty smart!
Thanks a ton!

I will see myself out now.