第二章:生成缩略图Creating Thunbnails
2.1 缩略图生成步骤Thumbnail-Creating Steps
用AspJpeg创建一个基图的缩略图,必须执行以下几个步骤:To create a basic image thumbnail with AspJpeg, the following steps must be taken:
- 创建AspJpeg元件的一个实例Create an instance of the AspJpeg object.
- 可以任意选用Open或者OpenBinary方法从磁盘上打开一个源图片或者从内存中打开一个源图片Open the source image from disk or memory using the Open or OpenBinary methods, respectively.
- 通过width和height两个属性来设置想要的缩略图的宽度和长度,如果要保持长宽比,则原尺寸比率能够通过OriginalHeight以及OriginalWidth属性来获得。Set the desired width and height of the thumbnail via the Width and Height properties. To preserve aspect ratio, the original dimensions can be retrieved via the OriginalWidth and OriginalHeight properties.
- 可以任意先用save、Binary、SendBinary三种方法将生成的缩略图保存到磁盘上、内存里,或者保存为HTTP数据流Save the resultant thumbnail to disk, memory, or an HTTP stream by calling Save, Binary, or SendBinary, respectively.
以下样例代码从磁盘上打开一个图片,然后生成一个缩略图,宽度被限度为一个特值(本例中为100象素),保持原长宽比:The following code sample opens a disk image and creates a thumbnail with the width hard-coded to a particular value (100 pixels in this sample) while preserving the original aspect ratio:
<% ' Create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Open source image
Jpeg.Open "c:\path\myimage.jpg"
' New width
L = 100
' Resize, preserve aspect ratio
Jpeg.Width = L
Jpeg.Height = Jpeg.OriginalHeight * L / Jpeg.OriginalWidth
' create thumbnail and save it to disk
Jpeg.Save "c:\path\thumbnail.jpg"
%>
在以上样例中,我们通过原高以及缩放比计算得出新的高度,缩放比则是由新的宽度和原宽度相除得来。In the sample above, we calculate the new height as a product of the original height and a scaling factor which, in turn, is computed as a ratio of the new width to the original width.
如果我们必须把缩略图置入一个给定的长方形,长或者宽的最大值是设定好了的,以下代码可能是有用的:If we were to inscribe the thumbnail into a given rectangle, i.e. set the longest dimension to a particular value, the following code could be used:
...
If jpeg.OriginalWidth > jpeg.OriginalHeight Then
jpeg.Width = L
jpeg.Height = jpeg.OriginalHeight * L / jpeg.OriginalWidth
Else
jpeg.Height = L
jpeg.Width = jpeg.OriginalWidth * L / jpeg.OriginalHeight
End If
...
2.2 将缩略图直接发送给浏览器Sending Thumbnails Directly to the Browser
如果被用在IIS/ASP环境中,JspJpeg能够不经在服务器上生成临时文件而直接把生成的缩略图发送给浏览器,这是通过SendBinary方法获得的。这种方法与Save方法相似,但是它用内部调用ASP的Pesponse.BinaryWrite方法把图片发送给前端浏览器,代替了把缩略图保存到磁盘里。 When used in an IIS/ASP environment, AspJpeg is capable of sending a resultant thumbnail directly to the browser without creating a temporary file on the server. This is achieved via the method SendBinary. This method is similar to Save but instead of saving a thumbnail to disk, it internally makes calls to ASP's Response.BinaryWrite method, thereby sending the image to the client browser.
SendBinary方法必须从一个不包含任何HTML标签的独立脚本文件中被调用。必须通过IMG标签的src属性引用这个脚本。The SendBinary method must be called from a separate script file which should contain no HTML tags whatsoever. This script should be invoked via the SRC attribute of an <IMG> tag.
这个名为02_manythumbs.asp的样例包含了若干个引用了02_sendbinary.asp和临时文件路径以及尺寸参数的IMG标签The code sample 02_manythumbs.asp contains several <IMG> tags invoking the script 02_sendbinary.asp and passing file path and size parameters to it, as follows:
<IMG SRC="02_sendbinary.asp?path=<% = Path %>&width=300">
下面的是一些ASP/VB脚本以及ASP.NET/C#版本的02_sendbinary.asp脚本,注意缩略图的宽度来自request.querystring("width"),而高度则由这个指定的宽度按比例算得。Below are the ASP/VB Script and ASP.NET/C# versions of the 02_sendbinary.asp script. Notice that the width of the thumbnail is passed via the query string variable Width, and the height is set to be proportional to the specified width.
再次说明:如果脚本中调用了Jpeg.SendBinary的话,它就不能够包含任何HTML标签,否则它生成的数据流会被破坏掉。Once again, a script calling Jpeg.SendBinary must not contain any HTML tags, or the data stream it generates will be corrupted.
在.NET中,你必须用<%@ Page aspCompat="True" %>这句指令来使SendBinary方法正常工作。Under .NET, you must use the directive <%@ Page aspCompat="True" %> for the SendBinary method to work properly.
VB Script:
<%
Response.Expires = 0
' create instance of AspJpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Open source file
Jpeg.Open( Request("path") )
' Set new height and width
Jpeg.Width = Request("Width")
Jpeg.Height = Jpeg.OriginalHeight * Jpeg.Width / Jpeg.OriginalWidth
' Perform resizing and
' send resultant image to client browser
Jpeg.SendBinary
%>
C#
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="ASPJPEGLib" %>
<%@ Page aspCompat="True" %>
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
// this script may not contain any HTML tags, not even comments
ASPJPEGLib.IASPJpeg objJpeg;
objJpeg = new ASPJPEGLib.ASPJpeg();
// Open source image
objJpeg.Open( Request["path"] );
// Set new width
objJpeg.Width = int.Parse(Request["width"]);
// Preserve aspect ratio
objJpeg.Height = objJpeg.OriginalHeight * objJpeg.Width / objJpeg.OriginalWidth;
// Send thumbnail data to client browser
objJpeg.SendBinary(Missing.Value);
}
</script>
点击下例链接来运行这些样例代码:Click the links below to run this code sample:
http://localhost/aspjpeg/manual_02/02_manythumbs.asp
http://localhost/aspjpeg/manual_02/02_manythumbs.aspx




上一篇
下一篇


文章来自:
Tags: 





