您的位置:寻梦网首页编程乐园ASP编程>把网页中的电话号码生成图片的ASP程序
ASP世界
如何用ASP将一组数字转换为彩色图片
作者:小龙  来源:追寻梦想

(本文由小龙根据“ASP 生成彩色可变长验证码程序”改编)
作用:用ASP程序将数字生成图片格式:

可用于将电话号码图片输出或将统计数字用图片输出,使用免费空间时,有些空间强加广告代码以致无法用JS调用统计代码,则这是一个替代方法:

<script language=javascript>
document.write('<a href=http://www.XXX.com/mystatspath/ndex.asp><img src=http://www.XXX.com/mystatspath/stat.asp?style=img&referer='+escape(document.referrer)+'&screenwidth='+(screen.width)+' alt="XX统计"></a>')
</script>


好啦,书归正卷,下面开始正式代码
调用代码: 在你需要输出图片的地方加入这段代码就行。
<%
Dim str
str=987654321888   ''(str 为你要转换为图片的数字串,支持1-25位)
Get_img(str)
%>

程序代码:建立 Function Get_img(str) 和 Function Get_ForeColor()

<%
Response.Expires = 0
Response.AddHeader "Pragma","no-cache"
Response.AddHeader "cache-ctrol","no-cache"
Response.ContentType = "Image/BMP"
Randomize Timer


Function Get_img(str)
Text_Len = len(str)

Dim vColorData(1)
vColorData(0) = ChrB(0) & ChrB(0) & ChrB(211) ' 蓝0,绿0,红0(黑色)
vColorData(1) = ChrB(255) & ChrB(255) & ChrB(255) ' 蓝250,绿236,红211(浅蓝色)

Dim Text_Data(9),Text_Len,Int_Temp(),I,j,k,Int_Temp2


''**********************

ReDim Int_Temp( Text_Len - 1 )
Text_Data(0) = "00000000000001111000001100110000110111000011011100001100110000111011000011101100001100110000011110000000000000"
Text_Data(1) = "00000000000000011000000011100000111110000000011000000001100000000110000000011000000001100000000110000000000000"
Text_Data(2) = "00000000000001111000001100110000110011000000001100000001100000001100000001100000001100000000111111000000000000"
Text_Data(3) = "00000000000001111000001100110000110011000000001100000011100000000011000011001100001100110000011110000000000000"
Text_Data(4) = "00000000000001100000000110000000011011000001101100000110110000110011000011111110000000110000000011000000000000"
Text_Data(5) = "00000000000011111100001100000000110000000011000000001111100000000011000000001100000001100000111100000000000000"
Text_Data(6) = "00000000000000111000000011000000011000000011111000001100110000110011000011001100001100110000011110000000000000"
Text_Data(7) = "00000000000011111100000000110000000110000000011000000011000000001100000001100000000110000000011000000000000000"
Text_Data(8) = "00000000000001111000001100110000110011000011101100000111100000110111000011001100001100110000011110000000000000"
Text_Data(9) = "00000000000001111000001100110000110011000011001100001100110000011111000000011000000011000000011100000000000000"

''下面生成各位数码
For I = 0 To Text_Len - 1
Int_Temp(I) = instr(1,"0123456789",mid(str,i+1,1),1)-1
Next

''下面输出文件头部分
Int_Temp2 = (Text_Len - 1) \ 4 * 220 + ((Text_Len - 1) / 4 - (Text_Len - 1) \ 4 * 4) * 44
Response.BinaryWrite ChrB(&H42) & ChrB(&H4D)
Response.BinaryWrite ChrB(((Int_Temp2 + 206) / 256 - (Int_Temp2 + 206) \ 256) * 256) & ChrB((Int_Temp2 + 206) \ 256) ''特殊位
Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&H76) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&H28) & ChrB(0) & ChrB(0) & ChrB(0)
Response.BinaryWrite ChrB(Text_Len * 10) ''特殊位
Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&HB) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&H1) & ChrB(0) & ChrB(&H4) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0)
Response.BinaryWrite ChrB(((Int_Temp2 + 88) / 256 - (Int_Temp2 + 88) \ 256) * 256) & ChrB((Int_Temp2 + 88) \ 256) ''特殊位

Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(16) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(16) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&H80) & ChrB(0) & ChrB(0) & ChrB(&H80) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&H80) & ChrB(&H80) & ChrB(0) & ChrB(&H80) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&H80) & ChrB(0) & ChrB(&H80) & ChrB(0)
Response.BinaryWrite ChrB(&H80) & ChrB(&H80) & ChrB(0) & ChrB(0) & ChrB(&H80) & ChrB(&H80) & ChrB(&H80) & ChrB(0) & ChrB(&HC0) & ChrB(&HC0) & ChrB(&HC0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&HFF) & ChrB(0) & ChrB(0) & ChrB(&HFF) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&HFF) & ChrB(&HFF) & ChrB(0) & ChrB(&HFF) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(&HFF) & ChrB(0) & ChrB(&HFF) & ChrB(0) & ChrB(&HFF) & ChrB(&HFF) & ChrB(0) & ChrB(0) & ChrB(&HFF) & ChrB(&HFF) & ChrB(&HFF) & ChrB(0)

