Issue 67413 in chromium: get image size in img onload function

200 views
Skip to first unread message

chro...@googlecode.com

unread,
Dec 18, 2010, 1:55:18 AM12/18/10
to chromi...@chromium.org
Status: Unconfirmed
Owner: ----
Labels: Type-Bug Pri-2 Area-Undefined

New issue 67413 by 2chin.yip: get image size in img onload function
http://code.google.com/p/chromium/issues/detail?id=67413

Chrome Version : 10.0.603.3
URLs (if applicable) :
Other browsers tested:
Firefox 3.x: OK
IE 7/8: OK
Chrome earlier than 10.0.x: OK

What steps will reproduce the problem?
1. attach an onload event to an img element<img src="xxx"
onload="dosth(this)"/>
2. assign the src of img element to an Image object, and get the width
var img = new Image();
img.src = this.src;
alert(img.width);

What is the expected result?
Can get the image width because the image should be already loaded while
the onload event is triggered, assign the src to an object should load the
image from browser cache.

What happens instead?
img.width equals to 0, the image will be downloaded again.

Please provide any additional information below. Attach a screenshot if
possible.


chro...@googlecode.com

unread,
Feb 18, 2011, 11:10:52 AM2/18/11
to chromi...@chromium.org

Comment #1 on issue 67413 by Mac...@gmail.com: get image size in img onload
function
http://code.google.com/p/chromium/issues/detail?id=67413

My case is related to this issue I think:
Essentially javascript detect a wrong width of an image loaded from cache.
1) if you doesn't have the image in cache it gets the right width (275)
2) if you reload the page and the image is in the cache it gets 0.

here attached my test case.

WORKAROUND:
if you add at the testcase this line :
img.setAttribute( "src" , "");
before :
img.setAttribute( "onload", "alert(this.width)");
it gets the correct width.


Attachments:
imageload.html 376 bytes

chro...@googlecode.com

unread,
May 10, 2011, 7:04:57 AM5/10/11
to chromi...@chromium.org

Comment #2 on issue 67413 by philippe...@gmail.com: get image size in img
onload function
http://code.google.com/p/chromium/issues/detail?id=67413

Chrome 11.0.696.65, MacOSX 10.6.7 : bug confirmed.
MacOsx build only. Work fine in Windows version.

chro...@googlecode.com

unread,
Oct 13, 2011, 7:35:49 AM10/13/11
to chromi...@chromium.org

Comment #3 on issue 67413 by spack...@gmail.com: get image size in img
onload function
http://code.google.com/p/chromium/issues/detail?id=67413

Can reproduce the same exact behaviour in Chrome 14.0.835.186 m on a XP
machine. Workaround does not work for me:

jQuery('<img class="lightbox_content" alt="" />')
.appendTo(jQuery('#lightbox'))
.attr('src', 'image.jpg')
.load(function(){
console.log(jQuery(this).width());
});

logs 0 instead of the image's width. Works fine in all other browsers and
older Chome versions.

chro...@googlecode.com

unread,
Oct 13, 2011, 9:00:07 AM10/13/11
to chromi...@chromium.org

Comment #4 on issue 67413 by Mac...@gmail.com: get image size in img onload
function
http://code.google.com/p/chromium/issues/detail?id=67413

so now the problem is worse than before... great :(

chro...@googlecode.com

unread,
Feb 24, 2012, 4:10:03 AM2/24/12
to chromi...@chromium.org
Updates:
Status: WontFix

Comment #5 on issue 67413 by no...@chromium.org: get image size in img
onload function
http://code.google.com/p/chromium/issues/detail?id=67413

Images are not loaded until the img.onload event fires. Slow servers
and/or large images may cause image
loads to be variously delayed cross browser, whether the image comes from a
cache or the network. The load event should be used if subsequent
processing of an image is needed, such as reading it's length.

var img = new Image();

img.onload = function() { console.log(this.width) }
img.onerror = function() { console.log('image load failed') }
img.src = .. some-image-url-here ..

You should always assign the onload handler _before_ setting the image .src


chro...@googlecode.com

unread,
Feb 25, 2012, 6:54:50 PM2/25/12
to chromi...@chromium.org

Comment #7 on issue 67413 by fi...@mxd.cz: get image size in img onload
function
http://code.google.com/p/chromium/issues/detail?id=67413

re #6:

works for me -> shows 275 all the time (v17.0.963.56)

chro...@googlecode.com

unread,
Feb 26, 2012, 9:30:47 PM2/26/12
to chromi...@chromium.org

Comment #8 on issue 67413 by no...@chromium.org: get image size in img
onload function
http://code.google.com/p/chromium/issues/detail?id=67413

#6, easier still per
http://www.w3.org/TR/html5/embedded-content-1.html#the-img-element

"The IDL attributes width and height must return the rendered width and
height of the image, in CSS pixels, if the image is being rendered, and is
being rendered to a visual medium; or else the intrinsic width and height
of the image, in CSS pixels, if the image is available but not being
rendered to a visual medium; or else 0, if the image is not available."

#7 agree, with or without an empty cache.


chro...@googlecode.com

unread,
Feb 27, 2012, 7:10:11 AM2/27/12
to chromi...@chromium.org

Comment #9 on issue 67413 by spack...@gmail.com: get image size in img
onload function
http://code.google.com/p/chromium/issues/detail?id=67413

I can affirm #7 and #8, seems to be fixed in Chrome 17.0.963.56 m (I tried
on a very slow XP machine and a very fast Win 7 machine). Tested my
lightbox-implementation (works again) and the test case from #6 (alerts
275).

But why is the the status set to WontFix? Obviously there was anything
fixed between Chrome 14 and 17.

chro...@googlecode.com

unread,
Feb 27, 2012, 7:22:14 AM2/27/12
to chromi...@chromium.org

Comment #10 on issue 67413 by Mac...@gmail.com: get image size in img
onload function
http://code.google.com/p/chromium/issues/detail?id=67413

maybe it got fixed resolving another bug, on a chromium 12.0.742.122 still
get the error.

Reply all
Reply to author
Forward
0 new messages