88class FileApi
99{
1010 protected $ basepath ;
11+ protected $ thumb_sizes = [];
1112
1213 public function __construct ($ basepath = '/ ' )
1314 {
@@ -22,6 +23,12 @@ public function __construct($basepath = '/')
2223 $ this ->basepath = $ basepath ;
2324 }
2425
26+ public function thumbs ($ thumb_sizes )
27+ {
28+ $ this ->thumb_sizes = $ thumb_sizes ;
29+ return $ this ;
30+ }
31+
2532 public function save (UploadedFile $ upload_file , $ cus_name = null )
2633 {
2734 $ file = $ this ->moveFile ($ upload_file , $ cus_name );
@@ -31,42 +38,104 @@ public function save(UploadedFile $upload_file, $cus_name = null)
3138 private function moveFile ($ upload_file , $ cus_name )
3239 {
3340 $ suffix = $ upload_file ->getClientOriginalExtension ();
34- $ filename = uniqid () . '. ' . $ suffix ;
35- if (!empty ($ cus_name )) {
36- $ filename = $ cus_name . '. ' .$ suffix ;
37- }
3841
39- switch (\File::mimeType ($ upload_file )) {
40- case 'image/jpeg ' :
41- case 'image/jpg ' :
42- $ img = imagecreatefromjpeg ($ upload_file ->getRealPath ());
43- $ exif = read_exif_data ($ upload_file ->getRealPath ());
44- if (isset ($ exif ['Orientation ' ])) {
45- switch ($ exif ['Orientation ' ]) {
46- case 8 :
47- $ img = imagerotate ($ img , 90 , 0 );
48- break ;
49- case 3 :
50- $ img = imagerotate ($ img , 180 , 0 );
51- break ;
52- case 6 :
53- $ img = imagerotate ($ img , -90 , 0 );
54- break ;
55- }
56- }
57-
58- imagejpeg ($ img , $ upload_file ->getRealPath ());
42+ if (empty ($ cus_name )) {
43+ $ original_name = uniqid ();
44+ } else {
45+ $ original_name = $ cus_name ;
5946 }
47+ $ filename = $ original_name . '. ' .$ suffix ;
48+
49+ $ img = $ this ->setTmpImage ($ upload_file );
6050
6151 \Storage::put (
6252 $ this ->basepath . $ filename ,
6353 file_get_contents ($ upload_file ->getRealPath ())
6454 );
6555
6656 \File::delete ($ upload_file ->getRealPath ());
57+
58+ if (!is_null ($ img ) && !empty ($ this ->thumb_sizes )) {
59+ $ this ->saveThumb ($ img , $ original_name , $ suffix );
60+ }
61+
6762 return $ filename ;
6863 }
6964
65+ public function saveThumb ($ img , $ original_name , $ suffix )
66+ {
67+ foreach ($ this ->thumb_sizes as $ size ) {
68+ $ width = imagesx ($ img );
69+ $ height = imagesy ($ img );
70+
71+ $ arr_size = explode ('x ' , $ size );
72+ $ thumb_width = (int )$ arr_size [0 ];
73+ $ thumb_height = (int )$ arr_size [1 ];
74+
75+ $ thumb_name = $ this ->basepath . $ original_name . '_ ' . $ size . '. ' . $ suffix ;
76+ $ main_image = $ original_name . '. ' . $ suffix ;
77+ $ tmp_filename = 'tmp/ ' . $ main_image ;
78+
79+ // create a new temporary thumbnail image
80+ $ tmp_thumb = imagecreatetruecolor ($ thumb_width , $ thumb_height );
81+
82+ // copy and resize original image into thumbnail image
83+ imagecopyresized ($ tmp_thumb , $ img , 0 , 0 , 0 , 0 , $ thumb_width , $ thumb_height , $ width , $ height );
84+
85+ // save tmp image
86+ \Storage::disk ('local ' )->put ($ tmp_filename , \Storage::get ($ this ->basepath . $ main_image ));
87+ $ tmp_path = \Storage::disk ('local ' )->getDriver ()->getAdapter ()->getPathPrefix ();
88+
89+ // save thumbnail image
90+ imagepng ($ tmp_thumb , $ tmp_path . $ tmp_filename );
91+ $ tmp_file = \Storage::disk ('local ' )->get ($ tmp_filename );
92+ \Storage::put ($ thumb_name , $ tmp_file );
93+
94+ // remove tmp image
95+ \Storage::disk ('local ' )->delete ($ tmp_filename );
96+ }
97+ }
98+
99+ public function setTmpImage ($ upload_file )
100+ {
101+ $ image_types = array ('image/png ' , 'image/gif ' , 'image/jpeg ' , 'image/jpg ' );
102+
103+ if (in_array (\File::mimeType ($ upload_file ), $ image_types )) {
104+ switch (\File::mimeType ($ upload_file )) {
105+ case 'image/png ' :
106+ $ img = imagecreatefrompng ($ upload_file ->getRealPath ());
107+ break ;
108+ case 'image/gif ' :
109+ $ img = imagecreatefromgif ($ upload_file ->getRealPath ());
110+ break ;
111+ case 'image/jpeg ' :
112+ case 'image/jpg ' :
113+ default :
114+ $ img = imagecreatefromjpeg ($ upload_file ->getRealPath ());
115+ $ exif = read_exif_data ($ upload_file ->getRealPath ());
116+ if (isset ($ exif ['Orientation ' ])) {
117+ switch ($ exif ['Orientation ' ]) {
118+ case 8 :
119+ $ img = imagerotate ($ img , 90 , 0 );
120+ break ;
121+ case 3 :
122+ $ img = imagerotate ($ img , 180 , 0 );
123+ break ;
124+ case 6 :
125+ $ img = imagerotate ($ img , -90 , 0 );
126+ break ;
127+ }
128+ }
129+ }
130+
131+ imagepng ($ img , $ upload_file ->getRealPath ());
132+
133+ return $ img ;
134+ } else {
135+ return null ;
136+ }
137+ }
138+
70139 public function getPath ($ filename )
71140 {
72141 if (mb_substr ($ this ->basepath , -1 , 1 , 'utf8 ' ) != '/ ' ) {
@@ -122,4 +191,23 @@ public function getResponse($filename, $headers = [])
122191 abort (404 );
123192 }
124193 }
194+
195+ public function drop ($ filename )
196+ {
197+ // Cut original file name
198+ $ dot = strpos ($ filename , '. ' );
199+ $ origin_name = substr ($ filename , 0 , $ dot );
200+
201+ // Find all images in basepath
202+ $ allFiles = \Storage::files ($ this ->basepath );
203+ $ files = array_filter ($ allFiles , function ($ file ) use ($ origin_name )
204+ {
205+ return preg_match ('/^(.*) ' .$ origin_name .'(.*)$/ ' , $ file );
206+ });
207+
208+ // Delete original image and thumbnails
209+ foreach ($ files as $ file ) {
210+ \Storage::delete ($ file );
211+ }
212+ }
125213}
0 commit comments