ASP requires components to add watermarks to pictures. Commonly used software include aspjpeg software and wsImage software developed by the Chinese. You can search and download these two software online. It is recommended to use wsImage developed by the Chinese. After all, it is a Chinese version and is easy to operate. A SP requires components to add watermarks to pictures... Commonly used software include aspjpeg software and wsImage software developed by the Chinese. You can search and download these two software online. It is recommended to use wsImage developed by the Chinese. After all, it is the Chinese version. Easy to operate.
How to register a component :
Just enter regsvr32 [Dll path] at the command prompt.
Adding a watermark to an image is nothing more than getting the size of the image and then writing the watermark on it. The ASP code only serves as a control component. Let’s use the code to explain everything.
1: Get the image size (here expressed in pixel values. Friends who learn PhotoShop should understand)
Copy the code code as follows:
<%
set obj=server.CreateObject(wsImage.Resize) ''Call component
obj.LoadSoucePic server.mappath(25.jpg) ''Open the picture, the picture name is 25.jpg
obj.GetSourceInfo iWidth,iHeight
response.write image width: & iWidth & <br> ''Get image width
response.write picture height: & iHeight & <br> ''Get the picture height
strError=obj.errorinfo
if strError<> then
response.write obj.errorinfo
end if
obj.free
set obj=nothing
%>
''------------------------------------------------ ----------------''
2: Add text watermark
Copy the code code as follows:
<%
set obj=server.CreateObject(wsImage.Resize)
obj.LoadSoucePic server.mappath(25.jpg) ''Load pictures
obj.Quality=75
obj.TxtMarkFont = Chinese Caiyun''Set the watermark text font
obj.TxtMarkBond = false ''Set the thickness of the watermark text
obj.MarkRotate = 0 ''Rotation angle of watermark text
obj.TxtMarkHeight = 25 ''Height of watermark text
obj.AddTxtMark server.mappath(txtMark.jpg), take you out of the country, &H00FF00&, 10, 70
strError=obj.errorinfo ''Generate the picture name, the text color is the position of the watermark in the picture
if strError<> then
response.write obj.errorinfo
end if
obj.free
set obj=nothing
%>
''------------------------------------------------ ----------------''
Three: Add image watermark
Copy the code code as follows:
<%
set obj=server.CreateObject(wsImage.Resize)
obj.LoadSoucePic server.mappath(25.jpg) ''Load pictures
obj.LoadImgMarkPic server.mappath(blend.bmp) ''Load watermark picture
obj.Quality=75
obj.AddImgMark server.mappath(imgMark.jpg), 315, 220,&hFFFFFF, 70
strError=obj.errorinfo ''Generate the picture name, the text color is the position of the watermark in the picture
if strError<> then
response.write obj.errorinfo
end if
obj.free
set obj=nothing
%>
''------------------------------------------------ ----------------''
In fact, adding watermarks to pictures is as simple as that. Then I will talk about the other two main uses of the WsImage.dll component. They include:
Crop pictures and generate thumbnails of pictures.
I'm still used to using code and adding comments:
Crop image:
Copy the code code as follows:
<%
set obj=server.CreateObject(wsImage.Resize)
obj.LoadSoucePic server.mappath(25.jpg)
obj.Quality=75
obj.cropImage server.mappath(25_crop.jpg),100,10,200,200 ''Define crop size and generate image name
strError=obj.errorinfo
if strError<> then
response.write obj.errorinfo
end if
obj.free
set obj=nothing
%>
Detailed note: The CropImage method of WsImage is used to crop the image. It is defined that when generating the image, 100 and 10 are the cropping points in the upper left corner, that is, 100 pixels from the left side of the image and 10 pixels from the top. The last two 200 represent cropping. of broadband and high and height.
''------------------------------------------------ ----------------''
Generate image thumbnails :
Copy the code code as follows:
<%
set obj=server.CreateObject(wsImage.Resize)
obj.LoadSoucePic server.mappath(25.jpg) ''Load pictures
obj.Quality=75
obj.OutputSpic server.mappath(25_s.jpg),0.5,0.5,3 ''Define the name of the thumbnail, which is the size
strError=obj.errorinfo
if strError<> then
response.write obj.errorinfo
end if
obj.free
set obj=nothing
%>
Detailed description:
There are four export methods for generating thumbnails :
(1) obj.OutputSpic server.mappath(25_s.jpg),200,150,0
200 is the output width and 150 is the output height. This output form forces the output width and height, which may cause image deformation.
(2) obj.OutputSpic server.mappath(25_s.jpg),200,0,1
Taking 200 as the output width, the output height will scale with the column.
(3) obj.OutputSpic server.mappath(25_s.jpg),0,200,2
Taking 200 as the output height, the output width will scale with the column.
(4) obj.OutputSpic server.mappath(25_s.jpg),0.5,0.5,3
The first 0.5 means that the generated thumbnail is half the width of the original image, which means the width is reduced.
The second 0.5 means that the generated thumbnail is half the height of the original image, which means a high reduction ratio.
The same reduction ratio of width and height means that the original image will be scaled down. If the scaling ratio of width and height is greater than 1, the original image will be enlarged.
2------------------------------------------------- -----------------------------------------------
Copy the code code as follows:
<%
Dim stream1,stream2,istart,iend,filename
istart=1
vbEnter=Chr(13)&Chr(10)
function getvalue(fstr,foro,paths)'fstr is the received name, foro Boolean false is file upload, true is a normal field, path is the upload file storage path
if foro then
getvalue=
istart=instring(istart,fstr)
istart=istart+len(fstr)+5
iend=instring(istart,vbenter+--------------------------------)
if istart>5+len(fstr) then
getvalue=substring(istart,iend-istart)
else
getvalue=
end if
else
istart=instring(istart,fstr)
istart=istart+len(fstr)+13
iend=instring(istart,vbenter)-1
filename=substring(istart,iend-istart)
filename9=right(getfilename(filename),4)'Get the original file suffix
filename8=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&int(9*10^3*rnd)+10 ^3'Get a random file name,
'If you want to lengthen the file name, please modify the value of 100 in (100*rnd)
filename=replace(getfilename(filename),getfilename(filename),filename8) 'Replace the original file name and use the replace function
filename=filename&filename9 'Add file suffix, the rule is to add the original file suffix to the generated random file name
istart=instring(iend,vbenter+vbenter)+3
iend=instring(istart,vbenter+--------------------------------)
filestart=istart
filesize=iend-istart-1
objstream.position=filestart
Set sf = Server.CreateObject(ADODB.Stream)
sf.Mode=3
sf.Type=1
sf.Open
objstream.copyto sf,FileSize
if filename<> then
Set rf = Server.CreateObject(Scripting.FileSystemObject)
i=0
fn=filename
while rf.FileExists(server.mappath(paths+fn))
fn=cstr(i)+filename
i=i+1
wend
filename=fn
sf.SaveToFile server.mappath(paths+filename),2
'''''''''''''''''''''''''''''''''''''''''' '
Dim Jpeg
Set Jpeg = Server.CreateObject(Persits.Jpeg)
If -2147221005=Err then
Response.write There is no such component, please install it! 'Check whether the AspJpeg component is installed
Response.End()
End If
Jpeg.Open (server.mappath(paths+filename)) 'Open the picture
If err.number then
Response.write failed to open the image, please check the path!
Response.End()
End if
Dim aa
aa=Jpeg.Binary 'Assign original data to aa
'==========Add text watermark==================
Jpeg.Canvas.Font.Color = &Hff0000 'Watermark text color
Jpeg.Canvas.Font.Family = Arial' font
Jpeg.Canvas.Font.Bold = True 'Whether to bold
Jpeg.Canvas.Font.Size = 30' font size
Jpeg.Canvas.Font.ShadowColor = &H000000 'Shadow color
Jpeg.Canvas.Font.ShadowYOffset = 1
Jpeg.Canvas.Font.ShadowXOffset = 1
Jpeg.Canvas.Brush.Solid = True
Jpeg.Canvas.Font.Quality = 4 'Output quality
Jpeg.Canvas.PrintText Jpeg.OriginalWidth/2-100,Jpeg.OriginalHeight/2+20,www.my9933.com 'Watermark position and text
bb=Jpeg.Binary 'Assign the value after text watermark processing to bb. At this time, the text watermark has no opacity
'============Adjust text transparency================
Set MyJpeg = Server.CreateObject(Persits.Jpeg)
MyJpeg.OpenBinary aa
Set Logo = Server.CreateObject(Persits.Jpeg)
Logo.OpenBinarybb
MyJpeg.DrawImage 0,0, Logo, 0.2 '0.3 is transparency
cc=MyJpeg.Binary 'Assign the final result to cc. At this time, the target image can also be generated.
response.BinaryWrite cc 'Output binary to the browser
MyJpeg.Save (server.mappath(paths+filename))
set aa=nothing
setbb=nothing
setcc=nothing
Jpeg.close
MyJpeg.Close
Logo.Close
'''''''''''''''''''''''''''''''''''''''''' '''
end if
getvalue=filename
end if
end function
Function subString(theStart,theLen)
dimi,c,stemp
objStream.Position=theStart-1
stemp=
for i=1 to theLen
if objStream.EOS then Exit for
c=ascB(objStream.Read(1))
If c > 127 Then
if objStream.EOS then Exit for
stemp=stemp&Chr(AscW(ChrB(AscB(objStream.Read(1)))&ChrB(c)))
i=i+1
else
stemp=stemp&Chr(c)
End If
Next
subString=stemp
End function
Function inString(theStart,varStr)
dim i,j,bt,theLen,str
InString=0
Str=toByte(varStr)
theLen=LenB(Str)
for i=theStart to objStream.Size-theLen
if i>objstream.size then exit Function
objstream.Position=i-1
if AscB(objstream.Read(1))=AscB(midB(Str,1)) then
InString=i
for j=2 to theLen
if objstream.EOS then
inString=0
Exit for
end if
if AscB(objstream.Read(1))<>AscB(MidB(Str,j,1)) then
InString=0
Exit For
end if
next
if InString<>0 then Exit Function
end if
next
End Function
Private function GetFileName(FullPath)
If FullPath <> Then
GetFileName = mid(FullPath,InStrRev(FullPath, /)+1)
Else
GetFileName =
End If
End function
function toByte(Str)
dim i,iCode,c,iLow,iHigh
toByte=
For i=1 To Len(Str)
c=mid(Str,i,1)
iCode =Asc(c)
If iCode<0 Then iCode = iCode + 65535
If iCode>255 Then
iLow = Left(Hex(Asc(c)),2)
iHigh =Right(Hex(Asc(c)),2)
toByte = toByte & chrB(&H&iLow) & chrB(&H&iHigh)
Else
toByte = toByte & chrB(AscB(c))
End If
Next
End function
%>
3------------------------------------------------- -----------------------------------------------
Use asp component Persits.Jpeg to add watermark to pictures and generate thumbnails
Copy the code code as follows:
<%
FileName=1.jpg
Set Jpeg = Server.CreateObject(Persits.Jpeg)
' Get the source image path
Path = Server.MapPath(FileName)
'Open the source image
'response.write(Path)
Jpeg.Open Path
'Set the details of generating thumbnails. There are many setting methods. The following method is to first determine the aspect ratio and then scale it proportionally.
If Jpeg.OriginalWidth / Jpeg.OriginalHeight > 1 then
Jpeg.Width = 98
Jpeg.Height = int((98/Jpeg.OriginalWidth)*Jpeg.OriginalHeight)
elseif Jpeg.OriginalWidth / Jpeg.OriginalHeight < 1 then
Jpeg.Width = 98
Jpeg.Height= int((98/Jpeg.OriginalWidth)*Jpeg.Height)
end if
'Set sharpening effect
Jpeg.Sharpen 1, 130
' Generate thumbnails to the specified path
Response.Write Server.MapPath(.)
Jpeg.Save Server.MapPath(.)&/small/&filename
'response.write filename1
'response.write Server.MapPath(uploadpic/small)&/&filename1
' Pay attention to these two Sessions
'Session(PPP0)=GP_curPath&FileName
'Session(PPP1)=GP_curPath&small&FileName
Set Jpeg = Nothing
'Automatically generate thumbnail image and end
'Start watermarking large pictures
'Create an instance
Set Jpeg = Server.CreateObject(Persits.Jpeg)
'Open the target image
Path = Server.MapPath(FileName)
'Open the source image
Jpeg.Open Path
'Add text watermark
Jpeg.Canvas.Font.Color = &HFF0000' Red
Jpeg.Canvas.Font.Family = Song Dynasty
Jpeg.Canvas.Font.Bold = True
Jpeg.Canvas.Print 10, 10, Honglan Technology
'Save file
Jpeg.Save Server.MapPath(.)&/small/w_&filename
'Unregister the object
Set Jpeg = Nothing
'Large picture watermarking ends
%>
4------------------------------------------------- -----------------------------------------------
Use ASPJPEG to build watermarked ASP implementation code
Copy the code code as follows:
<%
Class qswhImg
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)
'qiushuiwuhen (2002-8-12)
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)
'qiushuiwuhen (2002-8-12)
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)
'qiushuiwuhen (2002-8-12)
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)
'qiushuiwuhen (2002-8-12)
dim ret
ret = 0
for i = 1 to lenb(bin)
ret = ret *256 + ascb(midb(bin,i,1))
next
BinVal2=ret
End Function
Function getImageSize(filespec)
'qiushuiwuhen (2002-9-3)
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
SavefullPath=326151745wldn.jpg 'Picture path assignment or picture path variable assignment
'Get the width of the image
Set qswh = new qswhImg
arr = qswh.getImageSize(Server.Mappath(SavefullPath))
Set qswh = Nothing
str_ImgWidth=arr(1)
str_ImgHeight=arr(2)
If Int(str_ImgWidth) > 600 Then
str_ImgWidth = 600
Else
str_ImgWidth = str_ImgWidth
End If
'Add watermark
If Int(str_ImgWidth) > 300 And Int(str_ImgHeight) > 100 Then
LocalFile=Server.MapPath(SavefullPath)
TargetFile=Server.MapPath(SavefullPath)
DimJpeg
Set Jpeg = Server.CreateObject(Persits.Jpeg)
If -2147221005=Err then
Response.Write(<script language='javascript'>alert('This component does not exist, please install it!'); history.back();</script>) 'Check whether the AspJpeg component is installed
Response.End()
End If
Jpeg.Open (LocalFile) 'Open the picture
If err.number then
Response.Write(<script language='javascript'>alert('Failed to open the image, please check the path!'); history.back();</script>)
Response.End()
End if
Dim aa
aa=Jpeg.Binary 'Assign original data to aa
'==========Add text watermark==================
Jpeg.Canvas.Font.Color = &Hfffffff 'Watermark text color
Jpeg.Canvas.Font.Family = Arial 'Font
Jpeg.Canvas.Font.Bold = True 'Whether to bold
Jpeg.Canvas.Font.Size = 20 'Font size
Jpeg.Canvas.Font.ShadowColor = &H000000 'Shadow color
Jpeg.Canvas.Font.ShadowYOffset = 1
Jpeg.Canvas.Font.ShadowXOffset = 1
Jpeg.Canvas.Brush.Solid = True
Jpeg.Canvas.Font.Quality = 10 ' 'Output quality
Jpeg.Canvas.PrintText Jpeg.OriginalWidth/2-40,Jpeg.OriginalHeight/2-10, website construction' watermark position and text
bb=Jpeg.Binary 'Assign the value after text watermark processing to bb. At this time, the text watermark has no opacity
'============Adjust text transparency================
Set MyJpeg = Server.CreateObject(Persits.Jpeg)
MyJpeg.OpenBinary aa
Set Logo = Server.CreateObject(Persits.Jpeg)
Logo.OpenBinarybb
MyJpeg.DrawImage 0,0, Logo, 0.5 '0.3 is transparency
cc=MyJpeg.Binary 'Assign the final result to cc. At this time, the target image can also be generated.
Response.BinaryWrite cc 'Output binary to the browser
MyJpeg.Save (TargetFile)
set aa = nothing
setbb=nothing
setcc=nothing
Jpeg.Close
MyJpeg.Close
Logo.Close
End If
'Add watermark
%>