mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-19 00:22:15 +00:00
controller refactoring part 18
This commit is contained in:
48
src/Domain/Files/Actions/CreateImageThumbnailAction.php
Normal file
48
src/Domain/Files/Actions/CreateImageThumbnailAction.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
|
||||
class CreateImageThumbnailAction
|
||||
{
|
||||
/**
|
||||
* Create image thumbnail from gif, jpeg, jpg, png, webp or svg
|
||||
*/
|
||||
public function __invoke(
|
||||
string $file_path,
|
||||
string $filename,
|
||||
string $user_id
|
||||
): string|null {
|
||||
|
||||
$availableFormats = ['image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
|
||||
|
||||
// Create thumbnail from image
|
||||
if (in_array(Storage::disk('local')->mimeType($file_path), $availableFormats)) {
|
||||
// Get thumbnail name
|
||||
$thumbnail = "thumbnail-$filename";
|
||||
|
||||
// Create intervention image
|
||||
$image = Image::make(Storage::disk('local')->path($file_path))
|
||||
->orientate();
|
||||
|
||||
// Resize image
|
||||
$image->resize(512, null, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
})->stream();
|
||||
|
||||
// Store thumbnail to disk
|
||||
Storage::put("files/$user_id/$thumbnail", $image);
|
||||
}
|
||||
|
||||
// Return thumbnail as svg file
|
||||
if (Storage::disk('local')->mimeType($file_path) === 'image/svg+xml') {
|
||||
$thumbnail = $filename;
|
||||
}
|
||||
|
||||
return $thumbnail ?? null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user