Practical code for adding a watermark to the ASP wsImage component

Source: Internet
Author: User

Adding watermarks to Images Using ASP requires components... common applications include aspjpeg software and wsImage software developed by the Chinese people. You can search and download the two software online. We recommend that you use the wsImage developed by the Chinese people. After all, it is a Chinese version and easy to operate.

How to register Components:
Enter "regsvr32 [Dll path]" at the command prompt.
Adding a watermark to an image is nothing more than obtaining the image size, and then writing the watermark. ASP code is just a control component. Use code to describe everything.

I. Obtain the image size(This is represented by pixel values. All the friends who learn PhotoShop should understand)Copy codeThe Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize") ''call the component
Obj. LoadSoucePic server. mappath ("25.jpg") ''open the image. The image name is 25.jpg.
Obj. GetSourceInfo iWidth, iHeight
Response. write "Image Width:" & iWidth & "<br>" ''To get the Image Width
Response. write "Image Height:" & iHeight & "<br>" ''To get the Image Height
StrError = obj. errorinfo
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

''----------------------------------------------------------------''
Ii. Add a text watermark Copy codeThe Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize ")
Obj. LoadSoucePic server. mappath ("25.jpg")''
Obj. Quality = 75
Obj. TxtMarkFont = "文" ''sets the watermark text font
Obj. TxtMarkBond = false'' sets the watermark text width.
Obj. MarkRotate = 0'' watermark text Rotation Angle
Obj. TxtMarkHeight = 25'' height of the watermark text
Obj. AddTxtMark server. mappath ("txtMark.jpg"), "Take you away", & H00FF00 &, 10, 70
StrError = obj. errorinfo '': generate the image name. The text color is the position of the watermark in the image.
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

''----------------------------------------------------------------''
3. Add an image watermark Copy codeThe Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize ")
Obj. LoadSoucePic server. mappath ("25.jpg")''
Obj. LoadImgMarkPic server. mappath ("blend.bmp") ''loads the watermark image
Obj. Quality = 75
Obj. AddImgMark server. mappath ("imgMark.jpg"), 315,220, & hFFFFFF, 70
StrError = obj. errorinfo '': generate the image name. The text color is the position of the watermark in the image.
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

''----------------------------------------------------------------''
In fact, adding a watermark to an image is that simple. Then I will explain the other two main usage of the WsImage. dll component, including:
Crop an image to generate a thumbnail.
I still get used to it and add comments with the code to describe:
Crop images:Copy codeThe Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize ")
Obj. LoadSoucePic server. mappath ("25.jpg ")
Obj. Quality = 75
Obj. cropImage server. mappath ("25_crop.jpg"), 200,200, 10, ''defines the cut size and the name of the generated image
StrError = obj. errorinfo
If strError <> "" then
Response. write obj. errorinfo
End if
Obj. free
Set obj = nothing
%>

