刮刮后端接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.3 KiB

<?php
namespace app\logic;
use Intervention\Image\ImageManagerStatic as Image;
use think\facade\Filesystem;
class Upload
{
/**
* 图片上传
* @param $file
* @param $path
* @return bool|string
*/
public static function uploadImage($file,$path = 'topic')
{
// 上传到本地服务器
// $file = request()->file('image');
return self::compressAndSave(Filesystem::putFile( $path, $file));
}
/**
* 多图片上传
* @param array $files
* @param $path
* @return array
*/
public static function uploadImageAll(array $files,$path = 'topic')
{
//$files = request()->file('image');
$saveName = [];
foreach($files as $file){
$saveName[] = Filesystem::putFile( $path, $file);
}
return $saveName;
}
/**
* 图片压缩
* @param $path
* @return mixed
*/
public static function compressAndSave($path)
{
// 获取根目录
$rootPath = app()->getRootPath();
$storage = config('filesystem.disks.public.url');
// 获取绝对路径
$absolutePath = $rootPath . 'public/' . "{$storage}/" . $path;
$image = Image::make($absolutePath);
$image->encode('jpg',10)->save($absolutePath);
return $path;
}
}