176 lines
6.8 KiB
PHP
176 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace app\admin\service;
|
|
|
|
use app\model\Attachment;
|
|
use think\Exception;
|
|
use think\image\Image;
|
|
use think\File;
|
|
|
|
class FileService
|
|
{
|
|
|
|
protected $rootPath = './uploads/'; // 保存根路径
|
|
|
|
public function upload($file)
|
|
{
|
|
// $hasFile = $this->hasFile($file);
|
|
// if ($hasFile !== false) {
|
|
// return $hasFile;
|
|
// }
|
|
|
|
$info = $file->validate(['ext' => config('uploadFile.file_type')])->move($this->rootPath, true, false);
|
|
if ($info) {
|
|
|
|
$filePath = config('uploadFile.img_path') . DS . 'uploads' . DS . $info->getSaveName();
|
|
|
|
$data = [
|
|
'name' => $info->getInfo('name'), //图片原始名称
|
|
'savename' => $info->getFilename(), //新的图片名称
|
|
'filesize' => $info->getInfo('size'), //文件大小
|
|
'ext' => $info->getExtension(), //文件后缀
|
|
'md5' => $info->hash('md5'),
|
|
'sha1' => $info->hash('sha1'),
|
|
'mimetype' => $info->getMime(), //mime类型
|
|
'path' => config('uploadFile.img_path'), //路径
|
|
'url' => DS . 'uploads' . DS . $info->getSaveName(),
|
|
'create_time' => time(),
|
|
];
|
|
|
|
$isImg = in_array(strtolower($info->getExtension()), ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']); // 上传文件是否为图片
|
|
if ($isImg) {
|
|
// 生成 690 和1020尺寸的缩略图
|
|
$image = \think\Image::open($filePath);
|
|
$data['imagewidth'] = $image->width();
|
|
$data['imageheight'] = $image->height();
|
|
$data['imagetype'] = $info->getInFo('type');
|
|
|
|
$thumPath690 = 'uploads' . DS . 'thumb690' . DS . date('Ymd', time());
|
|
$thumPath1024 = 'uploads' . DS . 'thumb1024' . DS . date('Ymd', time());
|
|
$thumb1 = $this->saveThumbCOS($filePath, $thumPath690, $data['imagewidth'], $data['imageheight'], $data['savename'], 690);
|
|
$thumb2 = $this->saveThumbCOS($filePath, $thumPath1024, $data['imagewidth'], $data['imageheight'], $data['savename'], 1024);
|
|
$data['thum1'] = $thumb1 != false ? $thumb1 : 'uploads' . DS . $info->getSaveName();
|
|
$data['thum2'] = $thumb2 != false ? $thumb2 : 'uploads' . DS . $info->getSaveName();
|
|
}
|
|
|
|
|
|
//将图片转移到阿里云(原图)
|
|
$upload = ossUpload(trim(str_replace("\\", "/", $data['url']), '/'),$filePath);
|
|
|
|
unset($info); //释放变量才能删除图片
|
|
unlink($filePath); //把本地图片删除(原图)
|
|
|
|
if ($upload['code'] == -1){
|
|
throw new Exception($upload['msg']);
|
|
}
|
|
$result = Attachment::create($data);
|
|
|
|
if ($result) {
|
|
|
|
return [
|
|
'id' => $result->id,
|
|
'url' => str_replace('\\', '/', config('uploadFile.url') . $result['url']),
|
|
'name' => $result['name'],
|
|
'thumb_url' => $isImg ? str_replace('\\', '/', config('uploadFile.url') . '/' . $result['thum1']): '',
|
|
'thumb_url2' => $isImg ?str_replace('\\', '/', config('uploadFile.url') . '/' . $result['thum2']): '',
|
|
'ext' => $result['ext']
|
|
];
|
|
} else {
|
|
throw new Exception('文件保存失败');
|
|
}
|
|
} else {
|
|
throw new Exception($file->getError());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查上传文件是否存在,若存在则直接返回文件
|
|
* @param $file 上传文件内容
|
|
* @return array|bool
|
|
* @author: bordon
|
|
*/
|
|
public function hasFile($file)
|
|
{
|
|
if ($attach = Attachment::where(['md5' => $file->hash('md5'), 'sha1' => $file->hash('sha1')])->find()) {
|
|
return [
|
|
'id' => $attach->id,
|
|
'url' => config('uploadFile.url') . $attach['url'],
|
|
'name' => $attach['name'],
|
|
'thumb_url' => config('uploadFile.url') . DS . $attach['thum1'],
|
|
'thumb_url2' => config('uploadFile.url') . DS . $attach['thum2'],
|
|
'ext' => $attach['ext']
|
|
];
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 生成固定尺寸缩略图
|
|
* @param $image 图片内容
|
|
* @param $path 存储路径
|
|
* @param $width 图片宽度
|
|
* @param $height 图片高度
|
|
* @param savename 保存文件名
|
|
* @param $saveWidth 保存文件最大宽度
|
|
* @author: bordon
|
|
*/
|
|
public function saveThumb($filePath, $path, $width, $height, $savename, $saveWidth)
|
|
{
|
|
if ($width < $saveWidth) {
|
|
return false;
|
|
}
|
|
if (!is_dir($path)) {
|
|
mkdir($path, 0755, true);
|
|
}
|
|
\think\Image::open($filePath)->thumb($saveWidth, $height * ($width / $saveWidth))->save($path . DS . $savename, null, 100);
|
|
return $path . DS . $savename;
|
|
}
|
|
|
|
/**
|
|
* 生成固定尺寸缩略图存到腾讯云
|
|
* @param $image 图片内容
|
|
* @param $path 存储路径
|
|
* @param $width 图片宽度
|
|
* @param $height 图片高度
|
|
* @param savename 保存文件名
|
|
* @param $saveWidth 保存文件最大宽度
|
|
* @param $isWorker 是否定时器使用这方法(定时器保存图片路径要加上public)
|
|
* @author: bordon
|
|
*/
|
|
public function saveThumbCOS($filePath, $path, $width, $height, $savename, $saveWidth, $isWorker = false){
|
|
if ($width < $saveWidth) {
|
|
return false;
|
|
}
|
|
if (!is_dir($path)) {
|
|
mkdir($path, 0755, true);
|
|
}
|
|
$pathName = $path . DS . $savename; //入库 url字段
|
|
$thumbPath = config('uploadFile.img_path') . $pathName; //图片全路径
|
|
try{
|
|
//裁剪图片
|
|
\think\Image::open($filePath)->thumb($saveWidth, $height * ($width / $saveWidth))->save($pathName, null, 100);
|
|
if ($isWorker){
|
|
$thumbPath = ROOT_PATH . $pathName; //图片全路径(多了一层public目录)
|
|
$pathName = str_replace('public', '', $pathName); //保存图片到腾讯云要过滤public,保存路径为/uploads/../..
|
|
}
|
|
//将图片转移到阿里云
|
|
$upload = ossUpload( trim(str_replace("\\", "/", $pathName), '/') , $thumbPath);
|
|
|
|
//$upload = ossUpload($pathName, $thumbPath);
|
|
|
|
unlink($thumbPath); //把本地图片删除(缩略图)
|
|
if ($upload['code'] == 1){
|
|
return $pathName;
|
|
}else{
|
|
trace($upload['msg'], 'error');
|
|
return false;
|
|
}
|
|
}catch (\Exception $e){
|
|
unlink($thumbPath); //把本地图片删除(原图)
|
|
trace($e->getMessage(), 'error');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
} |