自制Thinkphp水印生成器

水印生成器1 水印生成器

首先是水印函数copy image.class.php 里面的一个函数,稍加修改:

[code lang=”php”]
static public function water_shanmao($source, $water, $savename=null, $alpha=80) {
//检查文件是否存在
if (!file_exists($source) || !file_exists($water))
return false;

//图片信息
$sInfo = self::getImageInfo($source);
$wInfo = self::getImageInfo($water);

//如果图片小于水印图片,不生成图片
if ($sInfo["width"] < $wInfo["width"] || $sInfo[‘height’] < $wInfo[‘height’]){
return ‘Img to small’;
exit;
}

//建立图像
$sCreateFun = "imagecreatefrom" . $sInfo[‘type’];
$sImage = $sCreateFun($source);
$wCreateFun = "imagecreatefrom" . $wInfo[‘type’];
$wImage = $wCreateFun($water);

//设定图像的混色模式
imagealphablending($wImage, true);

//图像位置,默认为右下角右对齐
$posY = ($sInfo["height"] – $wInfo["height"])-10;
$posX = ($sInfo["width"] – $wInfo["width"])-10;

//生成透明合并图像
imagecopy($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[‘width’], $wInfo[‘height’]);
//生成混合图像
//imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[‘width’], $wInfo[‘height’], $alpha);

//输出图像
$ImageFun = ‘Image’ . $sInfo[‘type’];
//如果没有给出保存文件名,默认为原图像名
if (!$savename) {
$savename = $source;
@unlink($source);
}
//保存图像
$ImageFun($sImage, $savename);
imagedestroy($sImage);
return ‘success’;
}
[/code]

上传和执行水印函数:

[code lang=”php”]
function shuiyin(){

$this->assign(‘title’,’水印生成器’);
//$this->display(‘pay/shopnew.html’);
$this->display(‘shanmao/shuiyin.html’);
}

function sy_shanmao(){//执行水印函数
import("ORG.Util.Image");
$im = new Image();
//$source = $_POST[‘img’];
$source = APP_PATH."/Tpl/default/avatar/shuiyin/".$_POST[‘img’];//一定要绝对路径,下同
$water = APP_PATH.’/Tpl/default/avatar/shuiyin/sy/cosshuiyin.png’;
$savename = APP_PATH."/Tpl/default/avatar/shuiyin/s/".$_POST[‘img’];
$re = $im->water_shanmao($source, $water, $savename, $alpha=80);//来源,水印源,生成路径,水印透明度
//dump($source);
$this->ajaxReturn($_POST[‘img’],$re);
}

function upload(){//上传
$this->checkLoginSession();
$uid = $_SESSION[‘userinfo’][‘id’];
$re = M(‘User’)->where("id = ‘$uid’")->find();
//dump($_SESSION[‘userinfo’]);
if($re[‘user_role’]<=3) exit;//权限
if (!empty($_FILES)) {
//如果有文件上传 上传附件
$imgname = $this->_upload();
//echo ‘<img src="/cityosweb/upload/shuiyin/cityofsteam_’.$imgname.’">’;
//$this->forward();
}
$this->assign("img",$imgname);
$this->assign(‘title’,’水印生成器’);
$this->display(‘shanmao/shuiyin.html’);
}

protected function _upload() {
import("ORG.Net.UploadFile");
//导入上传类
$upload = new UploadFile();
//设置上传文件大小
$upload->maxSize = 3292200;
//设置上传文件类型
$upload->allowExts = explode(‘,’, ‘png,jpg’);
//设置附件上传目录
$upload->savePath = APP_PATH.’/Tpl/default/avatar/shuiyin/’;
$upload->saveRule = uniqid;//设置上传文件规则
$upload->thumb = true ; //是否开启缩略图
$upload->thumbMaxWidth = "1000,800,500" ;//最大宽
$upload->thumbMaxHeight = "1000,800,500" ;//最大高
$upload->thumbPrefix =’b_,s_,m_’;//生成缩略图的前缀
$upload->thumbRemoveOrigin =true;//是否删除原图
if (!$upload->upload()) {
//捕获上传异常
$this->error($upload->getErrorMsg());
} else {
//取得成功上传的文件信息
$uploadList = $upload->getUploadFileInfo();
$url = $uploadList[0][‘savename’];
}

//dump($url);
//exit;
//$list = $model->add($data);
// dump($upload->getErrorMsg());

return $url;

}
[/code]

最后是模板文件:
点击上传生成后的图片,即可添加水印。

[code lang=”html”]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>水印生成器</title>
<script type="text/javascript" src="__TMPL__js/jquery/jquery.min.js"></script>
</head>
<body>
<div style="float:right"><a href="/cityosweb/shanmao/shuiyin" >New Upload</a></div>
<h3 style="text-align:center">水印生成器 <span style="vertical-align:super">beta</span></h3><br />

<hr /><br />
<br />

<{if $img}>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><a href="javascript:;" class="sy" pid="b_"><img src="/cityosweb/gameweb/Tpl/default/avatar/shuiyin/b_<{$img}>"></a></td>
<td><a href="javascript:;" class="sy" pid="s_"><img src="/cityosweb/gameweb/Tpl/default/avatar/shuiyin/s_<{$img}>"></a></td>
<td><a href="javascript:;" class="sy" pid="m_"><img src="/cityosweb/gameweb/Tpl/default/avatar/shuiyin/m_<{$img}>"></a></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>

<br />
<br />

<hr />

<{/if}>

<br />
<br />
<center>
<form id="upload" method=’post’ action="__URL__/upload/" enctype="multipart/form-data">
<table cellpadding=3 cellspacing=3 bgcolor="#999999">
<tr>
<td colspan="2" class="tLeft">
<div class="result" style="background:#E9E9F3"></div>
</td>
</tr>
<tr>
<td class="tRight tTop"></td>
<td class="tLeft tTop">
<div class="impBtn fLeft" ><input name="image" id="image" type="file" class="file huge" /></div>
<table id=’tbl’ style="clear:both"></table>
<input type="submit" value="Submit" class="button" >
</td>
</tr>
<tr>
<td class="tRight tTop"></td>
<td class="tLeft"></td>
</tr>
</table>
</form>
<br />
<br />
<font color="#993300">图片上传完成后,点击图片生成水印,生成水印后点击图片右击另存为,或者右击copy图片地址。</font><br />
<br />

When picture finished uploading, click on the image watermark generation, Right click save as picture, or right click copy picture address.<br />
<br />
<br />
<br />
</center>
<br />
<br />

<hr />
<div align="center">
Build by Mechanist Shanmao (2013)
</div>
<script type="text/javascript">
jQuery(function(){
jQuery(".sy").click(function(){
var img = jQuery(this).attr("pid")+"<{$img}>";
jQuery.post("__URL__/sy_shanmao",{img:img},function(data){
if(data.info!="success"){
alert(data.info);
return false;
}
jQuery("img").each(function(){
var imgp = jQuery(this).attr("src");
var ex = new RegExp(data.data);
if(ex.test(imgp)){
jQuery(this).attr("src","/cityosweb/gameweb/Tpl/default/avatar/shuiyin/s/"+data.data);
}
});
//jQuery().find("img").attr("/cityosweb/gameweb/upload/shuiyin/s/"+data.data);
},"json");
});
});
</script>
</body>
</html>
[/code]