Page 1 of 1

Load a sprite from URL

Posted: Sat Dec 10, 2022 1:23 am
by itzybitzyspider
Hello,

Is there a way to load a sprite image from a URL?

So trying to load dynamically from a list of URLs I get from an API call.

Thanks

Re: Load a sprite from URL

Posted: Sat Dec 10, 2022 1:48 am
by Paul
You will not be able to load an image from another website.
Your Browser will block it for security reasons.

Images must exist on the same server/location as your app.

Maybe read this thread for better explanation...
viewtopic.php?t=2265

Re: Load a sprite from URL

Posted: Sun Dec 11, 2022 2:01 am
by itzybitzyspider
Hello, so I'm looking at getting from HTTP the image itself. Since I got the response, can I pass it / encode / decode it into a format which I can LoadSprite into? Or worse case I guess I can put a script at the source URL (server) to convert the image into base64 and then I can read it into LoadSprite. However, I'd like to have it all in Spider and not have to maintain another script somewhere. There was a Base64Encoder function in PB but not in SB...

Code: Select all

Procedure HttpGetEvent(Success, Result$, UserData)
    If Success
      Debug Result$
    Else
      Debug "HTTPRequest(): Error"
    EndIf
  EndProcedure
  
  ; Get the content of this file, and display it in the debug window
  ;
  HTTPRequest(#PB_HTTP_Get, "https://xxx.ngrok.io/static/test.jpg", "", @HttpGetEvent())

Re: Load a sprite from URL

Posted: Sun Dec 11, 2022 2:33 am
by useful

Re: Load a sprite from URL

Posted: Sun Dec 11, 2022 4:14 am
by itzybitzyspider
I'm posting this so next time we're all good.

If you have control over your http server (in this case I have), with an Apache for example. You add it in your config or the .htaccess file. Look at the security risks before doing this. I don't have an issue because it's public images anyways.

Code: Select all

<VirtualHost *:443>
   ...
   Header add Access-Control-Allow-Origin "*"
   ...
</VirtualHost>
This will allow your app to just do this directly.

Code: Select all

LoadSprite(0,  "https://xxx.xxx-xx.com/xx/xx-xxx.jpg")
Or you have to have an in-between proxy of some sort which will allow * origin and then get the file for you. Squid proxy or Nginx would do.