Question: Best way to load and save files from a buffer when file type is not knownSee original GitHub issue
I’m loading images from the
IFormFile container. I’m not sure what the best way is to find the image type so that I can save the images in the original format they were uploaded in.
IFormFile.ContentType might not be reliable. I’m aware that libvips sniffs the first few bytes when loading a file from disk, is there a way to replicate that behaviour in this library? Perhaps to sniff from the intermediary buffer I’m using to load the image.
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
libvips has two useful functions to quickly identify the image type:
I just made these functions available in NetVips with commit https://github.com/kleisauke/net-vips/commit/3fffefc9c2aab64fbc3928d629823595ab333335.
Note that these functions will not detect truncated images, it just performs some basic verification of image metadata. For example, reading a buffer with
GIF89a will result in
If you also want to detect broken images, you can use this: https://github.com/kleisauke/net-vips/blob/3fffefc9c2aab64fbc3928d629823595ab333335/samples/NetVips.Samples/Samples/IdentifyExtension.cs#L40-L63
NetVips 1.1.0-rc3 is released on NuGet. If there’s nothing reported for a week, this will become 1.1.0 final.