When displaying products at the front of the website, thumbnails are generally used. Click to enter and then see the large image.
Thumbnails bring two troubles:
1. If only one large image is transmitted to the background, the width and height of the large image will only be fixed when displaying the thumbnail. This will not only cause the thumbnail to be deformed, but also slow down the page access speed.
2. If the background uploads two pictures every time, one large picture and one thumbnail. In this case, there is no problem in 1, but it will cause a lot of trouble to the backend staff. Because the backend staff does not necessarily know how to process and generate thumbnails; even if they know and can process it quickly, some time is wasted.
The following code can help you use the AspJpeg component to actually generate thumbnails according to the width and height ratio.
AspJpeg component download: http://www.aspjpeg.com/download.html
AspJpeg component usage: http://www.mydw.cn/tech/1/766.html
Registration code: 48958-77556-02411
<%
Dim sOriginalPath
sOriginalPath = "images/1.gif"
'The original image path is generally obtained after uploading, or
Dim is obtained from the database sReturnInfo, sSmallPath 'The function returns information, thumbnail path
sReturnInfo = BuildSmallPic(sOriginalPath, "images", 100, 100)
Response.Write "Return information:" & sReturnInfo & "<br/>"
If InStr(sReturnInfo, "Error_") <= 0 Then
sSmallPath = sReturnInfo 'The return information is
'Write sSmallPath to the database
'
Else
Response.Write "Detailed error:"
Select Case sReturnInfo
Case "Error_01"
Response.Write "<font color='red'>Failed to create the AspJpeg component, the component was not installed and registered correctly</font>" & "<br/>"
Case "Error_02"
Response.Write "<font color='red'>The original image does not exist, check the value passed in the s_OriginalPath parameter</font>" & "<br/>"
Case "Error_03"
Response.Write "<font color='red'>Thumbnail save failed. Possible reasons: the base address for saving the thumbnail does not exist, check the value passed in the s_OriginalPath parameter; no write permission to the directory; insufficient disk space</font>" & "<br/>"
Case "Error_Other"
Response.Write "<font color='red'>Unknown error</font>" & "<br/>"
End Select
Response.End
End If
%>
Original file name: <%=sOriginalPath%><br/>
Thumbnail file name: <%=sSmallPath%><br/>
Original image: <img src='<%=sOriginalPath%>' border=0><br/><br/>
Thumbnail: <img src='<%=sSmallPath%>' border=0>
<%
'================================================== ===============
'Author:laifangsong QQ:25313644
'Function: Generate thumbnails based on specified pictures
'Note: The "paths" mentioned below are all relative paths relative to the file that calls this function.
'parameter:
' s_OriginalPath: Original image path example: images/image1.gif
' s_BuildBasePath: The base path for generating images, regardless of whether it ends with "/", for example: images or images/
' n_MaxWidth: Generate the maximum width of the image
' If the thumbnail displayed in the foreground is 100*100, here n_MaxWidth=100, n_MaxHeight=100.
' n_MaxHeight: Generate the maximum height of the image
'Return value:
'Return the path of the generated thumbnail image
'Error handling:
' If an error occurs during function execution, an error code will be returned. The error code starts with "Error"
' Error_01: Failed to create AspJpeg component, the component was not correctly installed and registered.
' Error_02: The original image does not exist, check the value passed in the s_OriginalPath parameter
' Error_03: Thumbnail saving failed. Possible reasons: The thumbnail saving base address does not exist, check the value passed in the s_OriginalPath parameter; no write permission to the directory; insufficient disk space
' Error_Other: Unknown error
'Calling example:
'Dim sSmallPath 'Thumbnail path
' sSmallPath = BuildSmallPic("images/image1.gif", "images", 100, 100)
'================================================== ===============
Function BuildSmallPic(s_OriginalPath, s_BuildBasePath, n_MaxWidth, n_MaxHeight)
Err.Clear
On Error Resume Next
'Check whether the component has been registered
DimAspJpeg
Set AspJpeg = Server.Createobject("Persits.Jpeg")
If Err.Number <> 0 Then
Err.Clear
BuildSmallPic = "Error_01"
Exit Function
End If
'Check whether the original image exists
Dims_MapOriginalPath
s_MapOriginalPath = Server.MapPath(s_OriginalPath)
AspJpeg.Open s_MapOriginalPath 'Open the original picture
If Err.Number <> 0 Then
Err.Clear
BuildSmallPic = "Error_02"
Exit Function
End If
'Get the thumbnail width and height proportionally
Dim n_OriginalWidth, n_OriginalHeight 'Width and height of the original image
Dim n_BuildWidth, n_BuildHeight 'Thumbnail width, height
Dim div1, div2
Dim n1, n2
n_OriginalWidth = AspJpeg.Width
n_OriginalHeight = AspJpeg.Height
div1 = n_OriginalWidth / n_OriginalHeight
div2 = n_OriginalHeight / n_OriginalWidth
n1 = 0
n2 = 0
If n_OriginalWidth > n_MaxWidth Then
n1 = n_OriginalWidth / n_MaxWidth
Else
n_BuildWidth = n_OriginalWidth
End If
If n_OriginalHeight > n_MaxHeight Then
n2 = n_OriginalHeight / n_MaxHeight
Else
n_BuildHeight = n_OriginalHeight
End If
If n1 <> 0 Or n2 <> 0 Then
If n1 > n2 Then
n_BuildWidth = n_MaxWidth
n_BuildHeight = n_MaxWidth * div2
Else
n_BuildWidth = n_MaxHeight * div1
n_BuildHeight = n_MaxHeight
End If
End If
'Specify width and height to generate
AspJpeg.Width = n_BuildWidth
AspJpeg.Height = n_BuildHeight
'--Start saving thumbnails--
Dim pos, s_OriginalFileName, s_OriginalFileExt 'Position, original file name, original file extension
pos = InStrRev(s_OriginalPath, "/") + 1
s_OriginalFileName = Mid(s_OriginalPath, pos)
pos = InStrRev(s_OriginalFileName, ".")
s_OriginalFileExt = Mid(s_OriginalFileName, pos)
Dim s_MapBuildBasePath, s_MapBuildPath, s_BuildFileName 'Thumbnail absolute path, thumbnail file name
Dim s_EndFlag 'Small image file name ending tag example: If the large image file name is "image1.gif" and the ending tag is "_small", then the small image file name is "image1_small.gif"
If Right(s_BuildBasePath, 1) <> "/" Then s_BuildBasePath = s_BuildBasePath & "/"
s_MapBuildBasePath = Server.MapPath(s_BuildBasePath)
s_EndFlag = "_small" 'Can be customized, as long as the size of the picture can be distinguished
s_BuildFileName = Replace(s_OriginalFileName, s_OriginalFileExt, "") & s_EndFlag & s_OriginalFileExt
s_MapBuildPath = s_MapBuildBasePath & "" & s_BuildFileName
AspJpeg.Save s_MapBuildPath 'Save
If Err.Number <> 0 Then
Err.Clear
BuildSmallPic = "Error_03"
Exit Function
End If
'--Save the thumbnail to disk--
'Log out the instance
Set AspJpeg = Nothing
If Err.Number <> 0 Then
BuildSmallPic = "Error_Other"
Err.Clear
End If
BuildSmallPic = s_BuildBasePath & s_BuildFileName
End Function
%>