liyaozhi
2025-10-28 1320688354fd168c51cf2e05f29a2253f4ed9c00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
 
namespace app\common\library\storage\engine;
 
use think\facade\Filesystem;
use think\Image;
use app\common\model\settings\Setting as SettingModel;
 
use function Qiniu\thumbnail;
 
/**
 * 本地文件驱动
 */
class Local extends Server
{
    public function __construct()
    {
        parent::__construct();
    }
 
    /**
     * 上传图片文件
     */
    public function upload()
    {
        return $this->isInternal ? $this->uploadByInternal() : $this->uploadByExternal();
    }
 
    /**
     * 外部上传(指用户上传,需验证文件类型、大小)
     */
    private function uploadByExternal()
    {
        $saveName = '';
        // 验证文件并上传
        try{
            $saveName = Filesystem::disk('public')->putFile( '', $this->file);
        }catch (\Exception $e){
            log_write('文件上传异常:'.$e->getMessage());
        }
        return $saveName;
    }
 
    /**
     * 内部上传(指系统上传,信任模式)
     */
    private function uploadByInternal()
    {
        // 上传目录
        $uplodDir = public_path()  . '/uploads';
        // 要上传图片的本地路径
        $realPath = $this->getRealPath();
        if (!rename($realPath, "{$uplodDir}/$this->fileName")) {
            $this->error = 'upload write error';
            return false;
        }
        return true;
    }
 
    /**
     * 删除文件
     */
    public function delete($fileName)
    {
        // 文件所在目录
        $filePath = public_path() . "/uploads/{$fileName}";
        return !file_exists($filePath) ?: unlink($filePath);
    }
 
    /**
     * 返回文件路径
     */
    public function getFileName()
    {
        return $this->fileName;
    }
 
    /**
     * 生成缩略图(将原图复制到新路径,然后将原路径变为缩略图) by lyzflash
     */
    public function createThumb($saveName, $is_original = false)
    {
        // 获取上传设置
        $config = SettingModel::getItem('storage');
        $image = Image::open(public_path() . '/uploads/' . $saveName);
        $width = $image->width();
        $max_width = $config['max_width'] ?: 750;
        // 图片宽度大于系统设置时才处理
        $original_path = '';
        if ($width > $max_width) {
            // 如果保留原图
            if ($is_original) {
                $original_path = date('Ymd') .'/'. $this->buildOriginalName(); // 原图文路径
                $image->save(public_path()  . '/uploads/' . $original_path);
            }
            // 生成缩略图
            $image->thumb($max_width, $image->height())->save(public_path()  . '/uploads/'. $saveName);
        }
        return $original_path; // 注意:返回的是原图的路径
    }
 
    /**
     * 生成原图文件名
     */
    private function buildOriginalName()
    {
        // 要上传图片的本地路径
        $realPath = $this->file->getPathname();
        // 扩展名
        $ext = $this->file->getOriginalExtension();
        // 自动生成文件名
        return date('YmdHis') . substr(md5($realPath), 0, 6)
            . str_pad(rand(0, 9999), 6, '0', STR_PAD_LEFT) . ".{$ext}";
    }
 
}