K9 通用版 博客版 主题版 地方门户版 企业版 |   企业(ASP) 分类 使用 分享 疑问 模板 建议 帮助 错误 其他
+新建主题 [Ajax]
填写帐号密码即可完成注册
+新建话题 最新回复排序 最新主题排序 精华帖子

KingCMS5.1 上传图片与远程图片添加水印功能

1Next >

hkdeng 发表于:15-06-10 22:16 [添加收藏] 楼主 [回复] #Top#
hkdeng 人气:13 积分:19 金币:444
用了一段时间的kingcms,还有顾及到图片水印,现在注意到这里了,有些地方要完善一下,默认情况下kingcms支持远程抓图时水印,但是上传图片时却没有,下面代码可以实现上传时给图片加水印。


前台/system/config.asp文件 这段代码可以设置是否支持水印,前提是你的空间支持aspjpeg         www.hkdeng.com


'aspjpeg (true 支持;false不支持)
const king_isjpeg = true '服务器是否支持aspjpeg,Version 1.5
const king_regkey = "" '有些服务器有ASPJPEG组件,但不能使用时,就在这里输入可用的系列号就能正常使用
const king_watermark = true '是否打开水印功能 水印文件在 template/image/watermark.gif
const king_watermark_weight = 4 ' 水印的位置 0随机 1左上角 2右上角 3左下角 4右下角 5正中间
const king_watermark_alpha = 0.8 '水印的透明度


后台/Article/index.asp文件 sub king_edt()函数


if cstr(form("snapimg"))="1" then data(1,0)=king.snap(data(1,0)) '远程抓图


if len(artimg)>0 then
    if cstr(form("snapimg"))="1" or validate(artimg,5)=false then
     data(12,0)=artimg'如果已经被抓过图,则直接设置路径
    else'如果没有被抓过图,需要抓图;但考虑到覆盖源图,还是重命名一个图片
     data(12,0)=king.inst&king_upath&"/image/"&art.path&"/"&formatdate(date(),2)&"/"&int(timer()*100)&"."&king.extension(artimg)
     king.createfolder data(12,0)
     king.remote2local artimg,data(12,0)
    end if
   end if
  else
   if validate(data(12,0),7) and validate(data(12,0),5) then'如果是图片路径
    artimg=data(12,0)
    data(12,0)=king.inst&king_upath&"/image/"&art.path&"/"&formatdate(date(),2)&"/"&int(timer()*100)&"."&king.extension(data(12,0))
    king.createfolder data(12,0)
    king.remote2local artimg,data(12,0)
   end if
  end if


前台/system/fun.asp文件
public function snap(l1)
 dim objregexp,match,matches,arrimg,allimg,newimg,retstr
 dim i,fimagename,fname,filename,fext,k,arrnew,arrall,today,uppathfd
 set objregexp=new regexp
 objregexp.ignorecase=true
 objregexp.global=true
 objregexp.pattern="(<img([^>]*))( src=)([""'])(.*?)4(([^>]*)/?>)"
 set matches=objregexp.execute(l1)
  for each match in matches
   retstr=retstr&llllII(match.value)
  next
  arrimg=split(retstr,"||")
  allimg="":newimg=""
  today=formatdate(tnow,2)
  uppathfd="../../"&king_upath&"/image/"&path&"/"&today
  createfolder uppathfd'创建文件夹
  fname=timer()*100'定义一个随机图片文件名(无扩展名)
  for i=1 to ubound(arrimg)
   if arrimg(i)<>"" and instr(allimg,arrimg(i))<1 then
    filename=fname&i'文件名(无扩展名)
    fext=mid(arrimg(i),instrrev(arrimg(i),"."))
    fimagename=filename&fext'获得扩展名,加上一个i循环?这样容易产生重复文件.
    '判断是否存在同名的文件,如果没有就直接通过,如果有,就重命名
    while (isexist(uppathfd&"/"&fimagename))
     fimagename=filename&"_"&k&fext
     k=k+1
    wend
    remote2local arrimg(i),uppathfd&"/"&fimagename
    allimg=allimg&"||"&arrimg(i)
    newimg=newimg&"||"&inst&king_upath&"/image/"&path&"/"&today&"/"&fimagename
   end if
  next
  arrnew=split(newimg,"||")
  arrall=split(allimg,"||")
  for i=1 to ubound(arrnew)
   l1=replace(l1,arrall(i),arrnew(i))
  next
  snap=l1
 set matches=nothing
 set objregexp=nothing
