Skip to content

Commit c8dc265

Browse files
committed
refactoring uploader
1 parent a6e9017 commit c8dc265

File tree

7 files changed

+206
-117
lines changed

7 files changed

+206
-117
lines changed

src/BaseUploader.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Dosarkz\LaravelUploader;
3+
4+
/**
5+
* Class BaseUploader
6+
* @package Dosarkz\LaravelUploader
7+
*/
8+
class BaseUploader
9+
{
10+
/**
11+
* @param $uploading_file
12+
* @param $destination
13+
* @return FileUploader
14+
*/
15+
public static function file($uploading_file, $destination = null)
16+
{
17+
return new FileUploader($uploading_file, $destination);
18+
}
19+
20+
/**
21+
* @param $uploading_file
22+
* @param null $destination
23+
* @param bool $resize
24+
* @param $imageWidth
25+
* @param $imageHeight
26+
* @param $thumbWidth
27+
* @param $thumbHeight
28+
* @return ImageUploader
29+
*/
30+
public static function image($uploading_file, $destination = null, $resize = false, $imageWidth = 800,
31+
$imageHeight = null, $thumbWidth = 200, $thumbHeight = null)
32+
{
33+
return new ImageUploader($uploading_file, $destination, $resize, $imageWidth, $imageHeight,
34+
$thumbWidth, $thumbHeight);
35+
}
36+
}

src/Facade/LaravelUploaderFacade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class LaravelUploaderFacade extends Facade {
1111
*/
1212
protected static function getFacadeAccessor()
1313
{
14-
return 'laraveluploader';
14+
return 'uploader';
1515
}
1616
}

src/FileUploader.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace Dosarkz\LaravelUploader;
3+
4+
use Illuminate\Support\Facades\File;
5+
6+
/**
7+
* Class FileUploader
8+
* @package Dosarkz\LaravelUploader
9+
*/
10+
class FileUploader extends Uploader
11+
{
12+
/**
13+
* FileUploader constructor.
14+
* @param $uploaded_file
15+
* @param null $destination
16+
*/
17+
public function __construct($uploaded_file, $destination = null)
18+
{
19+
$this->setUploadedFile($uploaded_file);
20+
$this->setDestination($destination);
21+
22+
if (!is_dir(public_path($this->getDestination())))
23+
{
24+
File::makeDirectory(public_path($this->getDestination()), $mode = 0777, true);
25+
}
26+
27+
parent::__construct();
28+
}
29+
}

src/ImageUploader.php

Lines changed: 109 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,52 @@
22
namespace Dosarkz\LaravelUploader;
33

44
use Illuminate\Support\Facades\File;
5+
use Intervention\Image\Facades\Image;
56

