Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a non-square image to square thumbnail (by adding white space)?

Tags:

java

image

resize

I'd like to create a square thumbnail of an image using Java. I've already managed to resize images through a couple of ways. However I'd like to create a real square image, also from a non-square image.

Example: the source has a size of 200x400 (widht/height) the target size is 100x100 The algorithm would then need to resize the image to 50x100 and add 25x100 pixels of whitespace each on the left and on the right.

Can anyone help me with this?

like image 879
user1861258 Avatar asked Feb 01 '26 09:02

user1861258


1 Answers

Just create a 100x100 background; add the scaled image to it. Use Math.max(width, height) to determine the scale factor. Then, plot the scaled image over the background, use calculations (offset x, offset y) to put it in the proper position.

like image 62
Rob Audenaerde Avatar answered Feb 02 '26 22:02

Rob Audenaerde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!