Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Best way to load and save files from a buffer when file type is not known #37

Closed
avin-kavish opened this issue Jun 18, 2019 · 6 comments
Labels
enhancement New feature or request question Further information is requested
Milestone

Comments

@avin-kavish
Copy link

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.

@avin-kavish avin-kavish changed the title Best way to load and save files when file type is not known by extension Question: Best way to load and save files from a buffer when file type is not known by extension Jun 19, 2019
@avin-kavish avin-kavish changed the title Question: Best way to load and save files from a buffer when file type is not known by extension Question: Best way to load and save files from a buffer when file type is not known Jun 19, 2019
kleisauke added a commit that referenced this issue Jun 20, 2019
- Support mixed images and constants within Bandrank() and Bandjoin().
- Check for null pointer from vips_image_write_to_memory().
@kleisauke
Copy link
Owner

libvips has two useful functions to quickly identify the image type:

I just made these functions available in NetVips with commit 3fffefc.

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 VipsForeignLoadGifBuffer.

If you also want to detect broken images, you can use this:

public string GetExtensionNonTruncated(byte[] buffer)
{
try
{
// The fail option makes NetVips throw an exception on a file format error
var image = Image.NewFromBuffer(buffer, fail: true, access: Enums.Access.Sequential);
// Calculate the average pixel value. That way you are guaranteed to read every pixel
// and the operation is cheap.
var avg = image.Avg();
// Unfortunately, vips-loader is the operation nickname, rather
// than the canonical name returned by vips_foreign_find_load().
var vipsLoader = (string) image.Get("vips-loader");
var suffixLength = vipsLoader.EndsWith("load_buffer") ? 11 : 4;
return vipsLoader.Substring(0, vipsLoader.Length - suffixLength);
}
catch (VipsException e)
{
Console.WriteLine($"Couldn't identify image extension: {e.Message}");
return null;
}
}

@kleisauke kleisauke added enhancement New feature or request question Further information is requested labels Jun 20, 2019
@avin-kavish
Copy link
Author

avin-kavish commented Jun 20, 2019

Thanks for the prompt attention, when is this shipping on nuget?

@kleisauke
Copy link
Owner

I'm a bit busy with college and other side projects these days, but I hope to release NetVips 1.1.0 a few days after libvips 8.8.1 is released (libvips 8.8.1 will be released within a few days). Version 1.1.0 is more or less ready to be shipped (:shipit:). NetVips.Native takes most of the time, because I try to keep the dependencies up-to-date (where possible).

If you want to test this earlier, you can use the nightly version of NetVips. Add the https://ci.appveyor.com/nuget/net-vips feed in the <packageSources> section of your NuGet.config:

<packageSources>
  <add key="netvips-nightly" value="https://ci.appveyor.com/nuget/net-vips" />
</packageSources>

And update NetVips to version 1.1.0.140-develop.

@kleisauke kleisauke added this to the 1.1.0 milestone Jun 20, 2019
@kleisauke
Copy link
Owner

NetVips 1.1.0-rc3 is released on NuGet. If there's nothing reported for a week, this will become 1.1.0 final.

@avin-kavish
Copy link
Author

FindLoadBuffer seems stable so far. Closing as resolved.

@kleisauke
Copy link
Owner

NetVips v1.1.0 is now available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants