Страница 1 из 1

[Jquery,JS] как определить размер (в кб) картинки?

Добавлено: 2010-08-01 4:32:49
ProFTP
как определить размер (в кб) картикни/изображения??

размер ширины и длинны работает:

Код: Выделить всё

 
		var newImg = new Image();
		var height, width;

		newImg.onload = function() {
    	height = newImg.height;
    	width = newImg.width;

		};

		newImg.src = 'file://localhost/usr/home2/di/11/p/29/1.jpg'; 

   	var height = newImg.height;
    	var width = newImg.width;

    	var size = newImg.size;
  	

					
    alert ('The image size is '+size+'*'+height);					
					
		  $(".info").html( " "+width+" x "+height+", "+size+" Кбайт "); 	

а как определить именно размер картинки в кб?

вроде бы CSS ДОМ сохраняет это...?

очень желательно определить размер на Javascript...

Re: [Jquery,JS] как определить размер (в кб) картинки?

Добавлено: 2010-08-03 23:09:50
ProFTP
замутил:

Код: Выделить всё

		var newImg = new Image();
		var height, width;

		newImg.onload = function() {
    	height = newImg.height;
    	width = newImg.width;

		};

	newImg.src = 'http://x0.org.ua/test/p/29/1.jpg'; 


function getImageSizeInBytes(imgURL) {
    var request = new XMLHttpRequest();
    request.open("HEAD", imgURL, false);
    request.send(null);
    var headerText = request.getAllResponseHeaders();
    var re = /Content\-Length\s*:\s*(\d+)/i;
    re.exec(headerText);
    return parseInt(RegExp.$1);
}


   	var height = newImg.height;
    	var width = newImg.width;		
    	

   var size_image = getImageSizeInBytes('http://x0.org.ua/test/p/29/1.jpg');

 size_image = size_image / 1000; 


//	alert(getImageSizeInBytes('http://x0.org.ua/test/p/29/1.jpg'));				
					
//    alert ('The image size is '+size+'*'+height);					
					


		  $(".info").html( " "+width+" x "+height+", "+size_image+"  
но оно плохо работает в раных браузерах
http://x0.org.ua/gallery_test/index.html

opera9:

Код: Выделить всё

462 x 600, NaN Кбайт
firefox3.0

Код: Выделить всё

462 x 600, 126.792 Кбайт
etc