imazen / imageflow-dotnet

The official .NET API for Imageflow, the Rust image processing and optimization engine for web servers
GNU Affero General Public License v3.0
143 stars 25 forks source link

PDF - 'ImageMalformed: NoEnabledDecoderFound #41

Closed AnujAngooral closed 2 years ago

AnujAngooral commented 2 years ago

I am trying to extract the image from a PDF file. It used to work in ImageResizer with PdfRendererPlugin.

imageResizer = new Lazy<Config>( () => { var config = new Config(); new PdfRendererPlugin().Install(config); return config; }); var job = new ImageJob(sourceStream, destStream, new Instructions { OutputFormat = OutputFormat.Jpeg, Width = 1024 }); imageResizer.Value.CurrentImageBuilder.Build(imageJob);

How can I achieve the same in the Imageflow with .net standard? I tried the following code. But it gives error: Imageflow.Bindings.ImageflowException: 'ImageMalformed: NoEnabledDecoderFound: No ENABLED decoder found for file starting in [25, 50, 44, 46, 2D, 31, 2E, 33, D, 25, E2, E3]

`string imageSourcePath = "https://mywebsite/test.pdf";

        var request = HttpWebRequest.Create(imageSourcePath);
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            using (var responseStream = response.GetResponseStream())
            {
                MemoryStream sourceStream = new MemoryStream();
                responseStream.CopyTo(sourceStream);
                sourceStream.Seek(0, SeekOrigin.Begin);

     using (var b = new ImageJob())
                {
                    try
                    {

                        var r = await b.Decode(sourceStream, false).ResizerCommands("width=1024")

                           .EncodeToBytes(new MozJpegEncoder(100, true)).Finish().InProcessAsync();

                        var bytes = r.First.TryGetBytes();
                        using (var stream = new MemoryStream(bytes.Value.Array))
                        {

                            Image img = Image.FromStream(stream);

                            img.Save(@"D:\temp" + "\\" + Guid.NewGuid() + ".Jpeg", ImageFormat.Jpeg);
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

            }
        }

`

lilith commented 2 years ago

Imageflow does not have a PDF decoder for security reasons; they are easy to exploit.

On Wed, Feb 16, 2022, 12:11 AM Anuj Angooral @.***> wrote:

I am trying to extract the image from a PDF file. It used to work in ImageResizer with PdfRendererPlugin.

imageResizer = new Lazy( () => { var config = new Config(); new PdfRendererPlugin().Install(config); return config; }); var job = new ImageJob(sourceStream, destStream, new Instructions { OutputFormat = OutputFormat.Jpeg, Width = 1024 }); imageResizer.Value.CurrentImageBuilder.Build(imageJob);

How can I achieve the same in the Imageflow with .net standard? I tried the following code. But it gives error: Imageflow.Bindings.ImageflowException: 'ImageMalformed: NoEnabledDecoderFound: No ENABLED decoder found for file starting in [25, 50, 44, 46, 2D, 31, 2E, 33, D, 25, E2, E3]

`string imageSourcePath = "https://mywebsite/test.pdf";

    var request = HttpWebRequest.Create(imageSourcePath);
    using (var response = (HttpWebResponse)request.GetResponse())
    {
        using (var responseStream = response.GetResponseStream())
        {
            MemoryStream sourceStream = new MemoryStream();
            responseStream.CopyTo(sourceStream);
            sourceStream.Seek(0, SeekOrigin.Begin);

 using (var b = new ImageJob())
            {
                try
                {

                    var r = await b.Decode(sourceStream, false).ResizerCommands("width=1024")

                       .EncodeToBytes(new MozJpegEncoder(100, true)).Finish().InProcessAsync();

                    var bytes = r.First.TryGetBytes();
                    using (var stream = new MemoryStream(bytes.Value.Array))
                    {

                        Image img = Image.FromStream(stream);

                        img.Save(@"D:\temp" + "\\" + Guid.NewGuid() + ".Jpeg", ImageFormat.Jpeg);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

        }
    }

`

— Reply to this email directly, view it on GitHub https://github.com/imazen/imageflow-dotnet/issues/41, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2LHYGCXGND2JLA5MOC4DU3NE2LANCNFSM5OQXMEBA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>