| Conditions | 7 |
| Paths | 5 |
| Total Lines | 68 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 11 | public function makeThumbnail($fieldname = "image", $custom = []) |
||
| 12 | { |
||
| 13 | |||
| 14 | if (!empty(request()->$fieldname) && request()->has($fieldname) || $custom['image']) { |
||
|
|
|||
| 15 | |||
| 16 | /* ------------------------------------------------------------------- */ |
||
| 17 | |||
| 18 | $image_file = $custom['image'] ?? request()->file($fieldname); // Retriving Image File |
||
| 19 | $filenamewithextension = $image_file->getClientOriginalName(); //Retriving Full Image Name |
||
| 20 | $raw_filename = pathinfo($filenamewithextension, PATHINFO_FILENAME); //Retriving Image Raw Filename only |
||
| 21 | $filename = str_replace('-', '', $raw_filename); // Retrive Filename |
||
| 22 | $extension = $image_file->getClientOriginalExtension(); //Retriving Image extension |
||
| 23 | $imageStoreNameOnly = $filename . "-" . time(); //Making Image Store name |
||
| 24 | $imageStoreName = $filename . "-" . time() . "." . $extension; //Making Image Store name |
||
| 25 | |||
| 26 | /* ------------------------------------------------------------------- */ |
||
| 27 | |||
| 28 | /* ----------------------------------------Image Upload----------------------------------------- */ |
||
| 29 | $img = $custom['image'] ?? request()->$fieldname; |
||
| 30 | $this->update([ |
||
| 31 | $fieldname => $img->storeAs($custom['storage'] ?? config("thumbnail.storage_path", "uploads"), $imageStoreName, 'public') |
||
| 32 | ]); |
||
| 33 | /* --------------------------------------------------------------------------------------------- */ |
||
| 34 | |||
| 35 | $image = Image::cache(function ($cached_img) use ($image_file, $custom) { |
||
| 36 | return $cached_img->make($image_file->getRealPath())->fit($custom['width'] ?? config('thumbnail.img_width', 1000), $custom['height'] ?? config('thumbnail.img_height', 800)); |
||
| 37 | }, config('thumbnail.image_cached_time', 10), true); |
||
| 38 | $image->save(public_path('storage/' . $this->$fieldname), $custom['quality'] ?? config('thumbnail.image_quality', 80)); |
||
| 39 | |||
| 40 | if (config('thumbnail.thumbnail', true)) { |
||
| 41 | $thumbnails = false; |
||
| 42 | $thumbnails = $custom['thumbnails'] ?? config('thumbnail.thumbnails', false) ?? false; |
||
| 43 | $storage = $custom['storage'] ?? config('thumbnail.thumbnails_storage', false) ?? false; |
||
| 44 | if ($thumbnails) { |
||
| 45 | /* --------------------------------Custom Thumbnails------------------------------------------------- */ |
||
| 46 | foreach ($thumbnails as $thumbnail) { |
||
| 47 | $customthumbnail = $imageStoreNameOnly . '-' . $thumbnail['thumbnail-name'] . '.' . $extension; // Making Thumbnail Name |
||
| 48 | $custom_thumbnail = $image_file->storeAs($storage ?? config("thumbnail.storage_path", "uploads"), $customthumbnail, 'public'); // Thumbnail Storage Information |
||
| 49 | $make_custom_thumbnail = Image::cache(function ($cached_img) use ($image_file, $thumbnail) { |
||
| 50 | return $cached_img->make($image_file->getRealPath())->fit($thumbnail['thumbnail-width'], $thumbnail['thumbnail-height']); |
||
| 51 | }, config('thumbnail.image_cached_time', 10), true); //Storing Thumbnail |
||
| 52 | $make_custom_thumbnail->save(public_path('storage/' . $custom_thumbnail), $thumbnail['thumbnail-quality']); //Storing Thumbnail |
||
| 53 | } |
||
| 54 | /* -------------------------------------------------------------------------------------------------- */ |
||
| 55 | } else { |
||
| 56 | /* --------------------- Thumbnail Info--------------------------------- */ |
||
| 57 | //small thumbnail name |
||
| 58 | $smallthumbnail = $imageStoreNameOnly . '-small' . '.' . $extension; // Making Thumbnail Name |
||
| 59 | |||
| 60 | //medium thumbnail name |
||
| 61 | $mediumthumbnail = $imageStoreNameOnly . '-medium' . '.' . $extension; // Making Thumbnail Name |
||
| 62 | |||
| 63 | $small_thumbnail = $image_file->storeAs(config("thumbnail.storage_path", "uploads"), $smallthumbnail, 'public'); // Thumbnail Storage Information |
||
| 64 | $medium_thumbnail = $image_file->storeAs(config("thumbnail.storage_path", "uploads"), $mediumthumbnail, 'public'); // Thumbnail Storage Information |
||
| 65 | |||
| 66 | /* --------------------------------- Saving Thumbnail------------------------------------ */ |
||
| 67 | |||
| 68 | $medium_img = Image::cache(function ($cached_img) use ($image_file) { |
||
| 69 | return $cached_img->make($image_file->getRealPath())->fit(config('thumbnail.medium_thumbnail_width', 800), config('thumbnail.medium_thumbnail_height', 600)); //Storing Thumbnail |
||
| 70 | }, config('thumbnail.image_cached_time', 10), true); |
||
| 71 | |||
| 72 | $medium_img->save(public_path('storage/' . $medium_thumbnail), config('thumbnail.medium_thumbnail_quality', 60)); //Storing Thumbnail |
||
| 73 | |||
| 74 | $small_img = Image::cache(function ($cached_img) use ($image_file) { |
||
| 75 | return $cached_img->make($image_file->getRealPath())->fit(config('thumbnail.small_thumbnail_width', 400), config('thumbnail.small_thumbnail_height', 300)); //Storing Thumbnail |
||
| 76 | }, config('thumbnail.image_cached_time', 10), true); |
||
| 77 | |||
| 78 | $small_img->save(public_path('storage/' . $small_thumbnail), config('thumbnail.small_thumbnail_quality', 30)); //Storing Thumbnail |
||
| 79 | |||
| 159 |