>  기사  >  웹 프론트엔드  >  자바스크립트를 사용하여 이미지 크기와 이미지 비례 배율을 얻는 방법

자바스크립트를 사용하여 이미지 크기와 이미지 비례 배율을 얻는 방법

高洛峰
高洛峰원래의
2016-12-05 14:00:221305검색

이 기사의 예에서는 자바스크립트를 사용하여 이미지 크기를 얻고 비례적으로 이미지 크기를 조정하는 방법을 설명합니다. 참고용으로 모든 사람과 공유하세요.

이미지 크기 가져오기:

var originImage = new Image();
function GetImageWidth(oImage) {
  if (originImage.src != oImage.src) originImage.src = oImage.src;
  return originImage.width;
}
function GetImageHeight(oImage) {
  if (originImage.src != oImage.src) originImage.src = oImage.src;
  return originImage.height;
}

🎜>

function SetImage(ImgD, FitWidth, FitHeight) {
  var image = new Image();
  image.src = ImgD.src;
  if (image.width > 0 && image.height > 0) {
    if (image.width / image.height >= FitWidth / FitHeight) {
      if (image.width > FitWidth) {
        ImgD.width = FitWidth;
        ImgD.height = (image.height * FitWidth) / image.width;
      } else {
        ImgD.width = image.width;
        ImgD.height = image.height;
      }
    } else {
      if (image.height > FitHeight) {
        ImgD.height = FitHeight;
        ImgD.width = (image.width * FitHeight) / image.height;
      } else {
        ImgD.width = image.width;
        ImgD.height = image.height;
      }
    }
  }
}

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.