Skip to content

Commit 1c0a196

Browse files
committed
initial commit
0 parents  commit 1c0a196

File tree

6 files changed

+279
-0
lines changed

6 files changed

+279
-0
lines changed

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "dosarkz/laravel-uploader",
3+
"description": "Uploading Class for laravel 5",
4+
"authors": [
5+
{
6+
"name": "Yezhan Ashenov",
7+
"email": "ashenov.e@gmail.com"
8+
}
9+
],
10+
"minimum-stability": "dev",
11+
"require": {
12+
"intervention/image": "^2.3"
13+
},
14+
"autoload": {
15+
"classmap": [
16+
"src/Facade",
17+
"src/Config",
18+
"src/Provider"
19+
],
20+
"psr-4": {
21+
"Dosarkz\\LaravelUploader\\": "src/"
22+
}
23+
}
24+
}

src/Config/laraveluploader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'thumbWidth' => 100,
5+
6+
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Dosarkz\LaravelUploader\Facade;
3+
4+
use Illuminate\Support\Facades\Facade;
5+
6+
class LaravelUploaderFacade extends Facade {
7+
/**
8+
* Get the registered name of the component.
9+
*
10+
* @return string
11+
*/
12+
protected static function getFacadeAccessor()
13+
{
14+
return 'laraveluploader';
15+
}
16+
}

src/ImageUploader.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace Dosarkz\LaravelUploader;
3+
4+
use Illuminate\Support\Facades\File;
5+
6+
class ImageUploader extends Uploader
7+
{
8+
public function __construct($image = null)
9+
{
10+
$this->image = $image;
11+
$this->destination = 'uploads/images/';
12+
$this->thumbWidth = 100;
13+
14+
if (!is_dir(public_path($this->destination)))
15+
{
16+
File::makeDirectory(public_path($this->destination), $mode = 0777, true);
17+
}
18+
19+
parent::__construct();
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace Dosarkz\LaravelUploader\Provider;
3+
4+
use Dosarkz\LaravelUploader\ImageUploader;
5+
use Illuminate\Support\Facades\App;
6+
use Illuminate\Support\ServiceProvider;
7+
use Intervention\Image\ImageServiceProvider;
8+
9+
class LaravelUploaderServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Bootstrap the application services.
13+
*
14+
* @return void
15+
*/
16+
public function boot()
17+
{
18+
$this->publishes([
19+
__DIR__ . '/config/laraveluploader.php' => config_path('laraveluploader.php'),
20+
]);
21+
22+
23+
}
24+
/**
25+
* Register the application services.
26+
*
27+
* @return void
28+
*/
29+
public function register()
30+
{
31+
32+
33+
$this->app->bind('Image', 'Intervention\Image\Facades\Image' );
34+
35+
36+
$this->app['laraveluploader'] = $this->app->share(function($app)
37+
{
38+
return new ImageUploader();
39+
});
40+
}
41+
42+
43+
44+
45+
}

src/Uploader.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
namespace Dosarkz\LaravelUploader;
3+
4+
use Intervention\Image\Facades\Image;
5+
6+
abstract class Uploader
7+
{
8+
public function __construct()
9+
{
10+
$this->setFileName($this->generateFileName());
11+
$this->setThumb($this->generateThumb());
12+
$this->upload();
13+
}
14+
15+
/**
16+
* @var
17+
*/
18+
protected $fileName;
19+
/**
20+
* @var
21+
*/
22+
protected $thumb;
23+
/**
24+
* @var
25+
*/
26+
protected $image;
27+
/**
28+
* @var
29+
*/
30+
protected $destination;
31+
/**
32+
* @var
33+
*/
34+
public $model;
35+
/**
36+
* @var int
37+
*/
38+
public $imageHeight = null;
39+
/**
40+
* @var int
41+
*/
42+
public $imageWidth = 1024;
43+
/**
44+
* @var int
45+
*/
46+
public $thumbHeight = null;
47+
/**
48+
* @var int
49+
*/
50+
public $thumbWidth = 385;
51+
52+
/**
53+
* @return mixed
54+
*/
55+
public function getFileName()
56+
{
57+
return $this->fileName;
58+
}
59+
60+
/**
61+
* @param $fileName
62+
*/
63+
public function setFileName($fileName)
64+
{
65+
$this->fileName = $fileName;
66+
}
67+
68+
/**
69+
* @return string
70+
*/
71+
public function getThumb()
72+
{
73+
return $this->thumb;
74+
}
75+
76+
/**
77+
* @param $thumb
78+
*/
79+
public function setThumb($thumb)
80+
{
81+
$this->thumb = $thumb;
82+
}
83+
84+
/**
85+
* @return string
86+
*/
87+
public function generateFileName()
88+
{
89+
return time() . '_' . rand(0,100) . '.' . $this->image->getClientOriginalExtension();
90+
}
91+
92+
/**
93+
* @return string
94+
*/
95+
public function generateThumb()
96+
{
97+
return 'thumb_' . $this->getFileName();
98+
}
99+
100+
public function upload()
101+
{
102+
if ($this->image->getClientOriginalExtension() == 'gif')
103+
{
104+
$this->uploadFile($this->getFileName());
105+
}else{
106+
$this->uploadImageFile($this->getFileName());
107+
$this->uploadImageThumb($this->getThumb());
108+
}
109+
}
110+
111+
public function uploadFile($filename)
112+
{
113+
return $this->image->move(public_path($this->destination), $filename);
114+
}
115+
116+
/**
117+
* @param $filename
118+
* @param $resize
119+
* @return mixed
120+
*/
121+
public function uploadImageFile($filename, $resize = true)
122+
{
123+
if ($resize)
124+
{
125+
list($width, $height) = getimagesize($this->image->getRealPath());
126+
127+
if ($width > $this->imageWidth)
128+
{
129+
return Image::make($this->image->getRealPath())
130+
->resize($this->imageWidth, $this->imageHeight, function ($constraint) {
131+
$constraint->aspectRatio();
132+
})->save(public_path($this->destination . $filename));
133+
}else {
134+
return Image::make($this->image->getRealPath())
135+
->save(public_path($this->destination . $filename));
136+
}
137+
}else{
138+
return Image::make($this->image->getRealPath())
139+
->save(public_path($this->destination . $filename));
140+
}
141+
142+
}
143+
144+
/**
145+
* @param $thumb
146+
* @return mixed
147+
*/
148+
public function uploadImageThumb($thumb)
149+
{
150+
list($width, $height) = getimagesize($this->image->getRealPath());
151+
152+
if ($width > $this->thumbWidth)
153+
{
154+
Image::make($this->image->getRealPath())->resize($this->thumbWidth, $this->thumbHeight, function ($constraint) {
155+
$constraint->aspectRatio();
156+
})
157+
->save(public_path($this->destination . $thumb));
158+
}else{
159+
return Image::make($this->image->getRealPath())
160+
->save(public_path($this->destination . $thumb));
161+
}
162+
163+
164+
return $thumb;
165+
}
166+
167+
}

0 commit comments

Comments
 (0)