The ImageResizer module for IIS

In the Wake of the recent articles about resizing images on the server, I decided to share the implementation experience of the module ImageResizer for IIS. Of course to write a simple review would be too boring, but we are not limited to a simple introduction module.
So, we have has been bought ImageResizer + set of plug-ins Cloud Bundle to work with clouds.
Unfortunately I'm not allowed to name the projects which implemented this solution, but I can outline the essence. This news portals focused on North America. The site presents articles and videos.
Articles are stored in the database, the editor can attach to the article the title picture. The image is stored in Amazon S3. As You know, the editor can upload an image as small or huge and this is not to lose the original.
Video files are loaded, processed and stored in cloud storage video BrightCove. After you upload the video in the vault, it is strongly processed, re-encoded several streams in different formats and for video it automatically selects the thumbnail (thumbnail).
Of course, this all happens in the cloud, then the image is stored in the cloud, and we get a direct link to insert. But here, the picture may not always match the required size.
We have a total set of images on Amazon S3, a set of images in BrightCove and all images need to be able to quickly get adjusted to the right size. In addition, in different parts of the site may be used by various image sizes. In this case set plugin caching we had, but we had CloudFront.

Oddly enough, comrade bitmap in his article did not mention exactly how it resizes the image, because there are many ways (flour determine the required variant with the customer You can see in the photo). You can stretch in the frame, getting stretched face, you can maintain aspect ratio and fill the empty space with color. You can preserve the aspect ratio and trim the edges. The module also allows a bunch more we don't need eye candy, like applying filters and other things. In the end, on one project we started to stretch stretching is similar to css, but on another project, and filling color.
Standard scenarios
The default out of the box it is assumed that the image and IIS are located on the same server and resizing is as simple as adding parameters to the link:
/image.jpg?height=200&width=300
or
/image.jpg?h=200&w=300
. Fine, but how to get image from Amazon S3?
S3 Reader
Here we come to the aid of the first set of plug-ins of Cloud Bundle: S3 Reader. By simple settings of the web.config, it allows you to use the link in the following format:
/s3/bucket/folder/image.jpg?h=200&w=300
RemoteReader plugin
OK, now we can give the pictures from the S3, but S3 is a pretty popular thing, how about a completely different server? It includes RemoteReader plugin. There is not so simple. The end-link has the following format:
/remote.jpg.ashx?w=200&height=300&urlb64=45b45c4a2099b...&hmac=a2099ba2099b
. Where remote.jpg.ashx is the end point to intercept the links module, the parameter urlb64 is simply encrypted using the HMAC SHA-256 of the end link to a remote server with a picture. This link generates a special method:
RemoteReaderPlugin.Current.CreateSignedUrl("http://remote.com/image.jpg");
CloudFront
OK, with the main task we did. Now we need to somehow cache the result, otherwise all this will not work.
Caching plugins we have, but there is such a magical thing as CloudFront custom origin. We have created a new CloudFront distribution that points to our domain. So now we have links in the format:
and
httр://my-distribution-name.cloudfront.com/s3/bucket/folder/image.jpg?h=200&w=300
But now our website is open at httr://my-distribution-name.cloudfront.com, scoring cloudfront. Then we quickly added a section for pictures. And with CloudFront links
httр://my-distribution-name.cloudfront.com/s3/bucket/folder/image.jpg?h=200&w=300
began to redirect the request to
httr://my-site.com/images/s3/bucket/folder/image.jpg?h=200&w=300
Like all should work, but suddenly it turned out that CloudFront mercilessly cuts long string of options, and we've received tons of errors and not bugs. Here we helped CloudFront plugin, allowing you to convert links to the following format:
httр://my-distribution-name.cloudfront.com/remote.jpg.ashx;w=200;height=300;urlb64=45b45c4a2099b...;hmac=a2099ba2099b
and
httр://my-distribution-name.cloudfront.com/s3/bucket/folder/image.jpg;h=200;w=300
quick links
All this works fine, but engineering went even further. What if someone starts to generate we do not need the resolution of pictures? The maximum size increase is naturally limited, but still it can заDDOSить website, and even received pictures will soon spread hundreds of servers CloudFront worldwide. And of course link length is just terrible. Then we decided to implement the functionality of the short link.
When generating the link we put in the base with a long link such as:
/remote.jpg.ashx;w=200;height=300;urlb64=45b45c4a2099b...;hmac=a2099ba2099b
and get its id in the table. In layout the same vypilivaem link format:
httr://my-distribution-name.cloudfront.com/42
CloudFront appeals to us at:
httr://my-site.com/i/42
We intercept the request, select the long link according to the key 42 from a DB in server code do a HttpRequest to itself so that it caught ImageResizer and in response display the result of its work. CloudFront adds the image obtained on their servers and we do not receive this request until the next invalidation of the cache.
And the final touch is hiding a terrible prefix CloudFront links, which is a jumble of numbers and letters, which is not very nice, with the added domain.
As a result the end user link is:
httr://short.com/42
In this case the image is cached, easily accessible with the closest to the server account, the evil hacker can change the size, заDDOSив our module, we have a short link and most importantly all the pictures with the right dimensions look beautiful in their places.
Opinion
In conclusion, I want to say that the plugin works great, has a bunch of different settings and plug-ins on all occasions, has a debug mode in the event of an emergency. The author answers when dealing with problems and even seems to be paying for bugs. Of course, the module is paid, but it's worth it.
Комментарии
Отправить комментарий