Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can we get new image size dimension after giving object-fit:contain property to the image tag?

enter image description here

In the above image, you can see the original image size and image get shrink after giving the property "object-fit:contain".

can we calculate the effective image size after giving "Object-fit:contain" property.

like image 278
pratik solanki Avatar asked Mar 05 '23 12:03

pratik solanki


1 Answers

The image object after loading contains both the displayed dimensions of the container and the dimensions of the original image. So the calculations are fairly simple:

function getContainedSize(img) {
  var ratio = img.naturalWidth/img.naturalHeight
  var width = img.height*ratio
  var height = img.height
  if (width > img.width) {
    width = img.width
    height = img.width/ratio
  }
  return [width, height]
}

[ http://jsfiddle.net/wvbpcjhk/ ]

like image 107
stdob-- Avatar answered Apr 06 '23 22:04

stdob--