问题描述
如何在 ASP.NET 5、MVC 6、DNX451、MONO、Ubuntu 上重新调整图像大小?
How can I re-size an image in ASP.NET 5, MVC 6, DNX451, with MONO, running on Ubuntu?
我一直无法解决这个问题,因为我使用的标准组件(例如 ImageProcessor 和 ImageResizer.NET)似乎不起作用.
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.
推荐答案
我目前正在 DNX 4.5.1 (ASP.NET 5) 和 MVC 6 中开发一个网站,该网站旨在托管在 Ubuntu 服务器上.
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.
解决方案是使用 ImageMagick,这是一个非常好的 Linux 小软件.
The solution was to use ImageMagick, which is a very nice little piece of software for Linux.
我是这样解决的:
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.
这是一个很好的解决方案,但正如我所说,我只专注于在 Ubuntu 上进行这项工作,这将是我的生产环境,因此它被封装在一个 if 子句中,检查我是否在生产,但类似的方法在 Windows 环境中也可能是可能的,但我更愿意使用一些标准组件来使其工作.
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.
这篇关于在 Mono 上运行 ASP.NET 5 的 Ubuntu 上使用 MVC 6 调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!