@@ -31,6 +31,13 @@ class Resizer
3131 */
3232 protected $ options = [];
3333
34+ /**
35+ * The list of cacheable options (i.e. those specified, excl defaults)
36+ *
37+ * @var array
38+ */
39+ protected $ cacheableOptions = [];
40+
3441 /**
3542 * The original image resource
3643 *
@@ -66,6 +73,13 @@ class Resizer
6673 */
6774 protected $ formatCache = [];
6875
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+
6983 /**
7084 * Construct the resizer class
7185 *
@@ -128,14 +142,18 @@ public function setImage(string $image = null, bool $doNotModifyPath = false)
128142 }
129143 }
130144
145+ // Get the domain
146+ $ domain = $ _SERVER ['SERVER_NAME ' ] ?? '' ;
147+ if (empty ($ domain )) {
148+ $ domain = parse_url (url ()->to ('/ ' ), PHP_URL_HOST );
149+ }
150+
131151 // 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 ;
139157 }
140158
141159 // If not an absolute path, set it to an absolute path
@@ -159,9 +177,26 @@ public function getImagePath(): string
159177 {
160178 $ image = $ this ->image ;
161179
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 )), '/ ' );
165200 }
166201
167202 return $ image ;
@@ -187,8 +222,21 @@ protected function getDefaultImage(): string
187222 $ image = Settings::getDefaultImageNotFound (true );
188223 }
189224
190- // Use the default Image Not Found background, mode and quality
225+ // Use the default Image Not Found background
191226 $ 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
192240 $ this ->options ['mode ' ] = Settings::get ('image_not_found_mode ' , 'cover ' );
193241 $ this ->options ['quality ' ] = Settings::get ('image_not_found_quality ' , 65 );
194242
@@ -236,9 +284,22 @@ public function initResource()
236284 ]);
237285
238286 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+
239297 $ this ->im = $ this ->original = Image::make ($ this ->image );
240298 } 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+ }
242303 }
243304 }
244305
@@ -327,6 +388,9 @@ private function initOptions(array $options = null)
327388 'format ' => Settings::get ('format ' ),
328389 ];
329390
391+ // Don't cache defaults
392+ $ this ->cacheableOptions = $ options ;
393+
330394 // Merge defaults and options
331395 $ this ->options = array_merge ($ defaults , $ options );
332396
@@ -346,6 +410,16 @@ public function getOptions(): array
346410 return $ this ->options ;
347411 }
348412
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+
349423 /**
350424 * Get the absolute physical path of the image
351425 *
@@ -500,6 +574,9 @@ public function resizePermalink(string $identifier, int $width = null, int $heig
500574 $ options ['width ' ] = $ width ;
501575 $ options ['height ' ] = $ height ;
502576
577+ // Don't need to cache this
578+ unset($ options ['permalink ' ]);
579+
503580 // Set options, set hash for cache
504581 $ this ->initOptions ($ options );
505582
@@ -708,6 +785,30 @@ public function doResize()
708785 return $ this ;
709786 }
710787
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+
711812 /**
712813 * Detect format of input file for default export format
713814 *
@@ -731,8 +832,21 @@ public function detectFormat(bool $useNewFormat = false): array
731832 if ($ useNewFormat && !empty ($ this ->options ['format ' ]) && ($ this ->options ['format ' ] !== 'auto ' )) {
732833 $ format = $ this ->options ['format ' ];
733834 } 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+ }
736850 }
737851
738852 // For the most part, the mime is the format: image/{format}
0 commit comments