Esempio n. 1
0
 public static function UploadImage($oriFilePath, $fileName, User $user, $diskName = null)
 {
     $fs = Storage::disk($diskName);
     $fileContent = $fs->get($oriFilePath);
     $mimeType = $fs->mimeType($oriFilePath);
     if (!str_is('image/*', $mimeType)) {
         $fs->delete($oriFilePath);
         throw new RequestValidationException(RequestValidationException::FileIsNotImage);
     }
     $fileModel = self::saveFileByFileContent($fileContent, $fileName, $mimeType, $user, $diskName);
     $fs->delete($oriFilePath);
     $image = Image::make($fileContent);
     $thumbnail = Image::make($fileContent)->widen(300)->encode($fileModel->mime);
     $thumbnailFileModel = FileManager::saveFileByFileContent($thumbnail->encoded, $fileModel->name . '-thumbnail.' . $fileModel->ext, $thumbnail->mime(), $user);
     if ($image->getWidth() > 1500) {
         $highResolution = Image::make($fileContent)->widen(1500)->encode($fileModel->mime);
         $highResolutionFileModel = FileManager::saveFileByFileContent($highResolution->encoded, $fileModel->name . '-high-resolution.' . $fileModel->ext, $highResolution->mime(), $user);
     } else {
         $highResolution = Image::make($fileContent)->widen($image->getWidth())->encode($fileModel->mime);
         $highResolutionFileModel = FileManager::saveFileByFileContent($highResolution->encoded, $fileModel->name . '-high-resolution.' . $fileModel->ext, $highResolution->mime(), $user);
     }
     $imageModel = new ImageModel();
     $imageModel->width = $image->getWidth();
     $imageModel->height = $image->getHeight();
     $imageModel->file()->associate($fileModel);
     $imageModel->thumbnailFile()->associate($thumbnailFileModel);
     $imageModel->highResolutionFile()->associate($highResolutionFileModel);
     $imageModel->save();
     return $imageModel;
 }