Get width and height of a remote image using network.download()

How to get the original width and height of an image that’s loaded remotely when using network.download()

Hey @bgmadclown, I got Attempt to index field ‘target’ (a nil value) Here’s the code:

local function networkListener(event)
      if (event.isError) then
        print ("Network error - download failed");
      elseif (event.phase == "ended") then
        print (event.target, event.target.width, event.target.height);
      end
    end

local requestId = network.download(
      url,
      "GET",
      networkListener,
      {progress = true},
      "temp.png",
      system.TemporaryDirectory
    );

Is that " intended to be near temp.png?

Sorry, that was just a typo while copying the code. I edited it to its current format.

Oh sorry, I missed the network.download() part. My bad! I don’t know how to get the width and height through network.download() but you may want to try display.loadRemoteImage() with destFilename. Pretty much works the same.

Yeah I was earlier loading the image using display.loadRemoteImage() but I needed an option to cancel the loading if the scene has changed so I’m currently using network.download().

In this topic you said what it worked

Yes yes, there was a bug which I figured out yesterday. (Just updated the topic) So as an alternative using network.download()

Have you tried using network.request() to download the image and network.cancel() when you want to stop the download? Does it work?

(For the other topic, again, your issue could be solved by calling network.cancel())

Yeah I could try network.request(). I guess here I’ll have the option to do network.cancel() and also get the event.target.
Also, just confirming there’s no option to cancel a request while using display.loadRemoteImage() right?

Well, implementation is shared on documentation but I don’t know how easy it would be to modify it. You may need to take a look. Here: https://docs.coronalabs.com/api/library/display/loadRemoteImage.html#cancel-remote-load-before-completion

1 Like

Oh yes, I did check this out. Seemed quite complicated to get it done compared to other options.

By using network.request() we do not get the original width and height of the image.

@diongeorge

You can’t get the width and height of a remote image. You have to create it locally or use an advanced technique to inspect the file itself (after downloading it).

As far as the latter, I know some community members worked out how to do this in some cases, but it was never something that got used normally.

Your best bet is to download the image, then create a display object using it with display.newImage(), grab the width and height of the new image, then delete it.

Tip: SSK has a helper function to do this:

misc.getImageSize = function ( path, basePath )
	basePath = basePath or system.ResourceDirectory	
	local tmp = display.newImage( path, basePath, 10000,10000 )
	if( not tmp ) then
		return 0, 0
	end
	local sx = tmp.contentWidth
	local sy = tmp.contentHeight
	display.remove(tmp)
	return sx,sy
end
1 Like