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 "失敗";
        }

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM