ASP Resize Image and Insert to Database |
ASP Resize Image and Insert to Database เขียน ASP ในการ Upload ไฟล์ และทำการ Resize ก่อนเก็บลงใน Database ในรูปแบบของ Thumbnails โดยทำการแสดงไฟล์ที่เป็น Thumbnails เมื่อคลิกที่รูปก็จะทำการเปิดไฟล์ต้นฉบับ วิธีนี้ช่วยลดขนาดของการ Transfer Bandwidth ได้ด้วยครับ
ASP aspSmartUpload
Syntax
Server.CreateObject("OWC10.ChartSpace")
AspUploadResize1.html
<html>
<head>
<title>ThaiCreate.Com ASP & Upload Resize</title>
</head>
<body>
<form action="AspUploadResize2.asp" method="post" enctype="multipart/form-data" name="frmMain">
Upload
<input name="file1" type="file">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
AspUploadResize2.asp
<% Option Explicit %>
<html>
<head>
<title>ThaiCreate.Com ASP & Upload Resize</title>
</head>
<body>
<%
Dim mySmartUpload
Dim Conn,strSQL,objExec
'*** Create Object ***'
Set mySmartUpload = Server.CreateObject("aspSmartUpload.SmartUpload")
'*** Upload Files ***'
mySmartUpload.Upload
Dim sFileName,strPath
strPath = "MyResize"
sFileName = mySmartUpload.Files("file1").FileName
'*** Upload file1 ***'
If mySmartUpload.Files("file1").FileName <> "" Then
mySmartUpload.Files("file1").SaveAs(Server.MapPath(strPath & "/" & sFileName))
Response.write sFileName & " Resize Successful.<br>"
'*** Resize Images ***'
Dim Chs, objConst,NewWidth,NewHeight
Dim OutFormat,OutFileName
OutFormat = "Jpg" '*** Gif,Png ***'
OutFileName = "Thumbnails_"&sFileName
NewWidth = "100" '*** Set new Width , Height automatic caculate ***'
NewHeight = 0 '*** Auto Resize ***'
'*** Get Images Width & Height ***'
Dim objFso,myImg,Width,Height
Set objFso = Server.CreateObject("Scripting.FileSystemObject")
IF objFso.FileExists(Server.MapPath(strPath&"/"&sFileName)) Then
set myImg = Loadpicture(Server.MapPath(strPath&"/"&sFileName))
Width = Round(myImg.width / 26.4583)
Height = Round(myImg.height / 26.4583)
NewHeight = Round((NewWidth*Height)/Width)
'NewHeight = 100
'NewWidth = Round((NewHeight*Width)/Height) '*** or Automatic Height ***'
End If
'*************** End *************'
Set Chs = Server.CreateObject("OWC10.ChartSpace")
Set objConst = Chs.Constants
Chs.Interior.SetTextured Server.MapPath(strPath&"/"&sFileName), objConst.chStretchPlot, , objConst.chAllFaces
Chs.border.color = -3
'Chs.border.color = &H0000FF
'Chs.border.Weight = 3
Chs.ExportPicture Server.MapPath(strPath&"/"&OutFileName),OutFormat,NewWidth,NewHeight
Set objConst = Nothing
Set Chs = Nothing
'*** Insert Record ***'
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("mydatabase.mdb"),"" , ""
strSQL = ""
strSQL = strSQL &"INSERT INTO files "
strSQL = strSQL &"(Thumbnails,FilesName) VALUES ('"&OutFileName&"','"&sFileName&"')"
Set objExec = Conn.Execute(strSQL)
End If
Response.write("<a href=AspUploadResize3.asp>View files</a>")
Set mySmartUpload = Nothing
%>
</body>
</html>
AspUploadResize3.asp
<% Option Explicit %>
<html>
<head>
<title>ThaiCreate.Com ASP & Upload Resize</title>
</head>
<body>
<%
Dim Conn,strSQL,objRec
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("mydatabase.mdb"),"" , ""
strSQL = "SELECT * FROM files "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3
%>
<table width="200" border="1">
<tr>
<th width="50"> <div align="center">Files ID </div></th>
<th width="150"> <div align="center">Resize </div></th>
</tr>
<%
While Not objRec.EOF
%>
<tr>
<td><div align="center"><%=objRec.Fields("FilesID").Value%></div></td>
<td><center>
<a href="MyResize/<%=objRec.Fields("FilesName").Value%>">
<img src="MyResize/<%=objRec.Fields("Thumbnails").Value%>" border="0"></a></center>
</td>
</tr>
<%
objRec.MoveNext
Wend
%>
</table>
<%
objRec.Close()
Conn.Close()
Set objRec = Nothing
Set Conn = Nothing
%>
<br>
<a href="AspUploadResize1.html">New Upload</a>
</body>
</html>
Screenshot
|