教程版权:三元社区 - 阿强
适用版本:LeoBBS X 系列
修改文件:setstyles.cgi、attachment.cgi
教程作用:增加图片水印功能
效果参考: http://www.lzeweb.com/cgi-bin/city/topic.cgi?forum=33&topic=946 http://www.lzeweb.com/cgi-bin/city/photo.cgi?action=pic&id=1124808566.jpg_4
一、打开 setstyles.cgi,找到:
复制内容到剪贴板
代码:
<tr>
<td bgcolor=#FFFFFF colspan=2>
<font color=#333333>上传的 JPG 图片上水印的文字<BR>注:<font color=red>不能用中文</font>,也不要过长,否则影响效果</font><BR><BR></td>
<td bgcolor=#FFFFFF>
<input type=text name="watername" value="$watername" size=30></td>
</tr>在它上面加上:
复制内容到剪贴板
代码:
<!-- 3plugs 水印图片 -->
<tr>
<td bgcolor=#FFFFFF colspan=2>
<font color=#333333>图片水印名称<BR>注:水印图必须为 PNG 格式,如果留空则使用文字水印<BR><BR></td>
<td bgcolor=#FFFFFF>
<input type=text name="waterpic" value="$waterpic" size=20> 例: waterlogo.png,图片保存在 $imagesurl 路径</td>
</tr>
<!-- end -->二、打开 attachment.cgi,找到:
复制内容到剪贴板
代码:
$watername = "http://bbs.leobbs.com/" if ($watername eq "");
my ($imwidth, $imheight) = $image->getBounds ();
my $font = GD::gdLargeFont();
my $fontwidth = $font->width * length $watername;
my $background = $image->colorAllocate(0,0,0);
my $txt1 = $image->colorAllocate(255,255,255);
my $txt2 = $image->colorAllocate(0,0,0);
if ($imheight > 40 && $imwidth > 200) { # 小于 200*40 的图片不加水印
if ($picwaterplace1 eq "yes") { # 左上角
$image->string($font, 10, 11, $watername, $txt2);
$image->string($font, 11, 11, $watername, $txt2);
$image->string($font, 9, 10, $watername, $txt1);
$image->string($font, 10, 10, $watername, $txt1);
}
if ($picwaterplace2 eq "yes") { # 左下角
$image->string($font, 10, $imheight - 20 + 1, $watername, $txt2);
$image->string($font, 11, $imheight - 20 + 1, $watername, $txt2);
$image->string($font, 9, $imheight - 20, $watername, $txt1);
$image->string($font, 10, $imheight - 20, $watername, $txt1);
}
if ($picwaterplace3 eq "yes") { # 右上角
$image->string($font, $imwidth - $fontwidth - 9 + 1, 11, $watername, $txt2);
$image->string($font, $imwidth - $fontwidth - 10 + 1, 11, $watername, $txt2);
$image->string($font, $imwidth - $fontwidth - 9, 10, $watername, $txt1);
$image->string($font, $imwidth - $fontwidth - 10, 10, $watername, $txt1);
}
if ($picwaterplace4 eq "yes") { # 右下角
$image->string($font, $imwidth - $fontwidth - 9 + 1, $imheight - 20 + 1, $watername, $txt2);
$image->string($font, $imwidth - $fontwidth - 10 + 1, $imheight - 20 + 1, $watername, $txt2);
$image->string($font, $imwidth - $fontwidth - 9, $imheight - 20, $watername, $txt1);
$image->string($font, $imwidth - $fontwidth - 10, $imheight - 20, $watername, $txt1);
}
}修改为:
复制内容到剪贴板
代码:
if($waterpic eq "") {
$watername = "http://bbs.leobbs.com/" if ($watername eq "");
my ($imwidth, $imheight) = $image->getBounds ();
my $font = GD::gdLargeFont();
my $fontwidth = $font->width * length $watername;
my $background = $image->colorAllocate(0,0,0);
my $txt1 = $image->colorAllocate(255,255,255);
my $txt2 = $image->colorAllocate(0,0,0);
if ($imheight > 40 && $imwidth > 200) { # 小于 200*40 的图片不加水印
if ($picwaterplace1 eq "yes") { # 左上角
$image->string($font, 10, 11, $watername, $txt2);
$image->string($font, 11, 11, $watername, $txt2);
$image->string($font, 9, 10, $watername, $txt1);
$image->string($font, 10, 10, $watername, $txt1);
}
if ($picwaterplace2 eq "yes") { # 左下角
$image->string($font, 10, $imheight - 20 + 1, $watername, $txt2);
$image->string($font, 11, $imheight - 20 + 1, $watername, $txt2);
$image->string($font, 9, $imheight - 20, $watername, $txt1);
$image->string($font, 10, $imheight - 20, $watername, $txt1);
}
if ($picwaterplace3 eq "yes") { # 右上角
$image->string($font, $imwidth - $fontwidth - 9 + 1, 11, $watername, $txt2);
$image->string($font, $imwidth - $fontwidth - 10 + 1, 11, $watername, $txt2);
$image->string($font, $imwidth - $fontwidth - 9, 10, $watername, $txt1);
$image->string($font, $imwidth - $fontwidth - 10, 10, $watername, $txt1);
}
if ($picwaterplace4 eq "yes") { # 右下角
$image->string($font, $imwidth - $fontwidth - 9 + 1, $imheight - 20 + 1, $watername, $txt2);
$image->string($font, $imwidth - $fontwidth - 10 + 1, $imheight - 20 + 1, $watername, $txt2);
$image->string($font, $imwidth - $fontwidth - 9, $imheight - 20, $watername, $txt1);
$image->string($font, $imwidth - $fontwidth - 10, $imheight - 20, $watername, $txt1);
}
}
} else {
$image1 = GD::Image->newFromPng("${imagesdir}$waterpic", 1);
my ($width,$height) = $image1->getBounds ();# 得到水印logo图尺寸
$image1->transparent ($image1->colorAllocate (0,0,0));# 得出黑色以及设logo的黑色为透明
my ($imwidth, $imheight) = $image->getBounds ();# 得到主图的尺寸
my ($zwidth, $zheight);
# 如果主图宽大于logo宽2倍,高大于logo高2倍,则加水印
if (((int ($imwidth / 2)) > $width) && ((int ($imheight / 2)) > $height))
{
if ($picwaterplace1 eq ';yes'; || $picwaterplace3 eq ';yes';)
{
$zheight = 10;
} else {
$zheight = int ($imheight - $height - 10);
}
if ($picwaterplace1 eq ';yes'; || $picwaterplace2 eq ';yes';)
{
$zwidth = 10;
} else {
$zwidth = int ($imwidth - $width - 10);
}
$image->copy ($image1, $zwidth, $zheight, 0, 0, $width, $height);
}
}三、完成。其它的加上传人名字、水印透明度等效果自行参考修改。
四、本站图片水印LOGO: http://www.lzeweb.com/city/waterlogo.png