thinkPHP3.2 图片上传及压缩

最近经常需要用到图片上传及压缩,做个记录  tp3.2版本

    public function upload(){  

        if(!empty($_FILES)){

            $config = array(
                
                // 'saveName'   =>    array('uniqid',''),
                'exts'       =>    array('jpg', 'gif', 'png','jpeg'),
                'autoSub'    =>    false,
                'rootPaht'=>'./Uploads/',
                'savePath'   =>    './VideoImg/',
                    //'subName'    =>    array('date','Y-m'),
            );
            $upload = new \Think\Upload($config);// 实例化上传类

            $info   =   $upload->upload();

            if(!$info)  {

                $this->error($upload->getError()); // 上传错误提示错误信息

            }else{
                
                $image = new \Think\Image();

                foreach($info as $file) {

                    $thumb_file = './Uploads/VideoImg/' . $file['savename'];

                    $savePath =  './Uploads/VideoImg/'.'mini'. $file['savename'];

                    $image -> open( $thumb_file )->thumb(500,300,\Think\Image::IMAGE_THUMB_FILLED)->save($savePath);

                    return array(
                        'status' => 1,
                        'savepath' => $file['savepath'],
                        'savename' => $file['savename'],
                        'pic_path' => '/Uploads/VideoImg/' . $file['savename'],
                        'mini_pic' => '/Uploads/VideoImg/' . 'mini' .$file['savename']
                    );

                    @unlink($thumb_file); //上传生成缩略图以后删除源文件
                }
            }
        }
    }

你可能感兴趣的:(PHP)