/** * Set unique name for file * * @param File $file * @return File */ public function file(File $file) { if (FileSystem::exists($file->fullPath())) { $original_name = $file->name(); $file->setName($original_name . '-1'); for ($i = 2; FileSystem::exists($file->fullPath()); $i++) { $file->setName($original_name . '-' . $i); } } return $file; }
/** * Create thumbs for file * * @param File $file * @return array * @throws \Crip\FileManager\Exceptions\FileManagerException */ public function create(File $file) { foreach ($this->sizes as $size_key => $size) { $img = app(ImageManager::class)->make($file->fullPath()); $new_path = $file->getPath()->thumbPath($size_key); FileSystem::mkdir($new_path, 777, true); switch ($size[2]) { case 'width': // resize the image to a width of $sizes[ 0 ] and constrain aspect ratio (auto height) $img->resize($size[0], null, function ($constraint) { $constraint->aspectRatio(); }); break; case 'height': // resize the image to a height of $sizes[ 1 ] and constrain aspect ratio (auto width) $img->resize(null, $size[1], function ($constraint) { $constraint->aspectRatio(); }); break; // 'resize' // 'resize' default: $img->fit($size[0], $size[1]); break; } $img->save($file->getPath()->thumbPath($size_key, $file)); } return $this->details($file); }
/** * @param File $file * * @return bool * * @throws FileManagerException */ public function deleteFile(File $file) { $path = $file->fullPath(); if (!FileSystem::exists($path)) { throw new FileManagerException($this, 'err_file_not_found'); } if (!Perms::canDelete($path)) { throw new FileManagerException($this, 'err_file_delete_perm'); } if (FileSystem::delete($path)) { if ($file->isImage()) { $this->thumb->delete($file); } return true; } return false; }