Rectangle 27 3

I did a little research and was unable to discover a property that you could enumerate to get the images actual width and height. However there was another approach which seemed interesting which included an xmlHttpRequest to get the image and enumerate it's dimensions.

That made me think of a slightly faster way. The solution is to utilize a hidden <div> as a working container and pulling down the necessary images (using <img>) where you can enumerate width and height there. This pseudoCode For example:

  • Create an image node with the appropriate src path

Just for grins (if it helps) here was a draft of some code for the xmlHttpRequest:

var imgRequest = 
  function(sImgUrl, oRequestCallback){
    var xhr = new XMLHttpRequest();  
    xhr.open('GET', sImgUrl, true);  
    xhr.onreadystatechange = oRequestCallback;
    xhr.send(null);  
    };

var xhrImageRequest =
  function(result){
    var oImageDimensions;
    //Create and append <img> to <div #ImageEnumeration>
    //Get image width and height
    //return image width and height
    };

var myImage = imgRequest("http://example.com/myImage.png", xhrImageRequest);

I wish I could, but the system doesn't allow me... says I have to wait 6 hours.

javascript - how to get the real (unscaled) size of an embedded image ...

javascript css xml dom svg
Rectangle 27 3

I did a little research and was unable to discover a property that you could enumerate to get the images actual width and height. However there was another approach which seemed interesting which included an xmlHttpRequest to get the image and enumerate it's dimensions.

That made me think of a slightly faster way. The solution is to utilize a hidden <div> as a working container and pulling down the necessary images (using <img>) where you can enumerate width and height there. This pseudoCode For example:

  • Create an image node with the appropriate src path

Just for grins (if it helps) here was a draft of some code for the xmlHttpRequest:

var imgRequest = 
  function(sImgUrl, oRequestCallback){
    var xhr = new XMLHttpRequest();  
    xhr.open('GET', sImgUrl, true);  
    xhr.onreadystatechange = oRequestCallback;
    xhr.send(null);  
    };

var xhrImageRequest =
  function(result){
    var oImageDimensions;
    //Create and append <img> to <div #ImageEnumeration>
    //Get image width and height
    //return image width and height
    };

var myImage = imgRequest("http://example.com/myImage.png", xhrImageRequest);

I wish I could, but the system doesn't allow me... says I have to wait 6 hours.

javascript - how to get the real (unscaled) size of an embedded image ...

javascript css xml dom svg
Rectangle 27 18

You can only really do this using a callback of the load event as the size of the image is not known until it has actually finished loading. Something like the code below...

var imgTesting = new Image();

function CreateDelegate(contextObject, delegateMethod)
{
    return function()
    {
        return delegateMethod.apply(contextObject, arguments);
    }
}

function imgTesting_onload()
{
    alert(this.width + " by " + this.height);
}


imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
imgTesting.src = 'yourimage.jpg';

in jquery, you can use $.proxy for this.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 18

You can only really do this using a callback of the load event as the size of the image is not known until it has actually finished loading. Something like the code below...

var imgTesting = new Image();

function CreateDelegate(contextObject, delegateMethod)
{
    return function()
    {
        return delegateMethod.apply(contextObject, arguments);
    }
}

function imgTesting_onload()
{
    alert(this.width + " by " + this.height);
}


imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
imgTesting.src = 'yourimage.jpg';

in jquery, you can use $.proxy for this.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 18

You can only really do this using a callback of the load event as the size of the image is not known until it has actually finished loading. Something like the code below...

var imgTesting = new Image();

function CreateDelegate(contextObject, delegateMethod)
{
    return function()
    {
        return delegateMethod.apply(contextObject, arguments);
    }
}

function imgTesting_onload()
{
    alert(this.width + " by " + this.height);
}


imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
imgTesting.src = 'yourimage.jpg';

in jquery, you can use $.proxy for this.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 18

You can only really do this using a callback of the load event as the size of the image is not known until it has actually finished loading. Something like the code below...

var imgTesting = new Image();

function CreateDelegate(contextObject, delegateMethod)
{
    return function()
    {
        return delegateMethod.apply(contextObject, arguments);
    }
}

function imgTesting_onload()
{
    alert(this.width + " by " + this.height);
}


imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
imgTesting.src = 'yourimage.jpg';

in jquery, you can use $.proxy for this.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 18

You can only really do this using a callback of the load event as the size of the image is not known until it has actually finished loading. Something like the code below...

var imgTesting = new Image();

function CreateDelegate(contextObject, delegateMethod)
{
    return function()
    {
        return delegateMethod.apply(contextObject, arguments);
    }
}

function imgTesting_onload()
{
    alert(this.width + " by " + this.height);
}


imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
imgTesting.src = 'yourimage.jpg';

in jquery, you can use $.proxy for this.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 7

You cannot get file size of remote elements, either give a relative path on your system OR do a file_get_contents() to get contents first . Thus, instead of http:// , do a filesize('/path/to/local/system') . Make sure its readable by php process

yeah, true, i just noticed, thanks. i should have thought of it before hand, im such an idiot.

What about a tip in filesize() docs on PHP.net? It says: "As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality". Seems, you can use filesize() remote elements. You only have to have your server and PHP parser configured properly (security matters).

filesize - php get the KB size of an image - Stack Overflow

php filesize getimagesize
Rectangle 27 631

You can programmatically get the image and check the dimensions using Javascript...

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

This can be useful if the image is not a part of the markup.

IMO : Best answer to this question. Then again, if you have the image loaded in your DOM at the time this is required, then ok.

