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
func GetRemoteImage(url):
var image = Image.new()
print("Fetching remote image %s" % url)
request(url)
var res = await request_completed
var image = Image.new()
image.load_png_from_buffer(res[3])
var magicBytes = res[3].slice(0,8)
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

View File

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

View File

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