No component generation thumbnail _asp base

Source: Internet
Author: User
Tags abs chr
There are many ASP components that generate thumbnails on the web. If your virtual space does not support registering new components, you may feel your site is a bit tarnished. The heart is clear, combined with online resources to write a component-free thumbnail program, for reference only.

Let's look at the basic section first. First, we know that displaying the picture in the page is the following code:



SRC is the picture path, border control the edge of the picture, width is the length of the picture, height is the picture. The production of thumbnails is actually scaled on the original size. But generally in order to minimize distortion, we will scale proportionally. Thus, getting the long and wide dimensions of a picture is the key to generating thumbnails.

The following steps are to write a component-free build thumbnail:

1. No component to get picture size

Just learned the ASP before soon saw an article using ASCII code to achieve the size of the picture without components. Later tried, found in the acquisition of JPG image size is always not correct display, check the Internet, there are many sites reproduced this program, but no one pointed out the defects of this program, but also not to solve the defects of the method. Then Google again, and finally found an introduction to the use of ADODB.stream to get picture size of the article, according to its introduction method, modify the code inside tried to try, the effect is really good, now take it out to share with you:

Using ADODB.stream to get the general class of picture size <%
'////////////gps:get picture Size//////////////
'//////////////use ADODB.stream to get picture size//////////////
'/////////cited by Leon (Heart fine) 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) <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":
Todo
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 generic class without component to get the picture size is OK.



2. Get Picture path

Since more than one picture, as well as the picture needs to be sorted, we have designed a field imgurl that holds the relative path of the picture in the database. We put the uploaded pictures in a folder called images (as for how to upload pictures without components, the heart is not much to say). Now we first design a showimg.asp page to display thumbnails and related information. The specific design is as follows:

Image:

Picture format:

Picture size:

Picture size:

Number of clicks:


Below, we get the absolute path of the picture. The code is as follows:

<%
'/////gets showimg.asp's absolute path/////
Dim Curfile
Curfile=server.mappath (Request.ServerVariables ("Path_info"))

Dim Curfilename,filename

'/////picture relative path (stored in the database)
Cufilename=rs ("Imgurl")
'/////because showimg.asp and images are in the same directory, so we use InStrRev to get the images path/////
Filename=left (Curfile,instrrev (curfile, "\")) &cufilename

'/////set up a GPS class entity/////
Dim getpicsize
Set getpicsize=new GPS

Set fs=server.createobject ("Scripting.FileSystemObject")
'/////get Picture type/////
Dim Picsuffixname
Picsuffixname=fs. Getextensionname (filename)

Dim PD '//picture Dimension
Dim Pwidth,pheight

Select Case Picsuffixname
Case "GIF", "BMP", "JPG", "PNG":
'/////calls the GetImageSize function in the GPS generic class to get the picture size/////
Pd=getpicsize.getimagesize (filename)
PWIDTH=PD (1) '//Get picture width
PHEIGHT=PD (2) '//Get picture 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 the top of the <body> OK!

Of course, some people will say, get the path does not need to use path_info, directly with Server.MapPath () It is OK, oh, radish greens, the main is that I use path_info can achieve some of the functions of FSO with Server.MapPath () Not done, so keep using this.

3. Define Thumbnail size
This part of the code is the beholder, the benevolent. First, we need to specify the size range of the thumbnail display, such as: 300x260, the code can write 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
%>
Write the above code immediately after the second step. The code for the call is as follows:
border= "0" width=<%=pxwidth%> height=<%=pxheight%>>
As for the picture format can be used <%=PicSuffixName%>, picture size can be written
<%
Response.Write pxwidth& "X" &pxheight
%>
Picture size can be implemented with fso.getfilesize (filename), and the number of clicks can be implemented simply with the SQL statement, the specific code is no longer tired.

In this way, a component-free thumbnail program is written, it may be a bit copycat, but as long as we can master the method of trust is still a great improvement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.