@blo0p3r - the image doesnt need to be loaded into the DOM before this. It loads the image, and fires the onload to give us the dimensions. By the way - Best answer here!!

I wish you could do this with an XMLHttpRequest Object as well.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 631

You can programmatically get the image and check the dimensions using Javascript...

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

This can be useful if the image is not a part of the markup.

IMO : Best answer to this question. Then again, if you have the image loaded in your DOM at the time this is required, then ok.

@blo0p3r - the image doesnt need to be loaded into the DOM before this. It loads the image, and fires the onload to give us the dimensions. By the way - Best answer here!!

I wish you could do this with an XMLHttpRequest Object as well.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 631

You can programmatically get the image and check the dimensions using Javascript...

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

This can be useful if the image is not a part of the markup.

IMO : Best answer to this question. Then again, if you have the image loaded in your DOM at the time this is required, then ok.

@blo0p3r - the image doesnt need to be loaded into the DOM before this. It loads the image, and fires the onload to give us the dimensions. By the way - Best answer here!!

I wish you could do this with an XMLHttpRequest Object as well.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 631

You can programmatically get the image and check the dimensions using Javascript...

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

This can be useful if the image is not a part of the markup.

IMO : Best answer to this question. Then again, if you have the image loaded in your DOM at the time this is required, then ok.

@blo0p3r - the image doesnt need to be loaded into the DOM before this. It loads the image, and fires the onload to give us the dimensions. By the way - Best answer here!!

I wish you could do this with an XMLHttpRequest Object as well.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 630

You can programmatically get the image and check the dimensions using Javascript...

var img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

This can be useful if the image is not a part of the markup.

IMO : Best answer to this question. Then again, if you have the image loaded in your DOM at the time this is required, then ok.

@blo0p3r - the image doesnt need to be loaded into the DOM before this. It loads the image, and fires the onload to give us the dimensions. By the way - Best answer here!!

I wish you could do this with an XMLHttpRequest Object as well.

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 2

Don't use a null layout!!!

Swing was designed to be used with layout managers. Let each component determine its preferred size and then let the layout manager size and position the component based on the rules of the layout manager.

Instead of doing custom painting you can use Darry'ls Stretch Icon. The Icon will be painted based on the space available to the icon.

i have encountered a problem where the image is to big for the JLabel

Just reread that part of the question. In that case you may want to use the Image.getScaledInstance(...) method to scale the image to an appropriate size. The you can use a regular Icon and the appropriate layout manager.

what layout would i be able to use to easily choose the dimensions and where the objects are positioned

You are not supposed to choose the size/location of a component. That is the job of the layout manager. Read the tutorial on Layout Managers and choose the one (or combination of layout managers) that is appropriate for your requirements.

Chit, before I saw your answer, I closed the question as duplicate with a link to one of your answers. Coincidence? Too strange! 1 +

Ok, what layout would i be able to use to easily choose the dimensions and where the objects are positioned with out being restricted by things like grids from GridBagLayout?

I looked at what layout I'm using and I'm using the absolute layout which allows me to set absolute values to the location and dimension of my components.

@TomT, AbsoluteLayout is not really a layout. Again you should NOT be using absolute values to determine the location and size of components. Each component may have a slightly different LAF on different platforms.

@HovercraftFullOfEels, good coincidence but I may have been a little to quick to answer. The question is not really about dynamically resizing an image, but containing the maximum size of an image. Anyway all these answers give the OP some ideas.

java - How do i get the image from my jlable to auto scale or resize t...

java image swing awt jlabel
Rectangle 27 217

Chromium too (as of now)

@Jourkey: they've been in webkit (and so Safari, Chrome, Epiphany, and most mobile browsers...) for a long time

This is now supported in IE9 and all modern web browsers.

naturalWidth gave me undefined in chrome

worked for me in chrome just now...update?

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 217

Chromium too (as of now)

@Jourkey: they've been in webkit (and so Safari, Chrome, Epiphany, and most mobile browsers...) for a long time

This is now supported in IE9 and all modern web browsers.

naturalWidth gave me undefined in chrome

worked for me in chrome just now...update?

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 217

Chromium too (as of now)

@Jourkey: they've been in webkit (and so Safari, Chrome, Epiphany, and most mobile browsers...) for a long time

This is now supported in IE9 and all modern web browsers.

naturalWidth gave me undefined in chrome

worked for me in chrome just now...update?

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 217

Chromium too (as of now)

@Jourkey: they've been in webkit (and so Safari, Chrome, Epiphany, and most mobile browsers...) for a long time

This is now supported in IE9 and all modern web browsers.

naturalWidth gave me undefined in chrome

worked for me in chrome just now...update?

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 216

Chromium too (as of now)

@Jourkey: they've been in webkit (and so Safari, Chrome, Epiphany, and most mobile browsers...) for a long time

This is now supported in IE9 and all modern web browsers.

naturalWidth gave me undefined in chrome

worked for me in chrome just now...update?

jquery - How to get image size (height & width) using JavaScript? - St...

javascript jquery image jquery-plugins
Rectangle 27 1

Select the element by it's id attribute, to get the image you are referring to.

function getDimension () {
    var img = document.getElementById('myId');
    var width = img.clientWidth;
    var height = img.clientHeight;
    console.log(width);
    console.log(height);
}

window.onload = getDimension();
<img id="myId" class="edit-img about-img" src="http://via.placeholder.com/350x150">

html - Get the size of image in Javascript - Stack Overflow

javascript html