privacy: add function to strip location data from photos

Add a function to strip GPS data from images before uploading to
hosting service.

Lightning-address: kernelkind@getalby.com
Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind
2024-02-19 15:53:54 -05:00
committed by William Casarin
parent 58326f679e
commit 8d815fe4d6
4 changed files with 87 additions and 0 deletions

View File

@@ -211,3 +211,31 @@ func process_image_metadatas(cache: EventCache, ev: NostrEvent) {
}
}
func removeGPSDataFromImage(fromImageURL imageURL: URL) -> Bool {
guard let source = CGImageSourceCreateWithURL(imageURL as CFURL, nil) else {
print("Failed to create image source.")
return false
}
let data = NSMutableData()
guard let type = CGImageSourceGetType(source),
let destination = CGImageDestinationCreateWithData(data, type, 1, nil) else {
print("Failed to create image destination.")
return false
}
let removeGPSProperties: CFDictionary = [kCGImagePropertyGPSDictionary as String: kCFNull] as CFDictionary
CGImageDestinationAddImageFromSource(destination, source, 0, removeGPSProperties)
if CGImageDestinationFinalize(destination) {
do {
try data.write(to: imageURL, options: .atomic)
return true
} catch {
print("Failed to write image data: \(error)")
return false
}
} else {
print("Failed to finalize image destination.")
return false
}
}