将 .NET 缩略图功能添加到经典 ASP 多文件上传图像库






4.48/5 (9投票s)
一篇文章的第二部分,描述了一个基于 ASP 的多图像上传工具。我们添加了一个 .NET 缩略图生成器,并摆脱了图像处理的 DLL 注册限制。
引言
在我的原始文章中,我解释了创建图像异步上传机制的方法,以及一个功能齐全的图像库管理器,它使用 SimpleViewer
(一个灵活的免费 Flash 图像库)显示库。虽然功能完善,但原始解决方案有一个严重的限制,使其难以广泛使用:它依赖于一个 DLL 进行图像处理。这阻止了该解决方案在大多数托管解决方案上的部署,因为大多数主机不允许您注册 DLL,除非您拥有虚拟或专用服务器。
本文档解释了如何使用 .NET 移除该限制。大多数托管提供商允许您在他们的 Windows Server 包中使用 .NET 和经典 ASP。因此,此解决方案应该更“托管友好”。我设置的缩略图生成器可以在 thumbnail.aspx 中找到。
变更
我将重点介绍我对原始代码所做的更改,因此这部分不会像原始文章那样冗长。
对 .NET 的调用非常简单易于修改。让我们简要地看一下原始流程。
在原始代码中,上传模块处理文件上传和缩略图生成。为了分离缩略图生成,在流程中添加两个新的步骤。一个调用 .NET 缩略图生成器,另一个读取返回的 JSON 并做出反应。修改后的流程如下
代码
阅读第一篇文章(“经典 ASP 多文件上传图像库”)以了解上传工具的完整功能。我将重点介绍您需要进行的更改。
对 upload_files.js 的更改
在原始代码中,每次上传图像时,IFrame
都会调用父页面(顶部)上的函数 uploadDone2()
,并将返回的 JSON 作为参数传递。我将旧的 uploadDone2()
重命名为 thumbnailDone()
,并创建了一个新的 uploadDone2
,如下所示
// File is uploaded, read JSON and use it to call the thumbnailer
function uploadDone2(ret) {
var data = eval("("+ret+")"); // Read the return values from the iframe
if (!data.failure) {
var url="";
// Assemble URL to make thumbnail.aspx call
url += "thumbnail.aspx";
url += "?gallery_id=" + gallery_id;
url += "&size=" + data.size;
url += "&file=" + data.file_name;
//now set our IFrame target to the thumbnail page
$("upload_target").src = url;
}
else if (data.failure) { //Upload failed - show user the reason.
alert("Upload Failed: " + data.failure);
$("uploaded_image0" + upload_counter).src = "nosign.gif";
handleForm();
}
}
就是这样!我们的 Thumbnail.aspx 页面已经编码为在完成时调用 thumbnailDone()
。以下是代码的样子
<script type='text/javascript'>
function init() {
if ( top.thumbnailDone )
top.thumbnailDone( document.getElementsByTagName("body")[0].innerHTML );
}
window.onload=init;
</script>
以下是完整的 thumbnail.aspx 页面代码
<%@ Page Language="vb" Debug="false" Trace="false" %>
<%@ import Namespace="System.Data" %>
<%@ Import Namespace="Microsoft.VisualBasic" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
function HandleThumbs() as string
Dim galleryid As String = Request.QueryString("gallery_id")
Dim f As String = Request.QueryString("file")
Dim path As String = Server.MapPath(".") & "\images\" & galleryid & "\"
Dim filepath As String = path & f
Dim thumbroot As String = path & "thumb\"
Dim lSize As Long = 0
Dim Q as string = chr(34)
Dim s as string
'' old calls
''Call ThumbNail(strFileName, 32, 32, 1, 80)
''Call ThumbNail(strFileName, 64, 64, 1, 80)
''Call Thumbnail(strFileName, 96, 96, 1, 80)
''Call ThumbNail(strFileName, 120, 120, 1, 80)
''Call ThumbNail(strFileName, 240, 240, 1, 80)
''Call ThumbNail(strFileName, 480, 480, 1, 80)
''Call ThumbNail(strFileName, 640, 640, 1, 80)
''Call ThumbNail(strFileName, 800, 800, 1, 80)
''Call ThumbNail(strFileName, 960, 960, 1, 80)
If System.IO.File.Exists(filepath) Then
'' Generate a bunch of thumbnails, edit to suit your needs
lSize = 32
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 48
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 64
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 70
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 96
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 120
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 240
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 480
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 640
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 760
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
lSize = 960
SavePhoto(filepath, thumbroot & f & "." & lSize & "x" & lSize & ".jpg", lSize)
s = ""
s = s & "{ "
s = s & Q & "message" & Q & ":" & Q & "Thumbnails Complete!" & Q & ", "
s = s & Q & "failure" & Q & ": false, "
s = s & Q & "file_name" & Q & ":" & Q & request.querystring("file") & Q & ", "
s = s & Q & "size" & Q & ":" & Q & request.querystring("size") & Q & ", "
s = s & Q & "title" & Q & ":" & Q & "" & Q & ", "
s = s & Q & "description" & Q & ":" & Q & "" & Q & ", "
s = s & Q & "complete" & Q & ":" & Q & "yes" & Q & " "
s = s & "}"
Else
s = ""
s = s & "{ "
s = s & Q & "message" & Q & ":" & Q & "Path does not exist!" & Q & ", "
s = s & Q & "failure" & Q & ": true, "
s = s & Q & "file_name" & Q & ":" & Q & request.querystring("file") & Q & ", "
s = s & Q & "size" & Q & ":" & Q & request.querystring("size") & Q & ", "
s = s & Q & "title" & Q & ":" & Q & "" & Q & ", "
s = s & Q & "description" & Q & ":" & Q & "" & Q & ", "
s = s & Q & "complete" & Q & ":" & Q & "yes" & Q & " "
s = s & "}"
End If
HandleThumbs = s
End Function
'' Function lifted from
'' CM Image Helper Class, creates high quality images
'' Source: http://www.aboutmydot.net/index.php/high-quality-thumbnails-vbnet
Public Function SavePhoto(ByVal src As String, _
ByVal dest As String, ByVal w As Integer) As Boolean
Dim imgTmp As System.Drawing.Image
Dim sf As Double
Dim imgFoto As System.Drawing.Bitmap
imgTmp = System.Drawing.Image.FromFile(src)
If (imgTmp.Width > w) Then
sf = imgTmp.Width / w
imgFoto = New System.Drawing.Bitmap(w, CInt(imgTmp.Height / sf))
Dim recDest As New System.Drawing.Rectangle(0, 0, w, imgFoto.Height)
Dim gphCrop As System.Drawing.Graphics = _
System.Drawing.Graphics.FromImage(imgFoto)
gphCrop.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
gphCrop.CompositingQuality = _
System.Drawing.Drawing2D.CompositingQuality.HighQuality
gphCrop.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
gphCrop.DrawImage(imgTmp, recDest, 0, 0, imgTmp.Width,
imgTmp.Height, System.Drawing.GraphicsUnit.Pixel)
Else
imgFoto = imgTmp
End If
'Dim myImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo
Dim myEncoder As System.Drawing.Imaging.Encoder
Dim myEncoderParameter As System.Drawing.Imaging.EncoderParameter
Dim myEncoderParameters As System.Drawing.Imaging.EncoderParameters
Dim arrayICI() As System.Drawing.Imaging.ImageCodecInfo =
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
Dim jpegICI As System.Drawing.Imaging.ImageCodecInfo = Nothing
Dim x As Integer = 0
For x = 0 To arrayICI.Length - 1
If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
jpegICI = arrayICI(x)
Exit For
End If
Next
myEncoder = System.Drawing.Imaging.Encoder.Quality
myEncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
myEncoderParameter = New System.Drawing.Imaging.EncoderParameter(myEncoder, 60L)
myEncoderParameters.Param(0) = myEncoderParameter
imgFoto.Save(dest, jpegICI, myEncoderParameters)
imgFoto.Dispose()
imgTmp.Dispose()
Return True
End Function
</script>
<html>
<head>
<script type='text/javascript'>
function init() {
if ( top.thumbnailDone )
top.thumbnailDone( document.getElementsByTagName("body")[0].innerHTML );
}
window.onload=init;
</script>
</head>
<body id="body">
<%
if request.querystring("gallery_id")&""<>"" and request.querystring("file")<>"" then
response.write( HandleThumbs() )
else
response.write( "{ 'message':'Gallery and File are required',
'file':'', 'failure':true }" )
end if
%>
</body>
</html>
请花时间对本文进行投票和评论。谢谢。
版本历史
- 2010/07/19 gallery3.zip - 原始文件
- 2014/05/09 gallery3.a.zip - 修复了库信息编辑器中的一个错误