end function


'remote2local  
public sub remote2local(l1,l2)
 on error resume next
 dim I1,I2,I3:I1=trim(l1):I3=gethtm(I1,1)
 set I2=server.createobject(king_stm)
  I2.type=1
  I2.open
  I2.write I3
  I2.savetofile server.mappath(l2),2
  I2.close()
 set I2=nothing
 watermark l2
end sub


'watermark  
public sub watermark(l1)
 on error resume next
 dim I1,I2,I3
 if king_watermark and isobj(king_jpeg) and isexist(l1) then
 else
  exit sub
 end if
 set I1=server.createobject(king_jpeg)'原始图
 if len(king_regkey)>0 then I1.regkey =king_regkey
 I1.open server.mappath(l1)
 set I2=server.createobject(king_jpeg)'水印图片
 if len(king_regkey)>0 then I2.regkey =king_regkey
 I2.open server.mappath("../../"&king_templates&"/images/watermark.gif")
 if king_watermark_weight=0 then
  randomize
  I3=(round((rnd*99)+1) mod 5)+1
 else
  I3=king_watermark_weight
 end if
 '水印
 if I1.width>I2.width and I1.height>I2.height then
  select case cstr(I3)
  case"1" I1.DrawImage 0, 0,I2,king_watermark_alpha,&HFFFFFF
  case"2" I1.DrawImage I1.width-I2.width, 0,I2,king_watermark_alpha,&HFFFFFF
  case"3" I1.DrawImage 0, I1.height-I2.height,I2,king_watermark_alpha,&HFFFFFF
  case"4" I1.DrawImage I1.width-I2.width, I1.height-I2.height,I2,king_watermark_alpha,&HFFFFFF
  case"5" I1.DrawImage (I1.width-I2.width)/2, (I1.height-I2.height)/2,I2,king_watermark_alpha,&HFFFFFF
  end select
  I1.save server.mappath(l1)
 end if
 set I1=nothing
 set I2=nothing
end sub


以上是kingcms5.1实现水印的函数调用主要代码


下面实现如何手工上传图片时也实现水印


后台/system/manage.asp文件 sub king_filemanage()函数


添加以下红色部分代码就行了
case"upfile"
  king.clearol
  king.ol="<meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />"
  king.ol="<style type=""text/css"">*{margin:0px;padding:0px;}</style>"
  if instr(lcase(request.servervariables("content_type")),"multipart/form-data") then
   upload.FileType=ftype
   upload.SavePath=""
   if right(path,1)<>"/" then path=path&"/"
   if upload.save("upfile",path&upload.form("upfile_name")) then'成功上传
    filecate= upload.form("upfile_name")
    filecate=king.filecate(LCase(right(filecate,(len(filecate)-instrrev(filecate,".")))))
    if filecate="img" then
     king.watermark(path&upload.form("upfile_name"))
    end if
    king.txt "<script>window.parent.posthtm('../system/manage.asp?action=filemanage','aja','path="&server.urlencode(path&upload.form("upfile_name"))&"&type="&ftype&"&formname="&formname&"');window.parent.display('flo');</script>"'king.lang("'上传成功'")
   else
c6281564 发表于:15-06-18 16:23 沙发 [回复] #Top#
c6281564 人气:8 积分:17 金币:156
代码太长了,看懂一部分了

1Next >

发表回复

帐号 匿名发布 审核后可见 [加载完整在线编辑器]
内容
验证码
KingCMS 内容管理系统

关于我们 联系我们 广告报价 付款方式 站点导航

Copyright © 2004-2015 Focuznet All rights reserved.

广州唯众网络科技有限公司 粤ICP备08008106号