RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
使用TP5.1怎么为图片添加水印

这篇文章将为大家详细讲解有关使用TP5.1 怎么为图片添加水印,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联建站是一家专注于网站设计制作、成都网站制作与策划设计,云冈网站建设哪家好?创新互联建站做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:云冈等地区。云冈做网站价格咨询:13518219792

在 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】




   
   {$site.company}会员管理系统
   
   
   


   
       
                       
                           
                           上传照片            
       
       
           
               
                   
                                               

                   
               
           
       
       
           
                           
       
   
    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('上传失败 重试'); 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; }); });

【图片上传】

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'=>'保存失败'];
   }
}

关于使用TP5.1 怎么为图片添加水印就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站题目:使用TP5.1怎么为图片添加水印
转载源于:http://sczitong.cn/article/jijodp.html