6
6
use Illuminate \Contracts \Config \Repository ;
7
7
use Illuminate \Filesystem \FilesystemManager ;
8
8
use Illuminate \Validation \Factory ;
9
+ use Intervention \Image \Facades \Image ;
9
10
use Symfony \Component \HttpFoundation \File \UploadedFile ;
10
11
11
12
/**
@@ -62,9 +63,9 @@ class FileUploader
62
63
* @param FileRepositoryInterface $files
63
64
*/
64
65
public function __construct (
65
- Factory $ validator ,
66
- FileRotator $ rotator ,
67
- FilesystemManager $ manager ,
66
+ Factory $ validator ,
67
+ FileRotator $ rotator ,
68
+ FilesystemManager $ manager ,
68
69
FileRepositoryInterface $ files
69
70
)
70
71
{
@@ -74,6 +75,41 @@ public function __construct(
74
75
$ this ->validator = $ validator ;
75
76
}
76
77
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
+
77
113
/**
78
114
* Upload a file.
79
115
*
0 commit comments