When I don’t want to "destroy" the image quality, I don’t use the tag img
, I’d rather create a div
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 follow the result in this DEMO
Depending on the situation it is totally impossible to use this method, however, I can ensure a good image visualization. I also advise the use for WEB applications, in mobile it is pleasant to use optimized images.
In my projects MVC C#
I use the Simple.ImageResizer
, in it I use a controller
where I pass as parameter to URL
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
Man controller
is this way
Photocontroller
Add the library reference
using Simple.ImageResizer.MvcExtensions;
And this will be yours 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 to URL
of the image.
Routeconfig
routes.MapRoute(
name: "Photo",
url: "Photo/{file}",
defaults: new { controller = "Photo", action = "Index", file = ""}
);
Done this you can put the images this way.
<img src="Photo/caminho_da_foto.jpg?w=largura&h=altura" alt="" />
OBS.: In projects we use a folder for images, so we have to map the path not to give problem, thus passing only the name of the file by the URL, I will leave as an example my helper
mapped with the path.
Projectohelper
private static string path = @"D:\caminho\das\fotos\";
public static string PhotosUploadFolder(string subfolder = "")
{
return Util.path + subfolder;
}
And in the controller
the return line would look this way:
return new ImageResult(ProjetoHelper.PhotosUploadFolder(file), w, h);
Both ways have solved my problem in several projects, I hope the idea applies to you.
You want to restrict the size at the time of upload or resize their size in the view?
– Caputo
Treat image in view.
– Diego Zanardo
Do you have any side server script on your site? ASP, PHP, . Net, JSP ?
– Jorge Campos
.NET, with MVC !!!
– Diego Zanardo