1
|
|
|
<?php namespace GeneaLabs\LaravelImagery; |
2
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelImagery\Jobs\RenderDerivativeImages; |
4
|
|
|
use Intervention\Image\ImageManager; |
5
|
|
|
use Jenssegers\Model\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
class Image extends Model |
9
|
|
|
{ |
10
|
|
|
//TODO: this class needs serious refactoring!!! |
11
|
|
|
public function __construct( |
12
|
|
|
string $source, |
13
|
|
|
string $width = null, |
14
|
|
|
string $height = null, |
15
|
|
|
Collection $htmlAttributes = null, |
16
|
|
|
Collection $options = null |
17
|
|
|
) { |
18
|
|
|
parent::__construct(); |
19
|
|
|
|
20
|
|
|
$this->createCacheFolderIfMissing(); |
21
|
|
|
|
22
|
|
|
$this->originalHeight = $height; |
|
|
|
|
23
|
|
|
$this->originalWidth = $width; |
|
|
|
|
24
|
|
|
$this->htmlAttributes = $htmlAttributes; |
|
|
|
|
25
|
|
|
$this->heightIsPercentage = str_contains($height, '%'); |
|
|
|
|
26
|
|
|
$this->widthIsPercentage = str_contains($width, '%'); |
|
|
|
|
27
|
|
|
$this->source = $source; |
|
|
|
|
28
|
|
|
$this->image = (new ImageManager)->make($source); |
|
|
|
|
29
|
|
|
$this->height = intval($height); |
|
|
|
|
30
|
|
|
$this->width = intval($width); |
|
|
|
|
31
|
|
|
$this->originalPath = public_path(config('genealabs-laravel-imagery.storage-folder') . basename($source)); |
|
|
|
|
32
|
|
|
$this->alwaysPreserveAspectRatio = $options->get('alwaysPreserveAspectRatio', true); |
|
|
|
|
33
|
|
|
$this->doNotCreateDerivativeImages = $options->get('doNotCreateDerivativeImages', false); |
|
|
|
|
34
|
|
|
$this->overrideScreenConstraint = $options->get('overrideScreenConstraint', false); |
|
|
|
|
35
|
|
|
$this->screenConstraintMethod = $options->get('screenConstraintMethod', 'contain'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if ($this->sourceIsUrl($source)) { |
|
|
|
|
38
|
|
|
$this->image->save($this->originalPath); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->resizeImage($this->width, $this->height, $this->alwaysPreserveAspectRatio); |
42
|
|
|
|
43
|
|
|
if (! $this->doNotCreateDerivativeImages) { |
44
|
|
|
$job = (new RenderDerivativeImages($this->source))->onQueue('imagery'); |
45
|
|
|
dispatch($job); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function resizeImage(int $width, int $height, bool $alwaysPreserveAspect = false) |
50
|
|
|
{ |
51
|
|
|
if (! $height || ! $width) { |
52
|
|
|
$height = $height ?: $this->image->getHeight(); |
53
|
|
|
$width = $width ?: $this->image->getWidth(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$screenHeight = $_COOKIE['screenWidth'] ?? null; |
|
|
|
|
57
|
|
|
$screenWidth = $_COOKIE['screenHeight'] ?? null; |
58
|
|
|
$screenHeight = $screenWidth ? intval($screenWidth) : null; |
59
|
|
|
$screenWidth = $screenWidth ? intval($screenWidth) : null; |
60
|
|
|
$height = $this->determineHeight($height, $screenHeight); |
61
|
|
|
$width = $this->determineWidth($width, $screenWidth); |
62
|
|
|
|
63
|
|
|
if (! $height && ! $width) { |
64
|
|
|
$height = $this->image->height(); |
65
|
|
|
$width = $this->image->width(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$maxHeight = $this->determineMaxHeight($height, $screenHeight, $screenWidth); |
69
|
|
|
$maxWidth = $this->determineMaxWidth($width, $screenHeight, $screenWidth); |
70
|
|
|
|
71
|
|
|
$this->image->resize($maxWidth, $maxHeight, function ($constraint) use ($alwaysPreserveAspect) { |
72
|
|
|
if ($alwaysPreserveAspect || ! $width || ! $height) { |
|
|
|
|
73
|
|
|
$constraint->aspectRatio(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$constraint->upsize(); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
$this->height = $this->image->height(); |
|
|
|
|
80
|
|
|
$this->width = $this->image->width(); |
|
|
|
|
81
|
|
|
$this->storeImage(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
//TODO: refactor to have a single return type, instead of null or int |
85
|
|
View Code Duplication |
protected function determineMaxHeight($height, $screenHeight, $screenWidth) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
if (! $screenHeight || ! $screenWidth) { |
88
|
|
|
return $height; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$maxHeight = $height ?: $this->image->height(); |
92
|
|
|
|
93
|
|
|
if (! $this->overrideScreenConstraint) { |
94
|
|
|
$maxHeight = $screenHeight < $maxHeight ? $screenHeight : $maxHeight; |
95
|
|
|
|
96
|
|
|
if ($this->screenConstraintMethod === 'cover') { |
97
|
|
|
$imageToScreenHeight = $screenHeight / $this->image->height(); |
98
|
|
|
$imageToScreenWidth = $screenWidth / $this->image->width(); |
99
|
|
|
|
100
|
|
|
if ($imageToScreenHeight < $imageToScreenWidth) { |
101
|
|
|
$maxHeight = null; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $maxHeight; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
//TODO: refactor to have a single return type, instead of null or int |
110
|
|
View Code Duplication |
protected function determineMaxWidth($width, $screenHeight, $screenWidth) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
if (! $screenHeight || ! $screenWidth) { |
113
|
|
|
return $width; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$maxWidth = $width ?: $this->image->width(); |
117
|
|
|
|
118
|
|
|
if (! $this->overrideScreenConstraint) { |
119
|
|
|
$maxWidth = $screenWidth < $maxWidth ? $screenWidth : $maxWidth; |
120
|
|
|
|
121
|
|
|
if ($this->screenConstraintMethod === 'cover') { |
122
|
|
|
$imageToScreenHeight = $screenHeight / $this->image->height(); |
123
|
|
|
$imageToScreenWidth = $screenWidth / $this->image->width(); |
124
|
|
|
if ($imageToScreenHeight > $imageToScreenWidth) { |
125
|
|
|
$maxWidth = null; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $maxWidth; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function determineHeight($height, $screenHeight) : int |
134
|
|
|
{ |
135
|
|
|
if ($screenHeight && $height && $this->heightIsPercentage) { |
136
|
|
|
return $screenHeight * ($height / 100); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $height; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function determineWidth($width, $screenWidth) : int |
143
|
|
|
{ |
144
|
|
|
if ($screenWidth && $width && $this->widthIsPercentage) { |
145
|
|
|
return $screenWidth * ($width / 100); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $width; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function sourceIsUrl() : bool |
152
|
|
|
{ |
153
|
|
|
return collect(parse_url($this->source))->has('scheme'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function storeImage() |
157
|
|
|
{ |
158
|
|
|
$this->image |
159
|
|
|
->save(public_path(config('genealabs-laravel-imagery.storage-folder') . $this->fileName)); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getFileNameAttribute() : string |
163
|
|
|
{ |
164
|
|
|
$pathParts = pathinfo($this->source); |
165
|
|
|
$fileName = $pathParts['filename']; |
166
|
|
|
$extension = $pathParts['extension'] ?? ''; |
167
|
|
|
$extension = $extension ? ".{$extension}" : ''; |
168
|
|
|
|
169
|
|
|
if ($this->width || $this->height) { |
170
|
|
|
$fileName .= "_{$this->width}x{$this->height}"; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return "{$fileName}{$extension}"; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getImgAttribute() : string |
177
|
|
|
{ |
178
|
|
|
$scriptUrl = mix('js/cookie.js', 'genealabs-laravel-imagery'); |
179
|
|
|
$attributes = ''; |
180
|
|
|
|
181
|
|
|
$attributes = $this->htmlAttributes->map(function ($value, $attribute) use (&$attributes) { |
|
|
|
|
182
|
|
|
return " {$attribute}=\"{$value}\""; |
183
|
|
|
})->implode(''); |
184
|
|
|
|
185
|
|
|
return "<img src=\"{$this->url}\" |
|
|
|
|
186
|
|
|
width=\"{$this->originalWidth}\" |
187
|
|
|
height=\"{$this->originalHeight}\"{{ $attributes }} |
188
|
|
|
><script src=\"{$scriptUrl}\"></script>"; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getOriginalUrlAttribute() : string |
192
|
|
|
{ |
193
|
|
|
return asset(config('genealabs-laravel-imagery.storage-folder') . $this->fileName); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getPathAttribute() : string |
197
|
|
|
{ |
198
|
|
|
return public_path(config('genealabs-laravel-imagery.storage-folder') . $this->fileName); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function getPictureAttribute() : string |
202
|
|
|
{ |
203
|
|
|
//TODO: implement img tag attributes, move script to middleware injector |
204
|
|
|
$scriptUrl = mix('js/cookie.js', 'genealabs-laravel-imagery'); |
205
|
|
|
$sources = ''; |
206
|
|
|
|
207
|
|
|
foreach (array_reverse(config('genealabs-laravel-imagery.size-presets')) as $sizePreset) { |
208
|
|
|
$image = (new Imagery)->conjure( |
209
|
|
|
$this->source, |
210
|
|
|
$sizePreset, |
211
|
|
|
$sizePreset, |
212
|
|
|
[], |
213
|
|
|
['doNotCreateDerivativeImages' => true] |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
if ($sizePreset < $this->width || $sizePreset < $this->height) { |
217
|
|
|
$sources .= "<source srcset=\"{$image->url}\" media=\"(min-width: {$sizePreset}px)\">"; |
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return " |
222
|
|
|
<picture> |
223
|
|
|
{$sources} |
224
|
|
|
<img src=\"{$this->url}\"> |
225
|
|
|
</picture> |
226
|
|
|
<script src=\"{$scriptUrl}\"></script> |
227
|
|
|
"; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function getUrlAttribute() : string |
231
|
|
|
{ |
232
|
|
|
return asset(config('genealabs-laravel-imagery.storage-folder') . $this->fileName); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
protected function createCacheFolderIfMissing() |
236
|
|
|
{ |
237
|
|
|
app('filesystem')->disk('public')->makeDirectory(config('genealabs-laravel-imagery.storage-folder')); |
238
|
|
|
|
239
|
|
|
if (! file_exists(public_path(config('genealabs-laravel-imagery.storage-folder')))) { |
240
|
|
|
symlink( |
241
|
|
|
rtrim(storage_path('app/public/' . config('genealabs-laravel-imagery.storage-folder')), '/'), |
242
|
|
|
rtrim(public_path(config('genealabs-laravel-imagery.storage-folder')), '/') |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|