If you don’t want to completely resize the actual image file and want to keep the image proportions, you can poll the image by its size (height and width), and then multiply them by the required ratio to fit into your div.
For example, if you have an image of 1024x768 and you want to place it in a div with a width of 800, you know that the width will be 800 and the height will be 768 x (800/1024) = 600. Then, when displaying your image, you can set the necessary height and width options.
source share