How to use a fucntion to resize an image (found in a tutorial) for display.loadRemoteImage?

Hello, I have a question for resizing an image.The tutorial to fit images to a size is great to work on display.newImage(), but I cannot manage to do it for display.loadRemoteImage(). I think it because display.loadRemoteImage() does not return DisplayObject which is required for this resize function.

How can I resize a remote image like below?

In my case, I am using an image URL which is a result of API call (network.request). I store it as “image.png”. So far, I can display loadedimage, but I would like to resize it with the parameters I see in the tutorial (ie icon.x = 50 icon.y = 300, fitImage( icon, 200, 200, true)) Thank you very much for your help in advance!

    local json = require( "json" )     local function networkListener( event )         local res = json.prettify( event.response )         local decoded = json.decode( res )         if ( event.isError ) then             print( "--Network error-- ", ( res ) )         else             print( "Results: " .. ( res ) )             --Here a JSON value (URL) is fetched from an API call and use it as a remote image             local edmPreview = decoded.items[lot20].edmPreview[1]             -- Display image from web             local function networkListener1image( event )                 if ( event.isError ) then                     print ( "Network error - download failed" )                 else                     event.target.alpha = 0                     transition.to( event.target, { alpha = 1.0 } )                 end                 print ( "event.response.fullPath: ", event.response.fullPath )                 print ( "event.response.filename: ", event.response.filename )                 print ( "event.response.baseDirectory: ", event.response.baseDirectory )             end             local loadedimage = display.loadRemoteImage(edmPreview, "GET", networkListener1image, "image1.png", system.TemporaryDirectory, 200, display.contentCenterY )         end     end     network.request("https://www.europeana.eu/api/v2/search.json?wskey="..apikey.europeana.."&query=art&qf=when:1900&start="..lotmax.."&reusability=open&media=true", "GET", networkListener, params)

 

Inside the networkListener function there is a table passed in called “event”, on this table is a variable called “target” - this is your display object.

Thank you so much @GrahamRanson! It is quite straightforward to find a solution. I did not know Lua enough.

So, the solution was:

    --including a module local resize = require( "module\_imageresize" ) local json = require( "json" )     local function networkListener( event )         local res = json.prettify( event.response )         local decoded = json.decode( res )         if ( event.isError ) then             print( "--Network error-- ", ( res ) )         else             print( "Results: " .. ( res ) )             --Here a JSON value (URL) is fetched from an API call and use it as a remote image             local edmPreview = decoded.items[lot20].edmPreview[1]             -- Display image from web             local function networkListener1image( event )                 if ( event.isError ) then                     print ( "Network error - download failed" )                 else                     event.target.alpha = 0                     transition.to( event.target, { alpha = 1.0 } )                 end                 print ( "event.response.fullPath: ", event.response.fullPath )                 print ( "event.response.filename: ", event.response.filename )                 print ( "event.response.baseDirectory: ", event.response.baseDirectory )                 myImage = event.target                 resize.fitImage(myImage, 200, 200, true )             end             local loadedimage = display.loadRemoteImage(edmPreview, "GET", networkListener1image, "image1.png", system.TemporaryDirectory, 200, display.contentCenterY )         end     end     network.request("https://www.europeana.eu/api/v2/search.json?wskey="..apikey.europeana.."&query=art&qf=when:1900&start="..lotmax.."&reusability=open&media=true", "GET", networkListener, params)

I am calling a Module (module_imageresize.lua) which is basically the function explained in the tutorial.

local M = {} M.fitImage = function( displayObject, fitWidth, fitHeight, enlarge )     -- first determine which edge is out of bounds     local scaleFactor = fitHeight / displayObject.height     local newWidth = displayObject.width \* scaleFactor     if newWidth \> fitWidth then         scaleFactor = fitWidth / displayObject.width     end     if not enlarge and scaleFactor \> 1 then         return     end     displayObject:scale( scaleFactor, scaleFactor ) end return M

 

Glad I could help!

Inside the networkListener function there is a table passed in called “event”, on this table is a variable called “target” - this is your display object.

Thank you so much @GrahamRanson! It is quite straightforward to find a solution. I did not know Lua enough.

So, the solution was:

    --including a module local resize = require( "module\_imageresize" ) local json = require( "json" )     local function networkListener( event )         local res = json.prettify( event.response )         local decoded = json.decode( res )         if ( event.isError ) then             print( "--Network error-- ", ( res ) )         else             print( "Results: " .. ( res ) )             --Here a JSON value (URL) is fetched from an API call and use it as a remote image             local edmPreview = decoded.items[lot20].edmPreview[1]             -- Display image from web             local function networkListener1image( event )                 if ( event.isError ) then                     print ( "Network error - download failed" )                 else                     event.target.alpha = 0                     transition.to( event.target, { alpha = 1.0 } )                 end                 print ( "event.response.fullPath: ", event.response.fullPath )                 print ( "event.response.filename: ", event.response.filename )                 print ( "event.response.baseDirectory: ", event.response.baseDirectory )                 myImage = event.target                 resize.fitImage(myImage, 200, 200, true )             end             local loadedimage = display.loadRemoteImage(edmPreview, "GET", networkListener1image, "image1.png", system.TemporaryDirectory, 200, display.contentCenterY )         end     end     network.request("https://www.europeana.eu/api/v2/search.json?wskey="..apikey.europeana.."&query=art&qf=when:1900&start="..lotmax.."&reusability=open&media=true", "GET", networkListener, params)

I am calling a Module (module_imageresize.lua) which is basically the function explained in the tutorial.

local M = {} M.fitImage = function( displayObject, fitWidth, fitHeight, enlarge )     -- first determine which edge is out of bounds     local scaleFactor = fitHeight / displayObject.height     local newWidth = displayObject.width \* scaleFactor     if newWidth \> fitWidth then         scaleFactor = fitWidth / displayObject.width     end     if not enlarge and scaleFactor \> 1 then         return     end     displayObject:scale( scaleFactor, scaleFactor ) end return M

 

Glad I could help!