Add support for JPG images

This commit is contained in:
2023-06-01 21:25:45 +02:00
parent 060f44f57e
commit aa81ffc0a7
3 changed files with 17 additions and 3 deletions

View File

@@ -1,9 +1,19 @@
extends HTTPRequest extends HTTPRequest
func GetRemoteImage(url): func GetRemoteImage(url):
var image = Image.new()
print("Fetching remote image %s" % url) print("Fetching remote image %s" % url)
request(url) request(url)
var res = await request_completed var res = await request_completed
var image = Image.new() var magicBytes = res[3].slice(0,8)
image.load_png_from_buffer(res[3]) print(magicBytes)
var error = null
if magicBytes == PackedByteArray([137, 80, 78, 71, 13, 10, 26, 10]):
print("Detected PNG File")
error = image.load_png_from_buffer(res[3])
if magicBytes == PackedByteArray([255, 216, 255, 224, 0, 16, 74, 70]):
print("Detected JPG File")
error = image.load_jpg_from_buffer(res[3])
if error != OK:
print("Error fetching image ", str(error))
return image return image

View File

@@ -59,8 +59,10 @@ func RenderComposition(composition):
for comp in composition: for comp in composition:
var type = comp["type"] var type = comp["type"]
if type == "text": if type == "text":
print("Render label")
await RenderLabel(comp) await RenderLabel(comp)
if type == "image": if type == "image":
print("Render image")
await RenderImage(comp) await RenderImage(comp)
func RenderImage(def): func RenderImage(def):
@@ -72,6 +74,7 @@ func RenderImage(def):
texture.set_size_override(Vector2(def["width"], def["height"])) texture.set_size_override(Vector2(def["width"], def["height"]))
imageNode.texture = texture imageNode.texture = texture
$"/root/Main/RenderContainer".add_child(imageNode) $"/root/Main/RenderContainer".add_child(imageNode)
print("Image added")
func RenderLabel(def): func RenderLabel(def):
var textNode = Label.new() var textNode = Label.new()
@@ -93,6 +96,7 @@ func RenderLabel(def):
textNode.horizontal_alignment = alignments[def["horizontalAlignment"]] textNode.horizontal_alignment = alignments[def["horizontalAlignment"]]
$"/root/Main/RenderContainer".add_child(textNode) $"/root/Main/RenderContainer".add_child(textNode)
print("Label added")
func render(): func render():
print("Rendering frame %s" % counter) print("Rendering frame %s" % counter)

View File

@@ -13,7 +13,7 @@ script = ExtResource("1_6qp7h")
script = ExtResource("3_mg2dt") script = ExtResource("3_mg2dt")
[node name="Remote" type="HTTPRequest" parent="."] [node name="Remote" type="HTTPRequest" parent="."]
use_threads = true process_mode = 3
script = ExtResource("4_k4afm") script = ExtResource("4_k4afm")
[node name="Placeholder" type="Sprite2D" parent="."] [node name="Placeholder" type="Sprite2D" parent="."]