There are many ASP components that can generate thumbnails on the Internet. If your virtual space does not support registering new components, you may feel that your website has lost its luster. Do you know the steps to generate thumbnails without components? Let’s learn about it with the editor of the Wrong New Technology Channel!
Generate thumbnails without components
Let's take a look at the basics first. First of all, we know that the following code is displayed on the page:
src is the image path, border controls the edge width of the image, width is the length of the image, and height is the height of the image. The generation of thumbnails is actually scaling on the original size. But generally, in order to minimize distortion, we will scale to scale. Therefore, obtaining the length and width of the picture becomes the focus of generating thumbnails.
Here are the steps to write a component-free thumbnail generation:
1. Get the image size without components
I saw an article using ASCII code to obtain image sizes without components when I first learned ASP. Later I tried it and found that when I got the size of jpg-like images, I checked it online and found that many websites reprinted this program, but none of them pointed out the flaws of this program, let alone the solution to the flaws. Later I went Googled and finally found an article introducing the use of ADODB.stream to obtain image sizes. I tried it by modifying the code inside according to the method introduced. The effect is really good. Now I will take it out and share it with you:
Use ADODB.stream to get the common class <% of image size
'////////////// GPS: Get Picture Size //////////////
'///////////////////////////////
'///////////Cited By Leon (Xinqing) August 11, 2005 ////////////////
Class GPS
Dim aso
Private Sub Class_Initialize
Set aso=CreateObject("Adodb.Stream")
aso.Mode=3
aso.Type=1
aso.Open
End Sub
Private Sub Class_Terminate
set aso=nothing
End Sub
Private Function Bin2Str(Bin)
Dim I, Str
For I=1 to LenB(Bin)
clow=MidB(Bin,I,1)
if AscB(clow)<128 then
Str = Str & Chr(ASCB(clow))
Else
I=I+1
if I <= LenB(Bin) then Str = Str & Chr(ASCW(MidB(Bin,I,1)&clow))
end If
Next
Bin2Str = Str
End Function
Private Function Num2Str(num, base,lens)
'GPS (2005-8-11)
dim ret
ret = ""
While(num>=base)
ret = (num mod base) & ret
num = (num - num mod base)/base
wend
Num2Str = right(string(lens,"0") & num & ret,lens)
End Function
Private Function Str2Num(str,base)
'GPS (2005-8-11)
dim ret
ret = 0
for i=1 to len(str)
ret = ret *base + cint(mid(str,i,1))
next
Str2Num=ret
End Function
Private Function BinVal(bin)
'GPS (2002-8-11)
dim ret
ret = 0
for i = lenb(bin) to 1 step -1
ret = ret *256 + ascb(midb(bin,i,1))
next
BinVal=ret
End Function
Private Function BinVal2(bin)
'GPS (2002-8-11)
dim ret
ret = 0
for i = 1 to lenb(bin)
ret = ret *256 + ascb(midb(bin,i,1))
next
BinVal2=ret
End Function
'///The following is the calling code///
Function getImageSize(filespec)
'GPS (2002-8-11)
dim ret(3)
aso.LoadFromFile(filespec)
bFlag=aso.read(3)
select case hex(binVal(bFlag))
case "4E5089":
aso.read(15)
ret(0)="PNG"
ret(1)=BinVal2(aso.read(2))
aso.read(2)
ret(2)=BinVal2(aso.read(2))
case "464947":
aso.read(3)
ret(0)="GIF"
ret(1)=BinVal(aso.read(2))
ret(2)=BinVal(aso.read(2))
case "535746":
aso.read(5)
binData=aso.Read(1)
sConv=Num2Str(ascb(binData),2,8)
nBits=Str2Num(left(sConv,5),2)
sConv=mid(sConv,6)
while(len(sConv)
sConv=sConv&Num2Str(ascb(binData),2,8)
wend
ret(0)="SWF"
ret(1)=int(abs(Str2Num(mid(sConv,1*nBits+1,nBits),2)-Str2Num(mid(sConv,0*nBits+1,nBits),2))/20)
ret(2)=int(abs(Str2Num(mid(sConv,3*nBits+1,nBits),2)-Str2Num(mid(sConv,2*nBits+1,nBits),2))/20)
case "FFD8FF":
do
do: p1=binVal(aso.Read(1)): loop while p1=255 and not aso.EOS
if p1>191 and p1<196 then exit do else aso.read(binval2(aso.Read(2))-2)
do:p1=binVal(aso.Read(1)):loop while p1<255 and not aso.EOS
loop while true
aso.Read(3)
ret(0)="JPG"
ret(2)=binval2(aso.Read(2))
ret(1)=binval2(aso.Read(2))
case else:
if left(Bin2Str(bFlag),2)="BM" then
aso.Read(15)
ret(0)="BMP"
ret(1)=binval(aso.Read(4))
ret(2)=binval(aso.Read(4))
else
ret(0)=""
end if
end select
ret(3)="width=""" & ret(1) &""" height=""" & ret(2) &""""""
getimagesize=ret
End Function
End Class
%>
Copy the above code to generate a GPS.asp file, so that the general class with no component to obtain the image size is OK.
2. Get the image path
Since more than one picture and the pictures need to be stored in a classified manner, we designed a field ImgURL that stores the relative path of the picture in the database. We put all the uploaded images in a folder called images (as for how to upload images without components, I won’t say much). Now let’s design a ShowImg.asp page to display thumbnails and related information. The specific design is as follows:
picture:
Image format:
Image size:
Image size:
Number of clicks:
Below, we get the absolute path to the image. The code is as follows:
<%
'///// Get the absolute path of ShowImg.asp /////
Dim curFile
curFile=Server.mappath(Request.servervariables("PATH_INFO"))
Dim curfilename,filename
'///// The relative path of the image (stored in the database)
cufilename=rs("ImgURL")
'///// Because ShowImg.asp is in the same directory as images, we use instrrev to get the path of images /////
filename=left(curFile,instrrev(curFile,"/"))&cufilename
'///// Create GPS entity/////
Dim GetPicSize
Set GetPicSize=new GPS
Set fs=Server.CreateObject("Scripting.FileSystemObject")
'///// Get the image type/////
Dim PicSuffixName
PicSuffixName=fs.GetExtensionName(filename)
Dim PD '//Picture Dimension
Dim PWidth, PHeight
Select Case PicSuffixName
Case "gif","bmp","jpg","png":
'///// Call the GetImageSize function in the GPS general class to get the image size /////
PD=GetPicSize.GetImageSize(filename)
PWidth=PD(1) '//Get picture width
PHeight=PD(2) '//Get image height
Case "swf"
PD=GetPicSize.GetImageSize(filename)
PWidth=PD(1) '//Get Flash width
PHeight=PD(2) '//Get Flash height
Case Else
End Select
Set fs=Nothing
Set GetPicSize=Nothing
%>
Copy the above code to *4)> >
The above is OK!
Of course, some people may say that you don’t have to use PATH_INFO to get the path. You can just use server.mappath() directly. Haha, everyone has their own preferences. The main reason is that I can use PATH_INFO to implement some functions of FSO, but I haven’t done it with server.mappath(), so I have been using this.
3. Define thumbnail size
This part of the code is different from one's own opinion. First of all, we need to specify the size range of thumbnail display, for example: 300X260, the code can be written like this:
<%
Dim PXWidth, PXHeight
Dim Pp '//Proportion
If PWidth=0 Or PWidth="" Then
PXWidth=0
PXHeight=0
Else
Pp=FormatNumber(PWidth/PHeight,2) '//Add-width ratio
End If
If PWidth>=PHeight Then
If PWidth>=300 Then
PXWidth=300
PXHeight=FormatNumber(300/Pp,0)
Else
PXWidth=PWidth
PXHeight=PHeight
End If
Else
If PHeight>=260 Then
PXHeight=260
PXWidth=FormatNumber(260*Pp,0)
Else
PXWidth=PWidth
PXHeight=PHeight
End If
End If
%>
Write the above code immediately after the second step. The code when calling is as follows:
border="0" width=<%=PXWidth%> height=<%=PXHeight%>>
As for the image format, you can use <%=PicSuffixName%>, and the image size can be written as
<%
response.write PXWidth&"X"&PXHeight
%>
The image size can be achieved using FSO.GetFileSize(filename), while the number of clicks can be achieved simply using SQL statements, so the specific encoding will no longer be described.
In this way, a component-free thumbnail program will be written, which may be a bit wise, but as long as you can master the method, it will be greatly improved. >
The above article is about the operation method of generating thumbnails without components. I believe everyone has a certain understanding. If you want to know more technical information, please continue to pay attention to the wrong new technology channel!