Exemple #1
0
        protected async Task <byte[]> ResizeImageBytes(byte[] imageData, uint?desiredWidth, uint?desiredHeight)
        {
            using (var job = new FluentBuildJob())
            {
                var res = await job.Decode(imageData).ConstrainWithin(desiredWidth, desiredHeight)
                          .EncodeToBytes(new LibJpegTurboEncoder()).FinishAsync();

                var bytes = res.First.TryGetBytes();
                return(bytes.HasValue ? bytes.Value.Array : new byte[] {});
            }
        }
        public async Task TestBuildJob()
        {
            var imageBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII=");

            using (var b = new FluentBuildJob())
            {
                var r = await b.Decode(imageBytes).FlipHorizontal().Rotate90().Distort(30, 20).ConstrainWithin(5, 5)
                        .EncodeToBytes(new GifEncoder()).Finish().InProcessAsync();

                Assert.Equal(5, r.First.Width);
                Assert.True(r.First.TryGetBytes().HasValue);
            }
        }
        public async Task TestFilesystemJobPrep()
        {
            var isUnix = Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX;

            var imageBytes = Convert.FromBase64String(
                "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII=");

            using (var b = new FluentBuildJob())
            {
                string jsonPath;
                using (var job = await b.Decode(imageBytes).FlipHorizontal().Rotate90().Distort(30, 20)
                                 .ConstrainWithin(5, 5)
                                 .EncodeToBytes(new GifEncoder()).FinishWithTimeout(2000)
                                 .WriteJsonJobAndInputs(true))
                {
                    jsonPath = job.JsonPath;

                    if (isUnix)
                    {
                        Assert.True(File.Exists(jsonPath));
                    }
                    else
                    {
                        using (var file = System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(jsonPath))
                        {
                        } // Will throw filenotfoundexception if missing
                    }
                }

                if (isUnix)
                {
                    Assert.False(File.Exists(jsonPath));
                }
                else
                {
                    Assert.Throws <FileNotFoundException>(delegate()
                    {
                        using (var file = System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(jsonPath))
                        {
                        }
                    });
                }
            }
        }
        /// <summary>
        /// Resizes the specified image bytes.
        /// </summary>
        public async Task <byte[]> Resize(byte[] imageBytes, int maxSize)
        {
            if (imageBytes.Length == 0)
            {
                return(imageBytes);
            }

            using (var stream = new MemoryStream())
            {
                using (var b = new FluentBuildJob())
                {
                    await b.Decode(imageBytes)
                    .ConstrainWithin((uint)maxSize, (uint)maxSize)
                    .Encode(new StreamDestination(stream, true), new LibJpegTurboEncoder())
                    .FinishAsync();
                }

                return(stream.GetBuffer());
            }
        }
Exemple #5
0
        protected static async Task <byte[]> ResizeImageBytes(byte[] imageData, uint?desiredWidth, uint?desiredHeight, IEmailSender _emailSender)
        {
            try
            {
                using (var job = new FluentBuildJob())
                {
                    var res = await job.Decode(imageData).ConstrainWithin(desiredWidth, desiredHeight)
                              .EncodeToBytes(new LibJpegTurboEncoder()).Finish().InProcessAsync();

                    var bytes = res.First.TryGetBytes();
                    return(bytes.HasValue ? bytes.Value.Array : new byte[] { });
                }
            }
            catch (Exception ex)
            {
                await _emailSender.SendEmailAsync("*****@*****.**", "Error La Pesca en Línea ", ex.ToString());

                return(null);
            }
        }
Exemple #6
0
        public async Task TestFilesystemJobPrep()
        {
            var imageBytes = Convert.FromBase64String(
                "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII=");

            using (var b = new FluentBuildJob())
            {
                string jsonPath;
                using (var job = await b.Decode(imageBytes).FlipHorizontal().Rotate90().Distort(30, 20)
                                 .ConstrainWithin(5, 5)
                                 .EncodeToBytes(new GifEncoder()).FinishWithTimeout(2000)
                                 .WriteJsonJobAndInputs(true))
                {
                    jsonPath = job.JsonPath;

                    Assert.True(File.Exists(jsonPath));
                }
                Assert.False(File.Exists(jsonPath));
            }
        }
        public async Task TestBuildJobSubprocess()
        {
            string imageflowTool = Environment.GetEnvironmentVariable("IMAGEFLOW_TOOL");

            if (!string.IsNullOrWhiteSpace(imageflowTool))
            {
                var imageBytes = Convert.FromBase64String(
                    "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII=");
                using (var b = new FluentBuildJob())
                {
                    var r = await b.Decode(imageBytes).FlipHorizontal().Rotate90().Distort(30, 20).ConstrainWithin(5, 5)
                            .EncodeToBytes(new GifEncoder()).FinishWithTimeout(2000)
                            .InSubprocessAsync(imageflowTool);

                    // ExecutableLocator.FindExecutable("imageflow_tool", new [] {"/home/n/Documents/imazen/imageflow/target/release/"})

                    Assert.Equal(5, r.First.Width);
                    Assert.True(r.First.TryGetBytes().HasValue);
                }
            }
        }
        public async Task TestCustomDownscaling()
        {
            var imageBytes = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII=");

            using (var b = new FluentBuildJob())
            {
                var cmd = new DecodeCommands
                {
                    DownscaleHint       = new Size(20, 20),
                    DownscalingMode     = DecderDownscalingMode.Fastest,
                    DiscardColorProfile = true
                };
                var r = await b.Decode(new BytesSource(imageBytes), 0, cmd)
                        .Distort(30, 20, 50.0f, InterpolationFilter.RobidouxFast, InterpolationFilter.Cubic)
                        .ConstrainWithin(5, 5)
                        .EncodeToBytes(new LibPngEncoder()).Finish().InProcessAsync();

                Assert.Equal(5, r.First.Width);
                Assert.True(r.First.TryGetBytes().HasValue);
            }
        }