aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Library/Extensions.swift
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2025-11-07 17:40:59 -0800
committerDustin Mierau <dustin@mierau.me>2025-11-07 17:40:59 -0800
commit637cd9af54a58db0c5dc2062b6c20291afd31147 (patch)
tree19c0168a9ee0a2bcb158fdae6192aab361921475 /Hotline/Library/Extensions.swift
parent6afa5551add4541f376867b3d6527df9a0f793f3 (diff)
Add Kingfisher dependency. Add support for animated GIF banners. Add fast image format detection to Data.
Diffstat (limited to 'Hotline/Library/Extensions.swift')
-rw-r--r--Hotline/Library/Extensions.swift36
1 files changed, 36 insertions, 0 deletions
diff --git a/Hotline/Library/Extensions.swift b/Hotline/Library/Extensions.swift
index 469676c..b6a4b68 100644
--- a/Hotline/Library/Extensions.swift
+++ b/Hotline/Library/Extensions.swift
@@ -22,6 +22,42 @@ extension Data {
}
return false
}
+
+ enum ImageFormat {
+ case gif
+ case jpeg
+ case png
+ case webp
+ case unknown
+ }
+
+ var detectedImageFormat: ImageFormat {
+ guard self.count >= 12 else { return .unknown }
+
+ let bytes = [UInt8](self.prefix(12))
+
+ // GIF: "GIF8"
+ if bytes[0] == 0x47 && bytes[1] == 0x49 && bytes[2] == 0x46 && bytes[3] == 0x38 {
+ return .gif
+ }
+
+ // JPEG: FF D8 FF
+ if bytes[0] == 0xFF && bytes[1] == 0xD8 && bytes[2] == 0xFF {
+ return .jpeg
+ }
+
+ // PNG: 89 50 4E 47 0D 0A 1A 0A
+ if bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47 && bytes[4] == 0x0D && bytes[5] == 0x0A && bytes[6] == 0x1A && bytes[7] == 0x0A {
+ return .png
+ }
+
+ // WebP: "RIFF" at 0-3 and "WEBP" at 8-11
+ if bytes[0] == 0x52 && bytes[1] == 0x49 && bytes[2] == 0x46 && bytes[3] == 0x46 && bytes[8] == 0x57 && bytes[9] == 0x45 && bytes[10] == 0x42 && bytes[11] == 0x50 {
+ return .webp
+ }
+
+ return .unknown
+ }
}
// MARK: -