Skip to content

Commit 17a4f6f

Browse files
committed
refactoring uploader
1 parent c8dc265 commit 17a4f6f

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/BaseUploader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public static function file($uploading_file, $destination = null)
3030
public static function image($uploading_file, $destination = null, $resize = false, $imageWidth = 800,
3131
$imageHeight = null, $thumbWidth = 200, $thumbHeight = null)
3232
{
33+
if ($destination == null)
34+
{
35+
$destination = 'uploads/images';
36+
}
37+
3338
return new ImageUploader($uploading_file, $destination, $resize, $imageWidth, $imageHeight,
3439
$thumbWidth, $thumbHeight);
3540
}

src/ImageUploader.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Illuminate\Support\Facades\File;
55
use Intervention\Image\Facades\Image;
6+
use Illuminate\Http\UploadedFile;
67

78
/**
89
* Class ImageUploader
@@ -41,7 +42,7 @@ public function __construct($uploaded_file, $destination = null, $resize = false
4142
{
4243
$this->setUploadedFile($uploaded_file);
4344
$this->setDestination($destination);
44-
$this->setThumb('thumb_' . $this->getFileName());
45+
4546

4647
$this->resize = $resize;
4748
$this->imageWidth = $imageWidth;
@@ -53,7 +54,6 @@ public function __construct($uploaded_file, $destination = null, $resize = false
5354
{
5455
File::makeDirectory(public_path($this->destination), $mode = 0777, true);
5556
}
56-
5757
parent::__construct();
5858
}
5959

@@ -76,8 +76,9 @@ public function setThumb($thumb)
7676
/**
7777
*
7878
*/
79-
public function upload()
79+
public function upload(UploadedFile $uploadedFile)
8080
{
81+
$this->setThumb('thumb_'.$this->getFileName());
8182
$this->uploadImageFile($this->getFileName());
8283
$this->uploadImageThumb($this->getThumb());
8384
}
@@ -96,10 +97,10 @@ public function uploadImageFile($filename)
9697
return Image::make($this->getUploadedFile()->getRealPath())
9798
->resize($this->imageWidth, $this->imageHeight, function ($constraint) {
9899
$constraint->aspectRatio();
99-
})->save(public_path($this->getDestination() . $filename));
100+
})->save(public_path($this->getDestination() .'/'. $filename));
100101
}else {
101102
return Image::make($this->getUploadedFile()->getRealPath())
102-
->save(public_path($this->getDestination() . $filename));
103+
->save(public_path($this->getDestination() .'/'. $filename));
103104
}
104105
}
105106
/**
@@ -112,16 +113,15 @@ public function uploadImageThumb($thumb)
112113

113114
if ($width > $this->thumbWidth)
114115
{
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));
116+
return Image::make($this->getUploadedFile()->getRealPath())
117+
->resize($this->thumbWidth, $this->thumbHeight, function ($constraint) {
118+
$constraint->aspectRatio();
119+
})
120+
->save(public_path($this->getDestination() .'/'. $thumb));
120121
}else{
121122
return Image::make($this->getUploadedFile()->getRealPath())
122123
->save(public_path($this->getDestination() .'/'. $thumb));
123124
}
124-
return $thumb;
125125
}
126126

127127
}

src/Uploader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Dosarkz\LaravelUploader;
3-
4-
use UploadedFile;
3+
use Illuminate\Http\UploadedFile;
54

65
abstract class Uploader
76
{

0 commit comments

Comments
 (0)