Thinkphp5上传缩略图

/**
 * @param $width   裁剪的宽度
 * @param $height  裁剪的高度
 * @param string $filePath  保存缩略图文件的文件夹路径
 * @param string $name 上传的文件类型
 * @return array  缩略图和原图路径
 * @author: hhygyl  * @name: uploadThumb
 * @describe:缩略图上传
 */
public function uploadThumb($width, $height, $filePath = "thumb_images", $name = "file")
{
    $file = request()->file($name);
    if($file) {
        $filePaths = ROOT_PATH.'public'.DS.'uploads'.DS.$filePath;
        $info      = $file->validate(['size' => 2014*12, 'ext' => 'jpg,png,gif,jpeg'])->move($filePaths);
        if($info) {
            $md5       = $file->hash('md5');
            $sha1      = $file->hash('sha1');
            $imgpath   = 'uploads/'.$filePath.'/'.$info->getSaveName();
            $image     = \think\Image::open($imgpath);
            $date_path = 'uploads/'.$filePath.'/thumb/'.date('Ymd');
            if(!file_exists($date_path)) {
                mkdir($date_path, 0777, true);
            }
            $thumb_path = $date_path.'/'.$info->getFilename();
            $image->thumb($width, $height)->save($thumb_path);

            return ['code' => 1, 'msg' => "上传成功", 'thumb' => '/'.$thumb_path, 'path' => '/'.$imgpath , 'savename' => $info->getSaveName(),
             'filename' => $info->getFilename(), 'info' => $info->getInfo(), 'ext' => $info->getExtension(),'file_md5' => $md5, 
             'file_sha1' => $sha1, 'file_size' => $info->getSize(),'suffix' => $info->getType()];
        }
        else {
            // 上传失败获取错误信息
            return ['code' => 0, 'msg' => $file->getError()];
        }
    }
}

评论

(= ̄ω ̄=)··· 暂无内容!

回复

您还未登录,请先登录或者注册

微信扫一扫
关注该公众号