88class FileApi
99{
1010 protected $ basepath ;
11- protected $ thumb_sizes = ['S ' => '96x96 ' , 'M ' => '256x256 ' , 'L ' => '480x480 ' ];
11+ protected $ default_sizes = ['S ' => '96x96 ' , 'M ' => '256x256 ' , 'L ' => '480x480 ' ];
12+ protected $ thumb_sizes = null ;
13+ protected $ shouldCropThumb = false ;
1214
1315 public function __construct ($ basepath = '/ ' )
1416 {
@@ -46,6 +48,13 @@ public function thumbs($thumb_sizes = array())
4648 return $ this ;
4749 }
4850
51+ public function crop ()
52+ {
53+ $ this ->shouldCropThumb = true ;
54+
55+ return $ this ;
56+ }
57+
4958 public function save (UploadedFile $ upload_file , $ cus_name = null )
5059 {
5160 $ file = $ this ->moveFile ($ upload_file , $ cus_name );
@@ -151,7 +160,7 @@ private function moveFile($upload_file, $cus_name)
151160
152161 \File::delete ($ upload_file ->getRealPath ());
153162
154- if (!is_null ($ img ) && !empty ($ this ->thumb_sizes )) {
163+ if (!is_null ($ img ) && !empty ($ this ->getThumbSizes () )) {
155164 $ this ->saveThumb ($ img , $ original_name , $ suffix );
156165 }
157166
@@ -200,9 +209,9 @@ private function setTmpImage($upload_file)
200209
201210 private function saveThumb ($ img , $ original_name , $ suffix )
202211 {
203- foreach ($ this ->thumb_sizes as $ size_code => $ size ) {
204- if (is_int ($ size_code ) && $ size_code < count ($ this ->thumb_sizes )) {
205- $ size_name = $ size ;
212+ foreach ($ this ->getThumbSizes () as $ size_code => $ size ) {
213+ if (is_int ($ size_code ) && $ size_code < count ($ this ->getThumbSizes () )) {
214+ $ size_name = ' L ' ;
206215 } else {
207216 $ size_name = $ size_code ;
208217 }
@@ -211,7 +220,7 @@ private function saveThumb($img, $original_name, $suffix)
211220 $ main_image = $ original_name . '. ' . $ suffix ;
212221 $ tmp_filename = 'tmp/ ' . $ main_image ;
213222
214- $ tmp_thumb = $ this ->resizeThumb ($ img , $ size );
223+ $ tmp_thumb = $ this ->resizeOrCropThumb ($ img , $ size );
215224
216225 // save tmp image
217226 \Storage::disk ('local ' )->put ($ tmp_filename , \Storage::get ($ this ->basepath . $ main_image ));
@@ -227,25 +236,34 @@ private function saveThumb($img, $original_name, $suffix)
227236 }
228237 }
229238
230- private function resizeThumb ($ img , $ size )
239+ private function resizeOrCropThumb ($ img , $ size )
231240 {
232241 $ width = imagesx ($ img );
233242 $ height = imagesy ($ img );
234243 $ arr_size = explode ('x ' , $ size );
235244 $ thumb_width = (int )$ arr_size [0 ];
236245 $ thumb_height = (int )$ arr_size [1 ];
237246
247+ if ($ this ->thumbShouldCrop ()) {
248+ $ img = $ this ->cropThumb ($ img , $ width , $ height , $ thumb_width , $ thumb_height );
249+ } else {
250+ $ this ->resizeThumb ($ width , $ height , $ thumb_width , $ thumb_height );
251+ }
252+
238253 // create a new temporary thumbnail image
239254 $ tmp_thumb = imagecreatetruecolor ($ thumb_width , $ thumb_height );
240255
241- $ img = $ this ->cropThumb ($ img , $ width , $ height , $ thumb_width , $ thumb_height );
242-
243256 // copy and resize original image into thumbnail image
244257 imagecopyresized ($ tmp_thumb , $ img , 0 , 0 , 0 , 0 , $ thumb_width , $ thumb_height , $ width , $ height );
245258
246259 return $ tmp_thumb ;
247260 }
248261
262+ private function thumbShouldCrop ()
263+ {
264+ return $ this ->shouldCropThumb ;
265+ }
266+
249267 private function cropThumb ($ img , &$ width , &$ height , $ thumb_width , $ thumb_height )
250268 {
251269 $ image_ratio = $ height /$ width ;
@@ -281,4 +299,31 @@ private function cropThumb($img, &$width, &$height, $thumb_width, $thumb_height)
281299
282300 return $ img ;
283301 }
302+
303+ private function resizeThumb ($ width , $ height , &$ thumb_width , &$ thumb_height )
304+ {
305+ $ image_ratio = $ height /$ width ;
306+ $ thumb_ratio = $ thumb_height /$ thumb_width ;
307+
308+ if ($ image_ratio !== $ thumb_ratio ) {
309+ if ($ image_ratio < $ thumb_ratio ) {
310+ $ thumb_height = $ thumb_width *$ height /$ width ;
311+ } else if ($ image_ratio > $ thumb_ratio ) {
312+ $ thumb_width = $ thumb_height *$ width /$ height ;
313+ }
314+ }
315+ }
316+
317+ private function getThumbSizes ()
318+ {
319+ $ config = config ('fileapi.default_thumbs ' );
320+
321+ if (!is_null ($ this ->thumb_sizes )) {
322+ return $ this ->thumb_sizes ;
323+ } else if (!is_null ($ config )) {
324+ return $ config ;
325+ } else {
326+ return $ this ->default_sizes ;
327+ }
328+ }
284329}
0 commit comments