Author Topic: Random generated pictures from string using Robohash API!  (Read 3091 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Random generated pictures from string using Robohash API!
« on: July 28, 2020, 09:35:50 am »
This API hashes a string that you send it and will display an image generated based on that hash. This could be useful for people developing RPG style games where the character's picture can be generated based on the name or for a profile picture for a website where you don't want your actual face. See below for a screenshot of an image using "qb64.org" as the string:
 
robohashapi.png

And here is the source code:
Code: QB64: [Select]
  1. _TITLE "Robohash API"
  2.  
  3. DIM i&
  4. i& = Robohash("qb64.org")
  5.  
  6. IF i& <> 0 THEN
  7.     SCREEN i&
  8.  
  9. FUNCTION Robohash& (stringhash AS STRING)
  10.     DIM URL AS STRING
  11.     DIM URLFile AS STRING
  12.     DIM a%
  13.     URLFile = stringhash + ".png"
  14.     URL = "https://robohash.org/" + stringhash
  15.     a% = API_request(URL, URLFile)
  16.     DIM U AS INTEGER
  17.     U = FREEFILE
  18.     OPEN URLFile FOR BINARY AS #U
  19.     IF LOF(U) <> 0 THEN
  20.         Robohash = _LOADIMAGE(URLFile, 32)
  21.     ELSE
  22.         CLOSE #U
  23.         KILL URLFile
  24.         Robohash = 0
  25.     END IF
  26.     CLOSE #U
  27.     KILL URLFile
  28.  
  29.  
  30.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  31.  
  32. FUNCTION API_request (URL AS STRING, File AS STRING)
  33.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
Shuwatch!