Simple.ImageResizer demo

Upload photo for demo. The image is resized and output as a jpg 90% quality which is a good balance between size and quality. You can also Download the library

700 * 300

image

500 * 200

image

100 * 100

image

How to install

To add this to your site run

            install package simple.imageresizer.mvcextensions
            
This will add a ImageResult class allowing you to add this controller action to your site
            public class ImagesController : Controller
            {
                [OutputCache(VaryByParam = "*", Duration = 60 * 60 * 24 * 365)]
                public ImageResult Index(string filename, int w = 0, int h = 0)
                {
                    string filepath = Path.Combine(Server.MapPath("~/images2"), filename);
                    return new ImageResult(filepath, w, h);
                }
            }
            
Notice how you can pass in width and height into the ImageResult constructor. The output cache and some custom code in the ImageResult will agressively cache the image response on the client. The ImageResult class also takes care of caching the resized image to disk in subfolders where the original image is located. Therefore make sure that the asp.net user have write access to the image folder. Also you will need to add a custome mvc route to map image requests to your controller action.
            routes.MapRoute(
                "Image", "images/{filename}",
                new { controller = "Images", action = "Index", filename = "" }
            );
            
Last you may also need to add this line to your web.config to make sure the mvc pipeline is handling the image request
            <system.webServer>
                <runAllManagedModulesForAllRequests="true" />
            </system.webServer>
            
Code is hosted at github: https://github.com/terjetyl/Simple.ImageResizer
Comments and pull requests are welcome.