1
|
|
|
<?php namespace Anomaly\Streams\Platform\Image; |
2
|
|
|
|
3
|
|
|
use Anomaly\FilesModule\File\Contract\FileInterface; |
4
|
|
|
use Anomaly\Streams\Platform\Application\Application; |
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ImagePaths |
10
|
|
|
* |
11
|
|
|
* @link http://pyrocms.com/ |
12
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
13
|
|
|
* @author Ryan Thompson <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class ImagePaths |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Predefined paths. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $paths = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The config repository. |
27
|
|
|
* |
28
|
|
|
* @var Repository |
29
|
|
|
*/ |
30
|
|
|
protected $config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The request object. |
34
|
|
|
* |
35
|
|
|
* @var Request |
36
|
|
|
*/ |
37
|
|
|
protected $request; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The application object. |
41
|
|
|
* |
42
|
|
|
* @var Application |
43
|
|
|
*/ |
44
|
|
|
protected $application; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new ImagePaths instance. |
48
|
|
|
* |
49
|
|
|
* @param Repository $config |
50
|
|
|
* @param Request $request |
51
|
|
|
* @param Application $application |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function __construct(Repository $config, Request $request, Application $application) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->config = $config; |
56
|
|
|
$this->request = $request; |
57
|
|
|
$this->application = $application; |
58
|
|
|
|
59
|
|
|
$this->paths = $config->get('streams::images.paths', []); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the paths. |
64
|
|
|
* |
65
|
|
|
* @return array|mixed |
66
|
|
|
*/ |
67
|
|
|
public function getPaths() |
68
|
|
|
{ |
69
|
|
|
return $this->paths; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set the paths. |
74
|
|
|
* |
75
|
|
|
* @param array $paths |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function setPaths(array $paths) |
79
|
|
|
{ |
80
|
|
|
$this->paths = $paths; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add an image path hint. |
87
|
|
|
* |
88
|
|
|
* @param $namespace |
89
|
|
|
* @param $path |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function addPath($namespace, $path) |
93
|
|
|
{ |
94
|
|
|
$this->paths[$namespace] = rtrim($path, '/\\'); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the real path for a given path. |
101
|
|
|
* |
102
|
|
|
* @param $path |
103
|
|
|
* @return string |
104
|
|
|
* @throws \Exception |
105
|
|
|
*/ |
106
|
|
|
public function realPath($path) |
107
|
|
|
{ |
108
|
|
|
if (str_contains($path, '::')) { |
109
|
|
|
list($namespace, $path) = explode('::', $path); |
110
|
|
|
|
111
|
|
|
if (!isset($this->paths[$namespace])) { |
112
|
|
|
throw new \Exception("Path hint [{$namespace}::{$path}] does not exist!"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return rtrim($this->paths[$namespace], '/') . '/' . $path; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $path; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return the output path for an image. |
123
|
|
|
* |
124
|
|
|
* @param $path |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function outputPath(Image $image) |
128
|
|
|
{ |
129
|
|
|
$path = $image->getImage(); |
130
|
|
|
|
131
|
|
|
if ($path instanceof FileInterface) { |
|
|
|
|
132
|
|
|
$path = $path->path(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/* |
136
|
|
|
* If the path is already public |
137
|
|
|
* then just use it as it is. |
138
|
|
|
*/ |
139
|
|
|
if (str_contains($path, public_path())) { |
140
|
|
|
return str_replace(public_path(), '', $path); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/* |
144
|
|
|
* If the path is a file or file path then |
145
|
|
|
* put it in /app/{$application}/files/disk/folder/filename.ext |
146
|
|
|
*/ |
147
|
|
|
if (is_string($path) && str_is('*://*', $path)) { |
148
|
|
|
|
149
|
|
|
$application = $this->application->getReference(); |
150
|
|
|
|
151
|
|
|
list($disk, $folder, $filename) = explode('/', str_replace('://', '/', $path)); |
152
|
|
|
|
153
|
|
View Code Duplication |
if ($image->getAlterations() || $image->getQuality()) { |
|
|
|
|
154
|
|
|
$filename = md5( |
155
|
|
|
var_export([$path, $image->getAlterations()], true) . $image->getQuality() |
156
|
|
|
) . '.' . $image->getExtension(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($rename = $image->getFilename()) { |
160
|
|
|
|
161
|
|
|
$filename = $rename; |
162
|
|
|
|
163
|
|
|
if (strpos($filename, DIRECTORY_SEPARATOR)) { |
164
|
|
|
$directory = null; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return "/app/{$application}/files/{$disk}/{$folder}/{$filename}"; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/* |
172
|
|
|
* Get the real path relative to our installation. |
173
|
|
|
*/ |
174
|
|
|
$path = str_replace(base_path(), '', $this->realPath($path)); |
175
|
|
|
|
176
|
|
|
/* |
177
|
|
|
* Build out path parts. |
178
|
|
|
*/ |
179
|
|
|
$filename = basename($path); |
180
|
|
|
$directory = ltrim(dirname($path), '/\\') . '/'; |
181
|
|
|
$application = $this->application->getReference(); |
182
|
|
|
|
183
|
|
View Code Duplication |
if ($image->getAlterations() || $image->getQuality()) { |
|
|
|
|
184
|
|
|
$filename = md5( |
185
|
|
|
var_export([$path, $image->getAlterations()], true) . $image->getQuality() |
186
|
|
|
) . '.' . $image->getExtension(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($rename = $image->getFilename()) { |
190
|
|
|
$directory = null; |
191
|
|
|
$filename = ltrim($rename, '/\\'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return "/app/{$application}/assets/{$directory}{$filename}"; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|