Skip to content

Commit cf9d524

Browse files
authored
Merge pull request #5 from openclassify/resize-and-upload-added
image-resize
2 parents fbd3c78 + 6e9011f commit cf9d524

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/File/FileUploader.php

+39-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Config\Repository;
77
use Illuminate\Filesystem\FilesystemManager;
88
use Illuminate\Validation\Factory;
9+
use Intervention\Image\Facades\Image;
910
use Symfony\Component\HttpFoundation\File\UploadedFile;
1011

1112
/**
@@ -62,9 +63,9 @@ class FileUploader
6263
* @param FileRepositoryInterface $files
6364
*/
6465
public function __construct(
65-
Factory $validator,
66-
FileRotator $rotator,
67-
FilesystemManager $manager,
66+
Factory $validator,
67+
FileRotator $rotator,
68+
FilesystemManager $manager,
6869
FileRepositoryInterface $files
6970
)
7071
{
@@ -74,6 +75,41 @@ public function __construct(
7475
$this->validator = $validator;
7576
}
7677

78+
public function resizeAndUpload(UploadedFile $file, FolderInterface $folder, $resizeOptions = [])
79+
{
80+
$entry = $this->upload($file, $folder);
81+
82+
$type = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
83+
if (in_array($type, config('anomaly.module.files::mimes.types.image'))) {
84+
85+
$image = Image::make($file->getRealPath());
86+
87+
if (!empty($resizeOptions)) {
88+
$image->resize($resizeOptions['width'], $resizeOptions['height'], function ($constraint) {
89+
$constraint->aspectRatio();
90+
$constraint->upsize();
91+
});
92+
}
93+
94+
$resizedImageContent = $image->encode($file->getClientOriginalExtension(), $resizeOptions['quality']);
95+
96+
$resizedPath = $folder->getSlug() . '/resized-' . FileSanitizer::clean($file->getClientOriginalName());
97+
98+
$resizedEntry = $this->manager->disk($folder->getDisk()->getSlug())->write($resizedPath, $resizedImageContent);
99+
$this->files->save(
100+
$resizedEntry
101+
->setAttribute('size', strlen($resizedImageContent))
102+
->setAttribute('width', $resizeOptions['width'])
103+
->setAttribute('height', $resizeOptions['height'])
104+
->setAttribute('mime_type', $file->getMimeType())
105+
);
106+
107+
return $resizedEntry;
108+
}
109+
110+
return $entry;
111+
}
112+
77113
/**
78114
* Upload a file.
79115
*

0 commit comments

Comments
 (0)