欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

FCK在线编辑器上传图片加入水印功能

程序员文章站 2024-02-06 13:06:10
在线编辑器fckeditor,适应多种开发语言环境,功能强大免费开源,能根据自己要求扩展功能。大家可以到官方网站下载最新源代码。 利用编辑器在图片上传时,加入水印功能。   如何在自己的网...

在线编辑器fckeditor,适应多种开发语言环境,功能强大免费开源,能根据自己要求扩展功能。大家可以到官方网站下载最新源代码。

利用编辑器在图片上传时,加入水印功能。

 

如何在自己的网站中架设fckeditor编辑器?我就不说了,本文默认你已经架设过并且熟悉fckeditor内部结构。
在下载fckeditor编辑器的同时,如果是使用.net版本,还必须下载一个源代码包,在里面有一些功能类,和编译出来的dll文件,存放在bin文件中,我们所需要做的就是修改源代码,重新编译源代码,生成新的dll,在自己的网站中替换就可以了。
我使用的是(源代码版本号是fckeditor.net_2.5,编辑器文件版本号为fckeditor_2.6.3)
用vs2008打开代码包根目录下的fredck.fckeditorv2.csproj文件,待文件树展开后,找到filebrowser文件夹下的fileworkerbase.cs文件,对其进行修改。
我们需要的是修改fileworkerbase类中的fileupload方法函数。
在看代码,先做好准备工作。在自己的网站中建立了watermark.config文件,用于存放网站的一些配置信息,如水印的类型(文字型,图片型),是否需要加水印,文字型水印的文字内容等等和本文无关的重要配置信息。所以在如下带代码中,有一段是用来读取这些配置信息的。
在fileupload方法中找到ofile.saveas( sfilepath );语句。在其后加入
 

try
{
     dataset configds = new dataset();
     configds.readxml(server.mappath("~/config/watermark.config"));
     datatable configdt = configds.tables[0];
     if (configdt.rows[0]["watermarkstatus"].tostring() == "1")
     {
         image img = image.fromfile(sfilepath);
         if (configdt.rows[0]["watermarktype"].tostring() == "0")
         {
             graphics g = graphics.fromimage(img);
             g.drawimage(img, 0, 0, img.width, img.height);
             font f = new font("华文行楷", 40);
             brush b = new solidbrush(color.white);
             string addtext = configdt.rows[0]["watermarktext"].tostring();
             g.drawstring(addtext, f, b, img.width - 174, img.height - 40);
             g.dispose();
         }
         if (configdt.rows[0]["watermarktype"].tostring() == "1")
         {
             system.drawing.image copyimage = system.drawing.image.fromfile(server.mappath("~/watermark/watermark.gif"));
             graphics g = graphics.fromimage(img);
             g.drawimage(copyimage, new rectangle(img.width - copyimage.width, img.height - copyimage.height, copyimage.width, copyimage.height), 0, 0, copyimage.width, copyimage.height, graphicsunit.pixel);
             g.dispose();
         }
         sfilename = system.io.path.getfilenamewithoutextension(ofile.filename);
           string newpath = sfilename+"_"+datetime.now.tostring("yymmddhhmmss") + "." + sextension;
           newpath = system.io.path.combine(sserverdir, newpath);
           sfilename = newpath;
           img.save(newpath);
           img.dispose();
          if (file.exists(sfilepath))
          {
               file.delete(sfilepath);
          }
       }
     }
catch
{
      this.sendfileuploadresponse(808, isquickupload);
}
 

 

 

请注意,一定要在自己的站点根目录下新建config文件夹,将watermark.config存放其中,
 watermark.config中必须出现的几个字段如下:
  <watermarkstatus>0</watermarkstatus><!--0:默认加;1:不加-->
  <watermarktype>1</watermarktype><!--0:文字型水印;1:图片型水印-->
  <watermarktext>abcd</watermarktext>
  <watermarkpic>watermark.gif</watermarkpic>

 

 


整个工程包下载如下: