Resize images with MVC 6 on Ubuntu running ASP.NET 5 on Mono
Asked Answered
R

4

0

How can I re-size an image in ASP.NET 5, MVC 6, DNX451, with MONO, running on Ubuntu?

I have been unable to work this out, as the standard components that I have used such as ImageProcessor and ImageResizer.NET seem not to be working.

Renaissance answered 15/4, 2016 at 9:46 Comment(0)
R
1

I am currently developing a website in DNX 4.5.1 (ASP.NET 5) and MVC 6, which is meant to be hosted on an Ubuntu server.

Recently I ran in to issues with re-sizing images, so I had to think out of the box. In my case, it was not necessary, to re-size images on my development environment, so I focused on how to handle this on my upcoming prod environment.

The solution was to use ImageMagick, which is a very nice little piece of software for Linux.

This is how I solved it:

if (_hostingEnvironment.IsProduction())
{
        var command = "-c 'convert " + filePath + " -resize 960x960 -quality 70 " + filePath + "'";

        Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = "/bin/bash";
        proc.StartInfo.Arguments = command;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = false;
        proc.Start();
}

So this works by uploading the file to some folder, in my case a temporary folder, then I execute the convert command. I overwrite the same file with the conversion parameters that I need in my project. You can use more parameters, if you want larger images or better quality.

This is a nice solution, but as I said, I have only focused on making this work on Ubuntu, which will be my production environment, and therefor it is encapsulated in an if clause, checking whether I am on prod or not, but a similar approach could probably also be possible in Windows environments, but I would rather go for some standard component to make that work.

Renaissance answered 15/4, 2016 at 9:46 Comment(1)
Be very careful running ImageMagick on a server. I wouldn't recommend it. imagetragick.comConfirm
B
2

check out this cross platform library: https://github.com/JimBobSquarePants/ImageSharp

sample usage:

using (FileStream stream = File.OpenRead("foo.jpg"))
using (FileStream output = File.OpenWrite("bar.jpg"))
{
    Image image = new Image(stream);
    image.Resize(image.Width / 2, image.Height / 2)
         .Greyscale()
         .Save(output);
}
Boloney answered 20/4, 2016 at 7:2 Comment(0)
R
1

I am currently developing a website in DNX 4.5.1 (ASP.NET 5) and MVC 6, which is meant to be hosted on an Ubuntu server.

Recently I ran in to issues with re-sizing images, so I had to think out of the box. In my case, it was not necessary, to re-size images on my development environment, so I focused on how to handle this on my upcoming prod environment.

The solution was to use ImageMagick, which is a very nice little piece of software for Linux.

This is how I solved it:

if (_hostingEnvironment.IsProduction())
{
        var command = "-c 'convert " + filePath + " -resize 960x960 -quality 70 " + filePath + "'";

        Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = "/bin/bash";
        proc.StartInfo.Arguments = command;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = false;
        proc.Start();
}

So this works by uploading the file to some folder, in my case a temporary folder, then I execute the convert command. I overwrite the same file with the conversion parameters that I need in my project. You can use more parameters, if you want larger images or better quality.

This is a nice solution, but as I said, I have only focused on making this work on Ubuntu, which will be my production environment, and therefor it is encapsulated in an if clause, checking whether I am on prod or not, but a similar approach could probably also be possible in Windows environments, but I would rather go for some standard component to make that work.

Renaissance answered 15/4, 2016 at 9:46 Comment(1)
Be very careful running ImageMagick on a server. I wouldn't recommend it. imagetragick.comConfirm
B
0

Another way is used from SkiaSharp lib and its linux package SkiaSharp.NativeAssets.Linux.

My code is publish in Image Resize On Linux Github Repository

Bogusz answered 26/5, 2023 at 16:17 Comment(0)
B
0

Another way is SixLab library(download from nuget package). follow bottom code to use that.

public string SixLabResizeImage(Stream fileContents,
int Width, int Height, string saveFilePath)
{
    try
    {
        using (FileStream output = System.IO.File.OpenWrite(saveFilePath))
        {
            SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(fileContents);
                image.Mutate(
                    i => i.Resize(Width, Height)
                          );
            
                image.Save(output, GetEncoder(saveFilePath));
        }

        _logger.LogInformation("ok");
        return "ok";
    }
    catch (Exception err)
    {
        _logger.LogInformation(err.ToString());
        return err.Message;
    }
}

    private static SixLabors.ImageSharp.Formats.IImageEncoder GetEncoder(string saveFilePath)
    {
        var ext = saveFilePath.Substring(saveFilePath.LastIndexOf('.')).ToLower();
        switch (ext)//".jpg", ".jpeg", ".bmp", ".png", ".gif", ".tiff"
        {
            case ".jpg":
            case ".jpeg":
                return new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder();
            case ".bmp":
                return new SixLabors.ImageSharp.Formats.Bmp.BmpEncoder();
            case ".png":
                return new SixLabors.ImageSharp.Formats.Png.PngEncoder();
            case ".gif":
                return new SixLabors.ImageSharp.Formats.Gif.GifEncoder();
            case ".tiff":
            case ".tif":
                return new SixLabors.ImageSharp.Formats.Tiff.TiffEncoder();
            default:
                return new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder();
        }

Reference link1 and link2. My code published in this link.

Bogusz answered 9/6, 2023 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.