Home Web Front-end JS Tutorial How can I determine the file size and dimensions of an image using JavaScript?

How can I determine the file size and dimensions of an image using JavaScript?

Nov 23, 2024 am 05:23 AM

How can I determine the file size and dimensions of an image using JavaScript?

Determining Image File Size and Dimensions with Javascript

In web applications, it becomes necessary to retrieve information about images displayed on web pages. One common requirement is to determine an image's file size and resolution entirely within the browser context. This article explores various cross-browser techniques to accomplish this task.

Getting Image Dimensions

To determine the resolution of an image displayed on the web page, you can utilize the clientWidth and clientHeight properties of the DOM element containing the image. This approach returns the pixel dimensions of the image within the browser, excluding any borders or margins.

var img = document.getElementById('imageId');

var width = img.clientWidth;
var height = img.clientHeight;
Copy after login

Retrieving File Size

To obtain the file size of an image, one option is to use the fileSize property available in Internet Explorer for document and IMG elements. However, this approach is limited to IE and not supported in other browsers.

Using HEAD HTTP Requests

A more versatile technique is to issue an HTTP HEAD request to the image's URL. HEAD requests retrieve metadata about a resource without downloading its contents. The response headers include the Content-Length, representing the file size in bytes.

var xhr = new XMLHttpRequest();
xhr.open('HEAD', 'img/test.jpg', true);
xhr.onreadystatechange = function(){
  if (xhr.readyState == 4) {
    if (xhr.status == 200) {
      alert('Size in bytes: ' + xhr.getResponseHeader('Content-Length'));
    } else {
      alert('ERROR');
    }
  }
};
xhr.send(null);
Copy after login

Note: Ajax requests are subject to the same-origin policy, meaning they can only access resources from the same domain as the web page.

Retrieving Original Image Dimensions

To determine the original dimensions of an image before resizing or cropping, you can create an IMG element programmatically and set its onload event to retrieve its dimensions.

var img = document.createElement('img');

img.onload = function() { alert(img.width + ' x ' + img.height); };

img.src='http://sstatic.net/so/img/logo.png';
Copy after login

The above is the detailed content of How can I determine the file size and dimensions of an image using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

10 Mobile Cheat Sheets for Mobile Development

See all articles