When I do not want to "destroy" the image quality, I do not use the
img
tag, I prefer to create a
div
by specifying exactly the width and height without compromising the image, thus:
CSS
.img {
background:url(url_da_imagem);
background-size:cover;
background-position:center;
background-repeat:no-repeat;
}
You can track the result in this DEMO
DEMO
Depending on the situation it is totally impracticable to use this method, however, I can ensure a good view of the image. I also advise the use for WEB applications, it is pleasant to use optimized images in mobile.
In my projects MVC C#
I use Simple.ImageResizer
, I use a controller
where I pass as a parameter to URL
of the image and it returns me the image readjusted according to the parameters that I also pass via query-string
.
To install in your project, in the console type:
Install-Package Simple.ImageResizer
My controller
stays this way
PhotoController
Add library reference
using Simple.ImageResizer.MvcExtensions;
And this will be your action
[OutputCache(VaryByParam = "*", Duration = 60 * 60 * 24 * 365)]
public ImageResult Index(string file, int w = 0, int h = 0)
{
return new ImageResult(file, w, h);
}
I make a route to write friendly%% of the image.
RouteConfig
routes.MapRoute(
name: "Photo",
url: "Photo/{file}",
defaults: new { controller = "Photo", action = "Index", file = ""}
);
Once you've done this you can put the images in this way.
<img src="Photo/caminho_da_foto.jpg?w=largura&h=altura" alt="" />
NOTE: In the projects we use a folder for images, then we have to map the path to not give problem, so just passing the file name through the URL, I will leave as an example my URL
mapped with the path. >
ProjectHelper
private static string path = @"D:\caminho\das\fotos\";
public static string PhotosUploadFolder(string subfolder = "")
{
return Util.path + subfolder;
}
And in% w / o% the return line would look like this:
return new ImageResult(ProjetoHelper.PhotosUploadFolder(file), w, h);
Both ways have solved my problem in several projects, I hope the idea applies to you.