SixLabors / ImageSharp

:camera: A modern, cross-platform, 2D Graphics library for .NET
https://sixlabors.com/products/imagesharp/
Other
7.42k stars 852 forks source link

none-progressive jpeg with missing EOI marker throws EOFException #83

Closed vaindil closed 7 years ago

vaindil commented 7 years ago

I'm on 1.0.0-alpha1-00049, .NET Core 1.1.0. An image I'm testing with (attached) is throwing the below exception when I call var image = new Image(stream);. All other images I've tested work properly, but I'm not sure what's happening with this one.

(In case GitHub altered it, I've uploaded the image here.)

Exception:

EOFException: Exception of type 'ImageSharp.Formats.JpegDecoderCore+EOFException' was thrown.

My code:

[HttpPost]
public async Task<IActionResult> Upload(IFormFile image)
{
    using (var stream = new MemoryStream())
    {
        await image.CopyToAsync(stream);

        // image verification, stream variable is not touched/used

        stream.Seek(0, SeekOrigin.Begin);

        StripExif(stream);

        // do stuff
    }

    return Ok();
}

public static void StripExif(Stream stream)
{
    // exception thrown here
    var image = new Image(stream);

    stream.Seek(0, SeekOrigin.Begin);

    if (image.ExifProfile == null)
        return;

    // a ton of these follow
    image.ExifProfile.RemoveValue(ExifTag.GPSLatitude);

    // ...

    image.Save(stream);
    stream.Seek(0, SeekOrigin.Begin);
}

Beginning of stack trace:

ImageSharp.Formats.JpegDecoderCore+EOFException: Exception of type 'ImageSharp.Formats.JpegDecoderCore+EOFException' was thrown.
   at ImageSharp.Formats.JpegDecoderCore.Decode[TColor](Image`1 image, Stream stream, Boolean configOnly)
   at ImageSharp.Formats.JpegDecoder.Decode[TColor](Image`1 image, Stream stream)
   at ImageSharp.Image`1.Decode(Stream stream)
   at ImageSharp.Image`1.Load(Stream stream)
   at [removed].StripExif(Stream stream) in [removed]\UploadHelpers.cs:line 39
   at [removed].ImagesController.<Upload>d__5.MoveNext() in [removed]\ImagesController.cs:line 66
vaindil commented 7 years ago

Shoot, I just saw #18, not sure how I missed it. This is probably a duplicate.

JimBobSquarePants commented 7 years ago

Thanks yeah, a duplicate. Let's close this.

tocsoft commented 7 years ago

reopening as this is slightly different to the other issue... this is a none-progressive image the other is progressive

tocsoft commented 7 years ago

@vaindil The issue effecting your image has now been fixed if you upgrade ImageSharp.Formats.Jpeg to latest you should get the fix. (Fix landed in ImageSharp.Formats.Jpeg version 1.0.0-alpha1-00045)

Note this only fixes the issue for none-progressive jpegs so depending on what images you have uploaded it might not work for all of them but it should help some. If you get any more that you can still open locally then it more than likely going to be related to issue #18.