• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
车车大人
博客园    首页    新随笔    联系   管理     

tp5文件上传实现缩略图+水印的功能

public function AddNews(){
        $data = Request::instance()->param();
        //接收文件
        $file = request()->file('img');
        //上传
        $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
        if($info){
            //获取文件名字
            $path = $info->getSaveName();
            //拼接一下图片绝对路径
            $last_path = ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads'.DS.$path;
        }else{
            $path = null;
            $last_path = null;
        }

        if($last_path){
            //打开原图
            $image = \think\Image::open($last_path);
            //生成缩略图
            $thumb_path = rand().'.jpg';
            $image->thumb(10, 10)->save(ROOT_PATH . 'public' . DS . 'static' . DS .'thumb'.DS.$thumb_path);


        }


        if($last_path){
            //打开图片
            $image = \think\Image::open($last_path);
            //生成水印
            $water_path = ROOT_PATH . 'public' . DS . 'static' . DS .'waters'.DS.rand().'.jpg';
            $image->text('刘桂池',ROOT_PATH . 'public' . DS .'ttf'.DS.'fangzheng.ttf',20,'#00000')->save($water_path);
        }

        //拼接数据入库
        $arr['title'] = $data['title'];
        $arr['t_name'] = $data['t_name'];
        $arr['img'] = $path;
        $arr['content'] = $data['news_content'];
        $arr['thumb'] = empty($thumb_path)? null : $thumb_path;

        if(Db::table('d13')->insert($arr)){
            echo "成功";
        }else{
            echo "失败";
        }

    }

 

通往牛逼的路上,在意的只有远方!
posted @ 2019-01-10 10:14  车车大人  阅读(1263)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3