Skip to content

Instantly share code, notes, and snippets.

@aljachimiak
Created August 7, 2017 17:50
Show Gist options
  • Save aljachimiak/dd88ba96525a730ac65205572a3f9129 to your computer and use it in GitHub Desktop.
Save aljachimiak/dd88ba96525a730ac65205572a3f9129 to your computer and use it in GitHub Desktop.
When cropping an image that is too small, you may need to scale up one or both of the dimensions first.
class Image {
height: return this.height;
width: return this.width;
}
function Crop (image, newHeight, newWidth) {
const heightTooSmall = image.height < newHeight;
const widthTooSmall = image.width < newWidth;
// only do this if one or both of the dimensions is too small
if (heightTooSmall || widthTooSmall) {
let scaleWidth = 0;
let scaleHeight = 0;
let scaler = 0;
if (heightTooSmall) {
scaler = newHeight / image.height;
scaleHeight = newHeight;
scaleWidth = image.width * scaler;
} else {
scaler = newWidth / image.width;
scaleWidth = newWidth;
scaleHeight = image.height * scaler;
}
const correctSizedImage = ScaleUp(image, scaleHeight, scaleWidth);
// recurses in the case that both dimensions were too small
return Crop(correctSizedImage, newHeight, newWidth);
}
// this is what is happening to every image right now
return imigxCrop(image, newHeight, newWidth);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment