近段时间,在为公司开发网站管理系统页面,有些客户要求,上传的照片要能够自动添加水印功能,以防止其他人复用或者盗用一些比较重要的图片,给公司带来不必要的麻烦和损失,并且也能够防止别人进行图片的侵权。通过反复研究,结合layui,初步完成了图片的上传以及水印功能的添加。在这里列出部分重要代码,希望对大家有用。
首先,第一步需要安装图片处理插件,而安装此插件首先要在自己的电脑安装Composer软件,TP5.1操作手册提供了Composer的安装步骤:
在 Linux 和 Mac OS X 中可以运行如下命令:
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
在 Windows 中,你需要下载并运行 Composer-Setup.exe。具体的安装在这里就不详说了,安装完成Composer软件之后,就需要安装图片插件了,打开运行窗口(系统键+R),输入cmd,回车后,定位到自己的项目目录,然后运行:composer require topthink/think-image。
安装完成后就可以进行下一步的工作了。
下面是我的部分代码,仅供大家参考。
【HTML】
html>
<meta>
<title>{$site.company}会员管理系统</title>
<link>
<link>
<script></script>
<div>
<div>
<label>照片上传</label>
<div>
<input>
</div>
<div>
<button>上传照片</button>
</div>
</div>
<div>
<div>
<div>
<div>
<img>
<p></p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/learn/6657" title="ThinkPHP5.1企业站点快速开发-源码课件"><img
src="https://img.php.cn/upload/webcode/000/000/164/5fe3efb458cfd510.png" alt="ThinkPHP5.1企业站点快速开发-源码课件" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/learn/6657" title="ThinkPHP5.1企业站点快速开发-源码课件" class="overflowclass">ThinkPHP5.1企业站点快速开发-源码课件</a>
<p class="overflowclass">ThinkPHP5.1企业站点快速开发</p>
</div>
<a rel="nofollow" href="/xiazai/learn/6657" title="ThinkPHP5.1企业站点快速开发-源码课件" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<input>
</div>
</div>
</div>
<script>
layui.use(['form','layer','upload','element'], function(){
$ = layui.jquery;
var form = layui.form
,layer = layui.layer;
var upload = layui.upload;
var element = layui.element;
//常规使用 - 普通图片上传
var uploadInst = upload.render({
elem: '#face1'
,url: '{:url("uploadFile")}'
,before: function(obj){
//预读本地文件示例,不支持ie8
obj.preview(function(index, file, result){
$('#face_show').attr('src', result); //图片链接(base64)
});
element.progress('demo', '0%'); //进度条复位
layer.msg('上传中', {icon: 16, time: 0});
}
,done: function(data){
//如果上传失败
if(data.code > 0){
layer.msg('上传成功');
document.getElementById('face').value = data.path;
$('#faceText').html(''); //置空上传失败的状态
}else {
layer.msg('上传失败',{icon:2});
}
}
,error: function(){
//演示失败状态,并实现重传
var demoText = $('#faceText');
demoText.html('<span style="color: #FF5722;">上传失败 <a class="layui-btn layui-btn-xs demo-reload">重试');
demoText.find('.demo-reload').on('click', function(){
uploadInst.upload();
});
}
//进度条
,progress: function(n, index, e){
element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
if(n == 100){
layer.msg('上传完毕', {icon: 1});
}
}
});
form.on('submit(add)', function(data){
console.log(data);
//发异步,把数据提交给php
$.post('{:url(\'save\')}',$('form').serialize(),function(data){
if(data.code == 1){
layer.msg(data.msg);
setTimeout(function(){parent.window.location.reload();},1000);
}else{
layer.alert(data.msg, {icon: 6});
}
})
return false;
});
});
</script>
【图片上传】
public function uploadFile(){
//获取上传文件信息
$file = request()->file('file');
//以在上传目录下面生成以当前日期为子目录,存放上传文件
$path = date("Ymd");
//以当前时间和100~1000之间的随机数作为文件名称
$filename = time().rand(100,1000);
//将上传的文件移动到指定目录下
$info = $file->move('uploadfile/'.$path.'/',$filename);
//验证图片并移动到指定目录
if ($info){
//返回上传成功提示信息
//获取图片的名字
$imgName = $info->getFilename();
$size = $info->getInfo('size');
//获取图片的路径
$photo1 ='/uploadfile/'.$path.'/'.$info->getSaveName();
return json(['code'=>1,'path'=>$photo1]);
}else{
//返回上传失败提示信息
return $file->getError();
}
}
【水印类库】
namespace app\api\classes;
use think\Image;
class imgWaterClass
{
/**图片文字水印
* object(think\Image)#47 (3) {
["im":protected] => resource(96) of type (gd)
["gif":protected] => NULL
["info":protected] => array(4) {
["width"] => int(750)
["height"] => int(450)
["type"] => string(4) "jpeg"
["mime"] => string(10) "image/jpeg"
}
}
*
*/
public function imageWaterText($path,$text){
$img = ".".$path;
$image = Image::open($img);
$image->text($text,'./static/style/font/simsun.ttc',20,'#ffffff',9 ,"-10px")->save($img);
return $img;
}
public function imageWaterImg($path,$logo){
$img = ".".$path;
$logo = ".".$logo;
$image = Image::open($img);
$image->water($logo,Image::WATER_SOUTHEAST)->save($img);
return $img;
}
}
【后台程序处理】
public function save(){
$data = Request::param();
$water = new imgWaterClass();
$img_url = $data['face'];//需要添加水印的图片
$path = "/uploads/logo.png";//水印图片
$img = $water->imageWaterImg($img_url,$path);//添加水印图片
$img_text = $water->imageWaterText($img_url,'我是水印');//添加水印文字
if($img){
return ['code'=>1,'msg'=>'保存成功'];
}else{
return ['code'=>0,'msg'=>'保存失败'];
}
}
这是我根据我的实际操作写的一部分代码,希望对各位能够有所帮助。
谢谢!
相关推荐:最新的10个thinkphp视频教程










