1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Support\Traits; |
4
|
|
|
|
5
|
|
|
use App\Support\Helper; |
6
|
|
|
use App\Support\Image\Filters\Resize; |
7
|
|
|
use Exception; |
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
9
|
|
|
|
10
|
|
|
trait ImageStorage |
11
|
|
|
{ |
12
|
|
|
use AssetHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Store image file. |
16
|
|
|
* |
17
|
|
|
* @param mixed $file |
18
|
|
|
* @param string|null $identifier |
19
|
|
|
* @return string|null The stored path |
20
|
|
|
*/ |
21
|
|
|
protected function storeImageFile($file, $identifier = null) |
22
|
|
|
{ |
23
|
|
|
if ($file instanceof UploadedFile && ! $file->isValid()) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
try { |
28
|
|
|
$image = app('image') |
29
|
|
|
->make($file) |
30
|
|
|
->filter($this->getImageFilter($identifier)) |
31
|
|
|
->encode( |
32
|
|
|
$this->getImageEncodingFormat($identifier), |
33
|
|
|
$this->getImageEncodingQuality($identifier) |
34
|
|
|
); |
35
|
|
|
} catch (Exception $e) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$path = trim(trim($this->getImageDirectory($identifier), '/').'/'.trim($this->getImageFilename($image, $identifier), '/'), '/'); |
40
|
|
|
|
41
|
|
|
if ($path && $this->getFilesystem($identifier)->put($path, $image)) { |
42
|
|
|
return $path; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get image filter. |
48
|
|
|
* |
49
|
|
|
* @see http://image.intervention.io/api/filter |
50
|
|
|
* |
51
|
|
|
* @param string|null $identifier |
52
|
|
|
*/ |
53
|
|
|
protected function getImageFilter($identifier = null) |
54
|
|
|
{ |
55
|
|
|
return (new Resize)->width(1024); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get image encoding format. |
60
|
|
|
* |
61
|
|
|
* @see http://image.intervention.io/api/encode |
62
|
|
|
* |
63
|
|
|
* @param string|null $identifier |
64
|
|
|
* @return string|null |
65
|
|
|
*/ |
66
|
|
|
protected function getImageEncodingFormat($identifier = null) |
67
|
|
|
{ |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get image encoding quality. |
72
|
|
|
* |
73
|
|
|
* @see http://image.intervention.io/api/encode |
74
|
|
|
* |
75
|
|
|
* @param string|null $identifier |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
protected function getImageEncodingQuality($identifier = null) |
79
|
|
|
{ |
80
|
|
|
return 90; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get image output directory. |
85
|
|
|
* |
86
|
|
|
* @param string|null $identifier |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
protected function getImageDirectory($identifier = null) |
90
|
|
|
{ |
91
|
|
|
return 'images/'.dechex((int) date('Y') - 2010).'/'.dechex(date('W')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get image output filename. |
96
|
|
|
* |
97
|
|
|
* @param \Intervention\Image\Image $image |
98
|
|
|
* @param string|null $identifier |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function getImageFilename($image, $identifier = null) |
102
|
|
|
{ |
103
|
|
|
return md5($image).Helper::fileExtensionForMimeType($image->mime(), '.'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|