Note: The CropImage method of WsImage is used for image cropping. when an image is generated, 100 and 10 are the cut points in the upper left corner, that is, pixels on the left and 10 pixels on the top. the last two 200 represents the cut bandwidth and height.
''----------------------------------------------------------------''
Generating image thumbnails:Copy codeThe Code is as follows: <%
Set obj = server. CreateObject ("wsImage. Resize ")
Obj. LoadSoucePic server. mappath ("25.jpg")'' load the image
Obj. Quality = 75
Obj. OutputSpic server. mappath ("25_s.jpg"), 0.5, 0.5, 3 ''defines the size of the thumbnail.
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 is forced output width and height, which may cause image deformation.
(2) obj. OutputSpic server. mappath ("25_s.jpg"), 200,0, 1
The output width is 200, and the output height scales with the ratio column.
(3) obj. OutputSpic server. mappath ("25_s.jpg"), 0,200, 2
The output height is 200, and the output width scales with the ratio column.
(4) obj. OutputSpic server. mappath ("25_s.jpg"), 0.5, 0.5, 3
The first 0.5 indicates that the generated thumbnail is half the width of the source image, that is, the width reduction ratio.
The second 0.5 indicates that the generated thumbnail is half the height of the source image, that is, the scaled down ratio is high.
The scaled-down ratio of the width and height is the same, which means that the ratio of the source image is reduced. If the zoom ratio is greater than 1, the source image is enlarged.
2 cores ---------------------------------------------------------------------------------------Copy codeThe Code is as follows: <%
Dim stream1, stream2, istart, iend, filename
Istart = 1
VbEnter = Chr (13) & Chr (10)
Function getvalue (fstr, foro, paths) 'fstr is the name of the receiver, foro Boolean false is the file upload, true is a common field, and path is the storage path of the uploaded file
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 'random file name,
'If you want to lengthen the file name, modify the value of 100 in (100 * rnd ).
Filename = replace (getfilename (filename), getfilename (filename), filename8) 'replaces the original file name, using the replace Function
Filename = filename & filename9 'with the 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 "This component is not available. Please install it! "'Check whether the AspJpeg component is installed
Response. End ()
End If
Jpeg. Open (server. mappath (paths + filename) 'Open the image
If err. number then
Response. write "opening the image failed. Please check the path! "
Response. End ()
End if
Dim aa
Aa = Jpeg. Binary 'assigns original data to aa
'=========== Add a 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: The value after the text watermark is processed is assigned to bb. At this time, the text watermark has no opacity.
'============== Adjust the text transparency ========================
Set MyJpeg = Server. CreateObject ("Persits. Jpeg ")
MyJpeg. OpenBinary aa
Set Logo = Server. CreateObject ("Persits. Jpeg ")
Logo. OpenBinary bb
MyJpeg. DrawImage 0.2, Logo, '0. 3 is transparency
Cc = MyJpeg. Binary assign the final result to cc, and the target image can also be generated.
Response. BinaryWrite CC' will be output to the browser.
MyJpeg. Save (server. mappath (paths + filename ))
Set aa = nothing
Set bb = nothing
Set cc = nothing
Jpeg. close
MyJpeg. Close
Logo. Close
'''''''''''''''''''''''''''''''''''''''' '''''''''''''
End if
Getvalue = filename
End if
End function
Function subString (theStart, theLen)
Dim I, c, stemp
ObjStream. Position = theStart-1
Stemp = ""
For I = 1 to theLen
If objStream. EOS then Exit
C = ascB (objStream. Read (1 ))
If c & gt; 127 Then
If objStream. EOS then Exit
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
End if
If AscB (objstream. Read (1) <> AscB (MidB (Str, j, 1) then
InString = 0
Exit
End if
Next
If InString <> 0 then Exit Function
End if
Next
End Function
Private function GetFileName (FullPath)
If FullPath <> "" Then
GetFileName = mid (FullPath, limit Rev (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 Cores ---------------------------------------------------------------------------------------
Use the asp Component Persits. Jpeg to add a watermark to the image and generate a thumbnail. Copy codeThe Code is as follows: <%
FileName = "1.jpg"
Set Jpeg = Server. CreateObject ("Persits. Jpeg ")
'Obtain the source image path
Path = Server. MapPath (FileName)
'Open source images
'Response. write (Path)
Jpeg. Open Path
'Set the details of generating thumbnails. There are many ways to set them. The following method is to first judge the aspect ratio and then scale 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 the sharpening effect.
Jpeg. Sharpen 1,130
'Generate a thumbnail to the specified path
Response. Write Server. MapPath (".")
Jpeg. Save Server. MapPath (".") & "\ small \" & filename
'Response. write filename1
'Response. write Server. MapPath ("uploadpic/small") & "\" & filename1
'Note that these two sessions
'Session ("PPP0") = GP_curPath & FileName
'Session ("PPP1") = GP_curPath & "small" & FileName
Set Jpeg = Nothing
'Automatic generation of Scale-in graph ends
'Big image watermark starts
'Create an instance
Set Jpeg = Server. CreateObject ("Persits. Jpeg ")
'Open the Target Image
Path = Server. MapPath (FileName)
'Open source images
Jpeg. Open Path
'Add a text watermark
Jpeg. Canvas. Font. Color = & HFF0000 'red
Jpeg. Canvas. Font. Family = ""
Jpeg. Canvas. Font. Bold = True
Jpeg. Canvas. Print 10, 10, "macro blue technology"
'Save the file
Jpeg. Save Server. MapPath (".") & "\ small \ w _" & filename
'Logout object
Set Jpeg = Nothing
'Watermark the large image.
%>

4 Cores ---------------------------------------------------------------------------------------
Use ASPJPEG to construct and add a watermark ASP implementation code Copy codeThe Code is 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" 'image path assignment or image PATH variable assignment
'Get the Image Width
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_img width = 600
Else
Str_ImgWidth = str_ImgWidth
End If
'Watermarking
If Int (str_ImgWidth)> 300 And Int (str_ImgHeight)> 100 Then
LocalFile = Server. MapPath (SavefullPath)
TargetFile = Server. MapPath (SavefullPath)
Dim Jpeg
Set Jpeg = Server. CreateObject ("Persits. Jpeg ")
If-2147221005 = Err then
Response. Write ("<script language = 'javascript '> alert (' This component is not available, please install it! '); History. back (); </script> ")' check whether the AspJpeg component is installed.
Response. End ()
End If
Jpeg. Open (LocalFile) 'Open an image
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 'assigns original data to aa
'=========== Add a text watermark ========================
Jpeg. Canvas. Font. Color = & hffffff' 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: The value after the text watermark is processed is assigned to bb. At this time, the text watermark has no opacity.
'============== Adjust the text transparency ========================
Set MyJpeg = Server. CreateObject ("Persits. Jpeg ")
MyJpeg. OpenBinary aa
Set Logo = Server. CreateObject ("Persits. Jpeg ")
Logo. OpenBinary bb
MyJpeg. DrawImage 0.5, Logo, '0. 3 is transparency
Cc = MyJpeg. Binary assign the final result to cc, and the target image can also be generated.
Response. BinaryWrite CC' will be output to the browser.
MyJpeg. Save (TargetFile)
Set aa = nothing
Set bb = nothing
Set cc = nothing
Jpeg. Close
MyJpeg. Close
Logo. Close
End If
'Watermarking
%>

Related Article

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.