Unpackaged images question

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gp1


using Godot 3.0.6, can I download an image from the server at runtime and then cut it to multiple pieces as per the project’s needs?

basically, the image will not be exported with the package.
In other words, the project will have no prior knowledge about this image until the download moment.

Thank you in advance!

:bust_in_silhouette: Reply From: Calinou

You can use the HTTPRequest class to download an image and load it at run-time. See the snippet below for an example:

func _ready():
    # Create an HTTP request node and connect its completion signal
    var http_request = HTTPRequest.new()
    http_request.connect("request_completed", self, "_http_request_completed")

    # Perform the HTTP request. The URL below returns a PNG image as of writing.
    var http_error = http_request.request("https://via.placeholder.com/500")
    if http_error != OK:
        print("An error occurred in the HTTP request.")

# Called when the HTTP request is completed.
func _http_request_completed(result, response_code, headers, body):
    var image = Image.new()
    var image_error = image.load_png_from_buffer(body)
    if image_error != OK:
        print("An error occurred while trying to display the image.")

    var texture = ImageTexture.new()

    # Assign to the child TextureRect node
    $TextureRect.texture = texture

The Image class also has load_jpg_from_buffer() and load_webp_from_buffer() to load JPEG and WebP images.

Thank you.
Basically, we can use images that are not exported with the project but we will not have the benefits of the importation process as per the following link “Importing Images” until the ticket #17848 is completed “Cannot import any resource from any folder other than res://”.
Is my understanding correct?

Actually, the ticket #17848 is better explained in the original ticket, #17748Expose load/import methods for use with file from user directory”.

gp1 | 2019-02-03 14:54

Hi Calinou,
Above example fine working,
when i try facebook user photo url to load in textureRect but in android devices blank(black) image set [In Ios and simulator properly work].

In android device file extension(.png,.jpg) url fine work, but without file extension url not set image it’s blank image set.
In Android Work fine this url : http://docs.godotengine.org/en/stable/_static/docs_logo.png

how can i fix this one give me any help, Thank you.

Shailesh dhaduk | 2019-06-08 05:53

Just wondering, we don’t need to deallocate (delete) that http_request variable later on, right?

aliwoto | 2022-10-15 23:48