158 words, 1 min read

If you ever need a way to create an empty PNG in Elixir, this will do the trick:

defmodule EmptyPNG do
def generate() do
<<137, 80, 78, 71, 13, 10, 26, 10>> <>
ihdr_chunk() <>
idat_chunk() <>
iend_chunk()
end
defp ihdr_chunk() do
width = 1
height = 1
bit_depth = 8
color_type = 6 # RGBA
compression = 0
filter = 0
interlace = 0
data =
<<width::big-32, height::big-32, bit_depth, color_type, compression, filter, interlace>>
chunk("IHDR", data)
end
defp idat_chunk() do
# 1x1 pixel with RGBA(0,0,0,0) (fully transparent)
pixel_data = <<0, 0, 0, 0, 0>> # PNG filter type (0) + RGBA (4 bytes)
compressed = :zlib.compress(pixel_data)
chunk("IDAT", compressed)
end
defp iend_chunk(), do: chunk("IEND", <<>>)
defp chunk(type, data) do
length = byte_size(data)
crc = :erlang.crc32(type <> data)
<<length::big-32>> <> type <> data <> <<crc::big-32>>
end
end

To get the raw bytes of an empty PNG, execute:

EmptyPNG.generate()

This generates a minimal 1x1 transparent PNG using the PNG format specification.