⭐ Moe-Counter Compatible Website Hit Counter Written in Gleam
mayu.due.moe
hit-counter
svg
moe
1pub type ImageInformation {
2 ImageInformation(width: Int, height: Int, extension: String)
3}
4
5pub fn get_image_information(image) {
6 case image {
7 <<0x89, "PNG\r\n":utf8, 0x1A, "\n":utf8, _rest:bits>> ->
8 parse_png_chunks(image, 8)
9 <<
10 "GIF":utf8,
11 _version:bytes-3,
12 width:little-16,
13 height:little-16,
14 _rest:bits,
15 >> -> Ok(ImageInformation(width, height, "gif"))
16 _ -> Error("Unsupported image format")
17 }
18}
19
20fn parse_png_chunks(image, offset) {
21 case image {
22 <<
23 _:unit(8)-size(offset),
24 _length:32,
25 "IHDR":utf8,
26 width:32,
27 height:32,
28 _rest:bits,
29 >> -> Ok(ImageInformation(width, height, "png"))
30 <<_:size(offset), length:32, _:4, _:bits>> ->
31 parse_png_chunks(image, offset + length + 12)
32 _ -> Error("Invalid PNG chunk")
33 }
34}