How to get the dimension of a png file when local importing to roblox studio?

So I wanna create a command script that easily makes it easier when I’m editing in Roblox Studio
but I’m kinda confused on how to use File’s GetBinaryContent() to convert it into useful information since the Documentation about it doesn’t work.

I would appreciate it if someone who knows about these stuffs to tell me about it.

png is a complicated format with a specification that’s quite long. You can read through that to figure out how to do it, and there’s probably tutorials for other programming languages out there.

If you want to stick with png, try searching for pure lua 5.1 implementations like this one: GitHub - kartik-s/lua-png: An almost standards compliant decoder for the PNG file format

EDIT: Actually, it’s not that complicated to find the dimensions from the binary png data. If you start reading the spec from 11.2 “Critical Chunks”, the info is right there. Here’s how you can get the image dimensions:

local file = game:GetService("StudioService"):PromptImportFile()
local data = file:GetBinaryContents()

local IHDR_start, IHDR_end = data:find("IHDR")
local DIM_BYTES = 4
local width_start = IHDR_end
local height_start = width_start + DIM_BYTES
local width, height = 0, 0

for i = 1, DIM_BYTES do
	local byte = string.byte(data:sub(width_start+i, width_start+i)) --turn each character into a number matching the byte that codes for said character
	width += bit32.lshift(byte, 8 * (DIM_BYTES-i)) --lshifting because first bytes are most significant
end

for i = 1, DIM_BYTES do
	local byte = string.byte(data:sub(height_start+i, height_start+i))
	height += bit32.lshift(byte, 8 * (DIM_BYTES-i))
end

print(width, height)
2 Likes