| 12
 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
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 
 | 
 
 public function upload()
 {
 $input        = input('post.');
 $uploadPath   = public_path().'upload/';
 $uploadFile   = $this->request->file('file');
 $originalName = $uploadFile->getOriginalName();
 $originalType = FileAddons::getType(config('upload.ext'),  $originalName);
 $chunkId      = empty($input['id']) ? md5(sha1_file($uploadFile).$input['size']) : $input['id'];
 $chunkIndex   = $input['index'];
 $chunkCount   = $input['count'];
 $chunkPath    = 'chunk/'.$chunkId.'/';
 
 
 
 $extension = strtolower($uploadFile->getOriginalExtension());
 $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
 if (!in_array($extension, $allowedExtensions)) {
 throw new ValidateException('只允许上传图片文件');
 }
 
 
 if (empty($originalType)) {
 return json(['status' => 'error', 'message' => '类型不支持,在常规管理中可配置!']);
 }
 try {
 validate([ 'file' => ['fileSize' => config('upload.size')[$originalType], 'fileExt' => config('upload.ext')[$originalType]] ])->check(['file' => $uploadFile]);
 } catch (ValidateException $e) {
 return json(['status' => 'error', 'message' => $e->getMessage()]);
 }
 
 if ($chunkIndex == 1) {
 if (is_dir($uploadPath . $chunkPath)) {
 $oldChunkIndex = count(scandir($uploadPath . $chunkPath)) - 3;
 if ($oldChunkIndex > 1) {
 return json(['status' => 'success', 'message' => '断点续传', 'index' => $oldChunkIndex, 'id' => $chunkId]);
 }
 }
 }
 
 Filesystem::disk('public')->putFileAs($chunkPath, $uploadFile, $chunkIndex . '.tmp');
 if ($chunkIndex < $chunkCount) {
 return json(['status' => 'success', 'message' => '分片上传', 'index' => $chunkIndex, 'id' => $chunkId]);
 }
 
 for ($i = 1; $i <= $chunkCount; $i++) {
 if (! file_exists($uploadPath . $chunkPath . $i . '.tmp')) {
 return json(['status' => 'error', 'message' => '文件损坏,请重新上传']);
 }
 }
 
 $filePath = $uploadPath . $originalType . '/' . date('Ymd')  . '/';
 if (! is_dir($filePath)) {
 mkdir($filePath, 0777, true);
 }
 $fileExt   = $uploadFile->getOriginalExtension() ? $uploadFile->getOriginalExtension() : substr(strrchr($originalName, '.'), 1);
 $fileName  = md5(microtime(true) . $originalName) . '.' . $fileExt;
 $fileWrite = @fopen($filePath . $fileName, "wb");
 if (flock($fileWrite, LOCK_EX)) {
 for ($i = 1; $i <= $chunkCount; $i++) {
 $uploadFile = $uploadPath . $chunkPath . $i . '.tmp';
 if (!$handle = @fopen($uploadFile, "rb")) {
 break;
 }
 while ($buff = fread($handle, filesize($uploadFile))) {
 fwrite($fileWrite, $buff);
 }
 @fclose($handle);
 @unlink($uploadFile);
 }
 flock($fileWrite, LOCK_UN);
 }
 @fclose($fileWrite);
 
 $save = FileModel::create([
 'title'       => $originalName,
 'type'        => $originalType,
 'size'        => $input['size'],
 'url'         => str_replace(public_path(), '/', $filePath . $fileName),
 'status'      => 1,
 'theme'       => theme(),
 'create_time' => $input['create_time'],
 ]);
 
 if ($originalType === "image" && $fileExt != 'ico' && $fileExt != 'gif') {
 
 thumbnail($save['url'],100,100);
 
 $watermark = $this->request->watermark;
 if (! empty($watermark)) {
 if ($watermark['open'] === 1) {
 
 $watermarkConfig = $this->request->watermark;
 if (!empty($watermarkConfig)) {
 if ($watermarkConfig['open'] === 1) {
 $file     = str_replace('\/', '/', public_path() . $save->url);
 $image    = Image::open($file);
 $scale    = (int)$watermarkConfig['scale'] / 100;
 $position = (int)$watermarkConfig['position'];
 $opacity  = (int)$watermarkConfig['opacity'];
 $height   = $image->height();
 $width    = $image->width();
 if ($watermarkConfig['type'] === 'image') {
 $water = public_path() . 'upload/watermark.png';
 if (is_file($water)) {
 if ($watermarkConfig['sizeType'] === 'scale') {
 
 $thumb = Image::open($water);
 $waterName  = pathinfo($save->url, PATHINFO_FILENAME);
 $waterThumb = str_replace('watermark', 'watermark_thumb', $water);
 $thumb->thumb($width*$scale, $height*$scale)->save($waterThumb);
 $image->water($waterThumb, $position, $opacity)->save($file);
 if (is_file($waterThumb)) {
 unlink($waterThumb);
 }
 } else {
 
 $image->water($water, $position, $opacity)->save($file);
 }
 }
 } else {
 $opacity    = 127 - (127 * $opacity / 100);
 $dechex     = dechex($opacity);
 $fontColor  = $watermarkConfig['fontColor'].$dechex;
 $fontSize   = $watermarkConfig['sizeType'] === 'scale' ? $scale * ($width/2) : $watermarkConfig['fontSize'];
 $fontFamily = public_path() . $watermarkConfig['fontFamily'];
 $image->text($watermarkConfig['fontText'], $fontFamily, $fontSize, $fontColor, $position, 0, $watermarkConfig['fontAngle'])->save($file);
 }
 }
 }
 }
 }
 }
 
 event('UploadEnd', $save);
 return json(['status' => 'success', 'message' => '上传成功', 'data' => $save]);
 }
 
 |