Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
Upload | |
0.00% |
0 / 19 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
uploadImage | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
uploadFile | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Traits; |
4 | |
5 | use Exception; |
6 | use Illuminate\Support\Facades\Storage; |
7 | use Intervention\Image\ImageManagerStatic as Image; |
8 | |
9 | trait Upload |
10 | { |
11 | // $width => if i pass don't pass width image uploaded with fullsize |
12 | public static function uploadImage($image, $path, $width = NULL){ |
13 | try { |
14 | //Change Name |
15 | $imageName = rand(0,1000000) . time() . '.' . $image->getClientOriginalExtension(); |
16 | |
17 | //upload full size |
18 | if(!$width) |
19 | $image->move(public_path($path), $imageName); |
20 | |
21 | //upload width new size |
22 | if($width){ |
23 | $image_resize = Image::make($image->getRealPath()); |
24 | |
25 | //get new size |
26 | $new_width = $image_resize->width(); |
27 | $new_height = $image_resize->height(); |
28 | |
29 | if($width < $image_resize->width()){ |
30 | $original_width_origin = ($width / $image_resize->width()) * 100; |
31 | $new_height = ($image_resize->height() / 100) * $original_width_origin; |
32 | $new_width = $width; |
33 | } |
34 | |
35 | $image_resize->resize($new_width, $new_height) |
36 | ->save(public_path($path . '/' . $imageName)); |
37 | } |
38 | |
39 | return $imageName; |
40 | } catch (Exception $e) { |
41 | echo 'Caught exception: ', $e->getMessage(), "\n"; |
42 | } |
43 | } |
44 | |
45 | |
46 | public function uploadFile($file, $path){ |
47 | $name = rand(0,1000000) . time() . '.' . $file->getClientOriginalExtension(); |
48 | |
49 | // $file->move(base_path( $path . '/'), $name); |
50 | $file->move(base_path('public/' . $path . '/'), $name); |
51 | |
52 | return $name; |
53 | } |
54 | } |