''下面输出图片数据
For i = 10 to 0 Step -1
  For j = 0 to Text_Len - 1
    For k = 1 to 9 Step 2
      If Mid(Text_Data(Int_Temp(j)) , i * 10 + k , 1) = "0" Then Int_Temp2 = 15 * 16 Else Int_Temp2 = Get_ForeColor() * 16
      If Mid(Text_Data(Int_Temp(j)) , i * 10 + k + 1 , 1) = "0" Then Int_Temp2 = Int_Temp2 + 15 Else Int_Temp2 = Int_Temp2 + Get_ForeColor()
      Response.BinaryWrite ChrB(Int_Temp2)
    Next
  Next
  Int_Temp2 = (Text_Len / 4 - Text_Len \ 4) * 4
  Select Case Int_Temp2
  Case 1
    Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(0)
  Case 2
    Response.BinaryWrite ChrB(0) & ChrB(0)
  Case 3
    Response.BinaryWrite ChrB(0)
  End Select
Next
End Function

Function Get_ForeColor()
  ''得到一个前景色
  Get_ForeColor = CInt(Mid("00021209",Int(Rnd * 4) * 2 + 1,2))
End Function

%>

你可能会问,那10串神秘的 “00011100....”的数字是什么东西?
其实并不神秘,你将那串数字拆成10个一行,共10行,就知道了,下面就是演示, 我把字符“1”加了红色背景,更醒目些。程序就是根据这些数字来输出图片的, 你也可以自制字母和其他符号的字符串,用它们将字母和符号也用图片输出,当然,你需要改一下代码,例如,你加一个减号(“-”)(输出电话号码时有用):

Text_Data(10) = "00000000000000000000000000000000000000000000000000001111110000000000000000000000000000000000000000000000000000"

你必须找到下面这行,在“0123456789”的后面加上一个减号(“-”)
Int_Temp(I) = instr(1,"0123456789",mid(str,i+1,1),1)-1
改为:
Int_Temp(I) = instr(1,"0123456789-",mid(str,i+1,1),1)-1
以此类推,按顺序一一对应。

 

0000000000
000111000
0011001100
0011011100
0011011100
0011001100
0011101100
0011101100
0011001100
0001111000
0000000000
0000000000
0000011000
0000111000
0011111000
0000011000
0000011000
0000011000
0000011000
0000011000
0000011000
0000000000
0000000000
0001111000
0011001100
0011001100
0000001100
0000011000
0000110000
0001100000
0011000000
0011111100
0000000000
0000000000
0001111000
0011001100
0011001100
0000001100
0000111000
0000001100
0011001100
0011001100
0001111000
0000000000
0000000000
0001100000
0001100000
0001101100
0001101100
0001101100
0011001100
0011111110
0000001100
0000001100
0000000000
0000000000
0011111100
0011000000
0011000000
0011000000
0011111000
0000001100
0000001100
0000011000
0011110000
0000000000
0000000000
0000111000
0000110000
0001100000
0011111000
0011001100
0011001100
0011001100
0011001100
0001111000
0000000000
0000000000
0011111100
0000001100
0000011000
0000011000
0000110000
0000110000
0001100000
0001100000
0001100000
0000000000
0000000000
0001111000
0011001100
0011001100
0011101100
0001111000
0011011100
0011001100
0011001100
0001111000
0000000000
0000000000
0001111000
0011001100
0011001100
0011001100
0011001100
0001111100
0000011000
0000110000
0001110000
0000000000