Created
August 7, 2017 17:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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