Skip to content

Commit 825407a

Browse files
committed
config, optimize func
1 parent 5380d65 commit 825407a

7 files changed

+116
-44
lines changed

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,18 @@
1818
"psr-4": {
1919
"SergeyMiracle\\Uploadable\\": "src/"
2020
}
21+
},
22+
"extra": {
23+
"laravel": {
24+
"providers": [
25+
"SergeyMiracle\\Uploadable\\UploadableServiceProvider"
26+
]
27+
}
28+
},
29+
"suggest": {
30+
"spatie/image-optimizer": "Easily optimize images using PHP"
31+
},
32+
"config": {
33+
"sort-packages": true
2134
}
2235
}

config/uploadable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'root' => '/upload/', // slashes in start and end of path required
5+
6+
'images' => [
7+
'optimize' => false, // optimize images using spatie/image-optimizer
8+
'max_heigth' => 900 // auto resize image to a height when optimizing and constrain aspect ratio (auto width)
9+
]
10+
];

readme.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class Post extends Model {
1515
protected $uploadables = ['featured_image'];
1616
1717
protected $upload_dir = '.images';
18-
18+
19+
// optional
1920
protected $cropped = [
2021
'featured_image' => ['width' => 25, 'height' => 25]
2122
];
@@ -38,22 +39,25 @@ Setup 'upload' disk in `config/filesystems.php`
3839
3940
'upload' => [
4041
'driver' => 'local',
41-
'root' => public_path('upload'),
42+
'root' => public_path(config('uploadable.root')),
4243
'visibility' => 'public',
4344
],
4445
4546
...
4647
]
4748
```
4849

50+
4951
On saving array of files, a json encoded string saved in database.
5052

5153
Includes uploadable trait for controller - `UploadableControllerTrait`
5254
UploadableControllerTrait has two methods: `moveFile` and `moveImage`
53-
Both expects `UploadedFile` instance. moveImage methods crops image.
55+
Both expects `UploadedFile` instance. moveImage method crops image.
5456

5557
```
5658
$this->moveFile($request->file('image'))
5759
5860
$this->moveImage($request->file('image'), $width, $height)
59-
```
61+
```
62+
63+
If you want to use image optimization install `spatie/image-optimizer` and set optimization to true in config.

src/UploadableControllerTrait.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
trait UploadableControllerTrait
99
{
10+
use UtilsTrait;
11+
1012
/**
1113
* Save file on disk
1214
*
@@ -21,7 +23,13 @@ private function moveFile($file)
2123
throw new FileException($e->getMessage());
2224
}
2325

24-
return '/upload/' . $path;
26+
$path = config('uploadable.root') . $path;
27+
28+
if (config('uploadable.images.optimize') && getimagesize(public_path($path))) {
29+
$this->perfomOptimize(public_path($path));
30+
}
31+
32+
return $path;
2533
}
2634

2735
/**
@@ -40,26 +48,6 @@ private function moveImage($file, $width = 250, $height = 250)
4048
return $path;
4149
}
4250

43-
/**
44-
* @return string
45-
*/
46-
private function getUploadDir()
47-
{
48-
$date = new Carbon();
49-
return $this->upload_dir . DIRECTORY_SEPARATOR . $date->year . DIRECTORY_SEPARATOR . $date->month;
50-
}
51-
52-
/**
53-
* @param $file string
54-
* @return string
55-
*/
56-
private function createFileName($file)
57-
{
58-
$path = pathinfo($file);
59-
60-
return uniqid() . '_' . str_slug($path['filename'], '_') . '.' . $path['extension'];
61-
}
62-
6351
/**
6452
* Remove file
6553
*

src/UploadableModelTrait.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use Carbon\Carbon;
66
use Symfony\Component\HttpFoundation\File\Exception\FileException;
7+
use Spatie\ImageOptimizer\OptimizerChainFactory;
78

89
trait UploadableModelTrait
910
{
11+
use UtilsTrait;
12+
1013
/**
1114
* Boot the trait's observer.
1215
*
@@ -88,9 +91,16 @@ private function moveFile($file)
8891
throw new FileException($e->getMessage());
8992
}
9093

91-
return '/upload/' . $path;
94+
$path = config('uploadable.root') . $path;
95+
96+
if (config('uploadable.images.optimize') && getimagesize(public_path($path))) {
97+
$this->perfomOptimize(public_path($path));
98+
}
99+
100+
return $path;
92101
}
93102

103+
94104
/**
95105
* Uploadable fields getter.
96106
*
@@ -124,16 +134,6 @@ private function checkForUploadables()
124134
}
125135
}
126136

127-
/**
128-
* @param $file string
129-
* @return string
130-
*/
131-
private function createFileName($file)
132-
{
133-
$path = pathinfo($file);
134-
135-
return uniqid() . '_' . str_slug($path['filename'], '_') . '.' . $path['extension'];
136-
}
137137

138138
/**
139139
* Delete an existing 'uploadable' file in
@@ -169,14 +169,6 @@ private function deleteExisting($key)
169169
return false;
170170
}
171171

172-
/**
173-
* @return string
174-
*/
175-
public function getUploadDir()
176-
{
177-
$date = new Carbon();
178-
return $this->upload_dir . DIRECTORY_SEPARATOR . $date->year . DIRECTORY_SEPARATOR . $date->month;
179-
}
180172

181173
/**
182174
* @param string $upload_dir

src/UploadableServiceProvider

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace SergeyMiracle\Uploadable;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class UploadableServiceProvider extends ServiceProvider
8+
{
9+
public function boot()
10+
{
11+
if ($this->app->runningInConsole()) {
12+
$this->publishes([
13+
__DIR__.'/../config/uploadable.php' => config_path('uploadable.php'),
14+
], 'config');
15+
}
16+
}
17+
18+
19+
public function register()
20+
{
21+
$this->mergeConfigFrom(__DIR__.'/../config/uploadable.php', 'uploadable');
22+
}
23+
}

src/UtilsTrait.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace SergeyMiracle\Uploadable;
4+
5+
trait UtilsTrait
6+
{
7+
private function perfomOptimize($path)
8+
{
9+
[$width, $height] = getimagesize($path);
10+
11+
if ($height > config('uploadable.images.max_height')) {
12+
\Image::make($path)->resize(null, config('uploadable.images.max_height'), function ($constraint) {
13+
$constraint->aspectRatio();
14+
})->save();
15+
}
16+
17+
$optimizerChain = OptimizerChainFactory::create();
18+
$optimizerChain->optimize($path);
19+
}
20+
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getUploadDir()
26+
{
27+
$date = new Carbon();
28+
return $this->upload_dir . DIRECTORY_SEPARATOR . $date->year . DIRECTORY_SEPARATOR . $date->month;
29+
}
30+
31+
32+
/**
33+
* @param $file string
34+
* @return string
35+
*/
36+
private function createFileName($file)
37+
{
38+
$path = pathinfo($file);
39+
40+
return uniqid() . '_' . str_slug($path['filename'], '_') . '.' . $path['extension'];
41+
}
42+
}

0 commit comments

Comments
 (0)