Dynamic Image Resizing Module v3

今天忽然看到收藏夹里有一个IIS Download的网站(http://www.iis.net/downloads),其实就是微软的IIS官网,于是就想着点进去看看有没有什么好玩的东西,果然没有辜负我的期望,找到了一个名叫Dynamic Image Resizing Module v3的组件,说白了,就是IIS上处理图片的组件,不同于其它IIS的图片处理组件,需要在服务器端里写出代码去处理图片,这款插件,只需要在IIS上配置上了,访问图片文件时,只需加上指定的参数,输出的图片就会按这些参数去处理.这是在组件说明里提到的.

怀着好奇的心情,我点进去看了下.Image Resizing,网站打不开,看样子是需要翻长城才能下载.这难不到我.也不是此文的关键点,就略过吧.下载来是带源码和示例的,我直接按配置说明,有三种方法安装使用

* For a Visual Studio project

1) Use Project->Add Reference to add ImageResizer.dll to the project. 
2) Change the Project's web.config file to match the settings in the included Web.Config file.

* If you have NuGet, simply go to Tools->Library Package Manager->Package Manager Console
and run "Install-Package ImageResizer.WebConfig"

* If you're installing this on an IIS website, 

1) Copy ImageResizer.dll and ImageResizer.pdb into the /bin directory of the site (create the folder if it doesn't exist.
2) Copy Web.config into the root of the website. If it already exists, you'll need to add the new settings into the old file.
我需要的是第三种,直接把那两个文件拉到测试网站的bin目录下,在web.config里加入说明的几段内容.(注意,我的IIS是在Win7上面配置的IIS7,如果是IIS6及以下版本,是不能直接支持web.config配置的.)
我的原始web.config里只有几个默认文档配置,所以这里把配置好的内容全部帖出来供大家参考:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<configSections>
		<section name="resizer" type="ImageResizer.ResizerSection,ImageResizer"  requirePermission="false" />
	</configSections>
    <system.webServer>
        <defaultDocument enabled="true">
            <files>
                <clear />
                <add value="index.asp" />
                <add value="index.php" />
                <add value="index.htm" />
                <add value="index.html" />
            </files>
        </defaultDocument>
        <staticContent>
            <mimeMap fileExtension=".xz" mimeType="application/octet-stream" />
        </staticContent>
		<validation validateIntegratedModeConfiguration="false"/>
		<modules>
			<!-- This is for IIS7+ Integrated mode -->
			<add name="ImageResizingModule" type="ImageResizer.InterceptModule"/>
		</modules>
    </system.webServer>
</configuration>

然后放一个800*600的图片f.jpg到要目剥下 ,在浏览器输入访问地址:http://test.test.com/f.jpg(如何在本机测试时使用域名?),显示的是原始图片

再输入地址:http://test.test.com/f.jpg?width=500&borderColor=00ff00&borderWidth=10&rotate=45,图片变成了宽度500,高度按比例缩放旋转45度加了10px绿色边框缩略图了.这说明配置成功.

这个组件说明上支持以下参数及设置
width, height - force the width and/or height to certain dimensions. Whitespace will be added if the aspect ratio is different.
maxwidth, maxheight - fit the image within the specified bounds. (Most often used)
crop=auto - Crop the image the the size specified by width and height. Centers and minimally crops to preserve aspect ratio.
crop=(x1,y1,x2,y2) - Crop the image to the specified rectangle on the source image. You can use negative coordinates to specifiy bottom-right relative locations.
rotate=degress - Rotate the image.
bgcolor=color name| hex code (6-char). Sets the background/whitespace color.
stretch=fill - Stretches the image to width and height if both are specified. This is the only way to lose aspect ratio.
scale=both|upscaleonly|downscaleonly - By default, images are never upscaled. Use &scale=both to grow an image.
flip=h|v|both - Flips the image after resizing.
sourceFlip=h|v|both - Flips the source image before resizing/rotation.
paddingWidth=px & paddingColor=color|hex. paddingColor defaults to bgcolor, which defaults to white.
borderWidth=px, borderColor=color|hex.
format=jpg|png|gif
colors=2-255 - Control the palette size of PNG and GIF images. If omitted, PNGs will be 24-bit.
frame=x - Choose which frame of an animated GIF to display.
page=x - Choose which page of a multi-page TIFF document to display.


怎么样?功能全吧,基本上其它组件需要写代码去做的事情,这里只要加几个参数就搞定了.
那么,至于实际项目中好不好用,用到的时候再说吧.
总之,个人觉得很有用的东西,一定要记一下.