프로그래밍
Javascript로 Image Size(Width & height) 알아내기
pgmaru
2015. 8. 22. 13:32
반응형
Javascript로 Image Size(Width & height) 알아내기
다음과 같이 하면, JavaScript로 이미지 크기를 알아낼 수 있다.
<script language="javascript">
var img = new Image();
img.onload = function() {
var w = this.width;
var h = this.height;
alert("width : " + w + ", height : " + h);
}
img.src = '[이미지 경로]';
</script>
예를 들어, 다음과 같이 하면 티스토리 로고의 가로, 세로를 알아낼 수 있다.
<script language="javascript">
var img = new Image();
img.onload = function() {
var w = this.width;
var h = this.height;
alert("width : " + w + ", height : " + h);
}
function setImgSrc() {
img.src = 'http://i1.daumcdn.net/cfs.tistory/static/top/tistory_logo.gif';
}
</script>
다음 이미지를 클릭해 보세요.
<img src="http://i1.daumcdn.net/cfs.tistory/static/top/tistory_logo.gif"
onclick="javascript:setImgSrc();" />

반응형