Creating thumbnail images with automatic cropping and maintaining aspect ratio
Most thumbnail-generation solutions will shrink the original down while maintaining aspect ratio.
Usually you specify the maximum height and width of the thumbnail, e.g. 150 x 200
However, if your original image’s aspect ratio is different to the maximum thumbnail dimensions, you will end up with dead space vertically or horizontally (shown in green in the illustration). This can be quite an eyesore when displaying thumbnails in a grid.
I’ve got an algorithm that will automatically crop the image either horizontally or vertically to then match the thumbnail aspect ratio, so you end up with the thumbnails all being the same size even though they may be coming from originals of wildly different aspect ratio.
In the illustration, you can see that the image is scaled down and fills all the available thumbnail space, showing the parts in grey from the original that were cropped out.
The algorithm does the cropping before the resizing. It takes the width and height of the original image, and the width and height of the desired thumbnail. It will return the width and height that the original must be cropped to, to match the aspect ratio of the thumbnail.
Of course this isn’t quite enough; where do we do the cropping? All from the top of the image, or the bottom, or a mix? Ideally we want the user to choose which parts of the original they want cropped out. But we can do some very simple automatic cropping by cropping equal amounts from each side that needs to be cropped.
Now we know exactly which part of the original we can take and then resize down to a thumbnail.