There are many ASP components on the Internet that generate thumbnails. If your virtual space does not support registering new components, you may feel that your website has lost its luster. I am not very talented, so I combined online resources to write a component-free thumbnail generation program for reference only.
Let’s take a look at the basics first. First, we know that the code to display images on the page is as follows:
<img src=pic.gif border=0 width=300 height=260>
src is the image path, border controls the image edge width, 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 proportionally. Therefore, obtaining the length and width dimensions of the image has become the focus of generating thumbnails.
Here are the steps to write component-free thumbnail generation:
1. Get image size without components
Not long ago when I first learned ASP, I saw an article that used ASCII code to obtain image size without components. Later, I tried it and found that the size of jpg pictures could not be displayed correctly. I checked online and found that many websites reprinted this program, but no one pointed out the flaws of this program, let alone solved it. The shortcomings are solved. Later, I googled again and finally found an article that introduced the use of ADODB.stream to obtain the image size. According to the method introduced, I modified the code inside and tried it. The effect is really good. Now I will share it with you. :
Use ADODB.stream to obtain the general class of image size
<%
'///////////// GPS: Get Picture Size ///////////////
'//////////////Use ADODB.stream to obtain image size///////////////
'/////////Cited By Leon (心清) 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))
endIf
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)<nBits*4)
binData=aso.Read(1)
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 the GPS.asp file, so that the general class for obtaining image size without components is OK.
2. Get the image path
Since there is more than one picture and the pictures need to be stored in categories, we designed a field ImgURL in the database to store the relative path of the picture. We put all the uploaded pictures in a folder called images (I won’t go into details about how to upload pictures without components). Now we first 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:
Next, we get the absolute path of 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
'/////Relative path to the picture (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 class entity/////
Dim GetPicSize
Set GetPicSize=new GPS
Set fs=Server.CreateObject(Scripting.FileSystemObject)
'/////Get the picture 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 obtain the image size/////
PD=GetPicSize.GetImageSize(filename)
PWidth=PD(1) '//Get the picture width
PHeight=PD(2) '//Get the picture height
Case swf
PD=GetPicSize.GetImageSize(filename)
PWidth=PD(1) '//Get Flash width
PHeight=PD(2) '//Get the Flash height
Case Else
End Select
Set fs=Nothing
Set GetPicSize=Nothing
%>
Copy the above code to the top of <body> and it’s OK!
Of course, some people will say that you don’t have to use PATH_INFO to obtain 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 realize some functions of FSO and use server. .mappath() didn't work, so I kept using this.
3. Define thumbnail size
This part of the code is in the eye of the beholder, and the wise see wisdom. First, we need to specify the thumbnail display size range, 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) '//Aspect 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
%>
Just write the above code immediately after the second step. The code when calling is as follows:
<img src=<%=curfilename%> border=0 width=<%=PXWidth%>
height=<%=PXHeight%>>
As for the picture format, you can use <%=PicSuffixName%> to get it, and the picture size can be written as
<%
response.write PXWidth&X&PXHeight
%>
The image size can be realized using FSO.GetFileSize(filename), and the number of clicks can be simply realized using SQL statements. The specific coding will not be described again.
In this way, a component-free thumbnail generation program has been written. It may be a bit borrowed, but as long as everyone can master the method, I believe it will be greatly improved.