7+
/**
8+
* Class ImageUploader
9+
* @package Dosarkz\LaravelUploader
10+
*/
611
class ImageUploader extends Uploader
712
{
8-
public function __construct($image = null, $destination = null)
9-
{
10-
$this->image = $image;
13+
/**
14+
* @var bool
15+
*/
16+
protected $resize;
17+
/**
18+
* @var
19+
*/
20+
protected $imageWidth;
21+
/**
22+
* @var
23+
*/
24+
protected $imageHeight;
25+
/**
26+
* @var
27+
*/
28+
protected $thumbWidth;
29+
/**
30+
* @var
31+
*/
32+
protected $thumbHeight;
33+
/**
34+
* @var
35+
*/
36+
protected $thumb;
1137

12-
if ($destination == null)
13-
{
14-
$this->destination = 'uploads/images/';
15-
}else{
16-
$this->destination = $destination;
17-
}
1838

19-
$this->thumbWidth = 150;
39+
public function __construct($uploaded_file, $destination = null, $resize = false, $imageWidth = 800,
40+
$imageHeight = null, $thumbWidth = 200, $thumbHeight = null)
41+
{
42+
$this->setUploadedFile($uploaded_file);
43+
$this->setDestination($destination);
44+
$this->setThumb('thumb_' . $this->getFileName());
45+
46+
$this->resize = $resize;
47+
$this->imageWidth = $imageWidth;
48+
$this->imageHeight = $imageHeight;
49+
$this->thumbWidth = $thumbWidth;
50+
$this->thumbHeight = $thumbHeight;
2051

2152
if (!is_dir(public_path($this->destination)))
2253
{
@@ -25,4 +56,72 @@ public function __construct($image = null, $destination = null)
2556

2657
parent::__construct();
2758
}
59+
60+
/**
61+
* @return mixed
62+
*/
63+
public function getThumb()
64+
{
65+
return $this->thumb;
66+
}
67+
68+
/**
69+
* @param mixed $thumb
70+
*/
71+
public function setThumb($thumb)
72+
{
73+
$this->thumb = $thumb;
74+
}
75+
76+
/**
77+
*
78+
*/
79+
public function upload()
80+
{
81+
$this->uploadImageFile($this->getFileName());
82+
$this->uploadImageThumb($this->getThumb());
83+
}
84+
85+
86+
/**
87+
* @param $filename
88+
* @return mixed
89+
*/
90+
public function uploadImageFile($filename)
91+
{
92+
list($width, $height) = getimagesize($this->getUploadedFile()->getRealPath());
93+
94+
if ($width > $this->imageWidth)
95+
{
96+
return Image::make($this->getUploadedFile()->getRealPath())
97+
->resize($this->imageWidth, $this->imageHeight, function ($constraint) {
98+
$constraint->aspectRatio();
99+
})->save(public_path($this->getDestination() . $filename));
100+
}else {
101+
return Image::make($this->getUploadedFile()->getRealPath())
102+
->save(public_path($this->getDestination() . $filename));
103+
}
104+
}
105+
/**
106+
* @param $thumb
107+
* @return mixed
108+
*/
109+
public function uploadImageThumb($thumb)
110+
{
111+
list($width, $height) = getimagesize($this->getUploadedFile()->getRealPath());
112+
113+
if ($width > $this->thumbWidth)
114+
{
115+
Image::make($this->getUploadedFile()->getRealPath())
116+
->resize($this->thumbWidth, $this->thumbHeight, function ($constraint) {
117+
$constraint->aspectRatio();
118+
})
119+
->save(public_path($this->getDestination() . $thumb));
120+
}else{
121+
return Image::make($this->getUploadedFile()->getRealPath())
122+
->save(public_path($this->getDestination() .'/'. $thumb));
123+
}
124+
return $thumb;
125+
}
126+
28127
}

src/Provider/LaravelUploaderServiceProvider.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22
namespace Dosarkz\LaravelUploader\Provider;
33

4-
use Dosarkz\LaravelUploader\ImageUploader;
5-
use Illuminate\Support\Facades\App;
4+
use Dosarkz\LaravelUploader\BaseUploader;
65
use Illuminate\Support\ServiceProvider;
7-
use Intervention\Image\ImageServiceProvider;
86

97
class LaravelUploaderServiceProvider extends ServiceProvider
108
{
@@ -16,7 +14,7 @@ class LaravelUploaderServiceProvider extends ServiceProvider
1614
public function boot()
1715
{
1816
$this->publishes([
19-
__DIR__ . '/config/laraveluploader.php' => config_path('laraveluploader.php'),
17+
__DIR__ . '/config/laravel-uploader.php' => config_path('laravel-uploader.php'),
2018
]);
2119

2220

@@ -32,8 +30,8 @@ public function register()
3230
$this->app->bind('Image', 'Intervention\Image\Facades\Image');
3331

3432

35-
$this->app->singleton('laraveluploader', function ($app) {
36-
return new ImageUploader();
33+
$this->app->singleton('uploader', function ($app) {
34+
return new BaseUploader();
3735
});
3836
}
3937

0 commit comments

Comments
 (0)