@@ -31,6 +31,13 @@ class Resizer
31
31
*/
32
32
protected $ options = [];
33
33
34
+ /**
35
+ * The list of cacheable options (i.e. those specified, excl defaults)
36
+ *
37
+ * @var array
38
+ */
39
+ protected $ cacheableOptions = [];
40
+
34
41
/**
35
42
* The original image resource
36
43
*
@@ -66,6 +73,13 @@ class Resizer
66
73
*/
67
74
protected $ formatCache = [];
68
75
76
+ /**
77
+ * Can this Resizer default the image (when the image doesn't exist, for example)
78
+ *
79
+ * @var boolean
80
+ */
81
+ protected $ allowDefaultImage = true ;
82
+
69
83
/**
70
84
* Construct the resizer class
71
85
*
@@ -128,14 +142,18 @@ public function setImage(string $image = null, bool $doNotModifyPath = false)
128
142
}
129
143
}
130
144
145
+ // Get the domain
146
+ $ domain = $ _SERVER ['SERVER_NAME ' ] ?? '' ;
147
+ if (empty ($ domain )) {
148
+ $ domain = parse_url (url ()->to ('/ ' ), PHP_URL_HOST );
149
+ }
150
+
131
151
// Check if the image is an absolute url to the same server, if so get the storage path of the image
132
- if (!empty ($ _SERVER ['SERVER_NAME ' ])) {
133
- $ regex = '/^(?:https?:\/\/)? ' . $ _SERVER ['SERVER_NAME ' ] . '(?::\d+)?\/(.+)$/ ' ;
134
- if (preg_match ($ regex , $ image , $ m )) {
135
- // Convert spaces, not going to urldecode as it will mess with pluses
136
- $ image = base_path (str_replace ('%20 ' , ' ' , $ m [1 ]));
137
- $ absolutePath = true ;
138
- }
152
+ $ regex = '/^(?:https?:\/\/)? ' . $ domain . '(?::\d+)?\/(.+)$/ ' ;
153
+ if (preg_match ($ regex , $ image , $ m )) {
154
+ // Convert spaces, not going to urldecode as it will mess with pluses
155
+ $ image = base_path (str_replace ('%20 ' , ' ' , $ m [1 ]));
156
+ $ absolutePath = true ;
139
157
}
140
158
141
159
// If not an absolute path, set it to an absolute path
@@ -159,9 +177,26 @@ public function getImagePath(): string
159
177
{
160
178
$ image = $ this ->image ;
161
179
162
- // If the image is invalid, default to Image Not Found
163
- if ($ image === null || $ image === '' || !file_exists ($ image )) {
164
- $ image = $ this ->getDefaultImage ();
180
+ if ($ this ->allowDefaultImage ) {
181
+ // If the image is invalid, default to Image Not Found
182
+ if ($ image === null || $ image === '' || !file_exists ($ image )) {
183
+ $ image = $ this ->getDefaultImage ();
184
+ }
185
+ }
186
+
187
+ return $ image ;
188
+ }
189
+
190
+ /**
191
+ * Get the path to the image (relative path preferred)
192
+ */
193
+ public function getImagePathRelativePreferred (): string
194
+ {
195
+ $ image = $ this ->getImagePath ();
196
+ $ base = rtrim (base_path (), '/ ' );
197
+
198
+ if (Str::startsWith ($ image , $ base )) {
199
+ $ image = ltrim (substr ($ image , strlen ($ base )), '/ ' );
165
200
}
166
201
167
202
return $ image ;
@@ -187,8 +222,21 @@ protected function getDefaultImage(): string
187
222
$ image = Settings::getDefaultImageNotFound (true );
188
223
}
189
224
190
- // Use the default Image Not Found background, mode and quality
225
+ // Use the default Image Not Found background
191
226
$ this ->options ['background ' ] = Settings::get ('image_not_found_background ' , '#fff ' );
227
+
228
+ // If the 404 image should be transparent then remove the default background
229
+ if (Settings::get ('image_not_found_transparent ' )) {
230
+ unset($ this ->options ['background ' ]);
231
+ }
232
+
233
+ // If the 404 image format is not auto then apply this format
234
+ $ format = Settings::get ('image_not_found_format ' , 'auto ' );
235
+ if ($ format !== 'auto ' ) {
236
+ $ this ->options ['format ' ] = $ format ;
237
+ }
238
+
239
+ // Apply 404 image mode and quality
192
240
$ this ->options ['mode ' ] = Settings::get ('image_not_found_mode ' , 'cover ' );
193
241
$ this ->options ['quality ' ] = Settings::get ('image_not_found_quality ' , 65 );
194
242
@@ -236,9 +284,22 @@ public function initResource()
236
284
]);
237
285
238
286
try {
287
+ // Default the image if it's a directory
288
+ if (is_dir ($ this ->image )) {
289
+ throw new \Exception ('Image file does not exist (is directory) ' );
290
+ }
291
+
292
+ // Default the image if it doesn't exist
293
+ if (is_dir ($ this ->image )) {
294
+ throw new \Exception ('Image file does not exist (not found) ' );
295
+ }
296
+
239
297
$ this ->im = $ this ->original = Image::make ($ this ->image );
240
298
} catch (\Exception $ e ) {
241
- $ this ->im = $ this ->original = Image::make ($ this ->getDefaultImage ());
299
+ if ($ this ->allowDefaultImage ) {
300
+ $ this ->setFormatCache ([]);
301
+ $ this ->im = $ this ->original = Image::make ($ this ->image = $ this ->getDefaultImage ());
302
+ }
242
303
}
243
304
}
244
305
@@ -327,6 +388,9 @@ private function initOptions(array $options = null)
327
388
'format ' => Settings::get ('format ' ),
328
389
];
329
390
391
+ // Don't cache defaults
392
+ $ this ->cacheableOptions = $ options ;
393
+
330
394
// Merge defaults and options
331
395
$ this ->options = array_merge ($ defaults , $ options );
332
396
@@ -346,6 +410,16 @@ public function getOptions(): array
346
410
return $ this ->options ;
347
411
}
348
412
413
+ /**
414
+ * Get the options defined in this resizer, excluding defaults
415
+ *
416
+ * @return array
417
+ */
418
+ public function getCacheableOptions (): array
419
+ {
420
+ return $ this ->cacheableOptions ;
421
+ }
422
+
349
423
/**
350
424
* Get the absolute physical path of the image
351
425
*
@@ -500,6 +574,9 @@ public function resizePermalink(string $identifier, int $width = null, int $heig
500
574
$ options ['width ' ] = $ width ;
501
575
$ options ['height ' ] = $ height ;
502
576
577
+ // Don't need to cache this
578
+ unset($ options ['permalink ' ]);
579
+
503
580
// Set options, set hash for cache
504
581
$ this ->initOptions ($ options );
505
582
@@ -708,6 +785,30 @@ public function doResize()
708
785
return $ this ;
709
786
}
710
787
788
+ /**
789
+ * Prevent the Resizer from defaulting the image
790
+ *
791
+ * @return $this
792
+ */
793
+ public function preventDefaultImage ()
794
+ {
795
+ $ this ->allowDefaultImage = false ;
796
+
797
+ return $ this ;
798
+ }
799
+
800
+ /**
801
+ * Prevent the Resizer from defaulting the image
802
+ *
803
+ * @return $this
804
+ */
805
+ public function allowDefaultImage ()
806
+ {
807
+ $ this ->allowDefaultImage = false ;
808
+
809
+ return $ this ;
810
+ }
811
+
711
812
/**
712
813
* Detect format of input file for default export format
713
814
*
@@ -731,8 +832,21 @@ public function detectFormat(bool $useNewFormat = false): array
731
832
if ($ useNewFormat && !empty ($ this ->options ['format ' ]) && ($ this ->options ['format ' ] !== 'auto ' )) {
732
833
$ format = $ this ->options ['format ' ];
733
834
} else {
734
- $ format = File::mimeType ($ this ->getImagePath ());
735
- $ format = Str::after ($ format , '/ ' );
835
+ if (File::exists ($ path = $ this ->getImagePath ())) {
836
+ $ format = File::mimeType ($ path );
837
+ $ format = Str::after ($ format , '/ ' );
838
+ } else {
839
+ // If the file doesn't exist then inherit from the new format
840
+ $ format = $ this ->options ['format ' ];
841
+ // If the new format is automatic, then use the default 404 image format (otherwise jpg)
842
+ if ($ format === 'auto ' ) {
843
+ $ format = Settings::get ('image_not_found_format ' , 'auto ' );
844
+ // And lastly, if you have nothing defined you can get a JPG
845
+ if ($ format === 'auto ' ) {
846
+ $ format = 'jpg ' ;
847
+ }
848
+ }
849
+ }
736
850
}
737
851
738
852
// For the most part, the mime is the format: image/{format}
0 commit comments