@@ -62,6 +62,13 @@ abstract class BaseHandler implements ImageHandlerInterface
6262 */
6363 protected $ image = null ;
6464
65+ /**
66+ * Whether the image file has been confirmed.
67+ *
68+ * @var bool
69+ */
70+ protected $ verified = false ;
71+
6572 /**
6673 * Image width.
6774 *
@@ -158,6 +165,7 @@ public function withFile(string $path)
158165 // Clear out the old resource so that
159166 // it doesn't try to use a previous image
160167 $ this ->resource = null ;
168+ $ this ->verified = false ;
161169
162170 $ this ->image = new Image ($ path , true );
163171
@@ -177,9 +185,9 @@ protected function ensureResource()
177185 {
178186 if ($ this ->resource === null )
179187 {
180- $ path = $ this ->image ->getPathname ();
188+ $ path = $ this ->image () ->getPathname ();
181189 // if valid image type, make corresponding image resource
182- switch ($ this ->image ->imageType )
190+ switch ($ this ->image () ->imageType )
183191 {
184192 case IMAGETYPE_GIF :
185193 $ this ->resource = imagecreatefromgif ($ path );
@@ -206,6 +214,43 @@ public function getFile()
206214 return $ this ->image ;
207215 }
208216
217+ /**
218+ * Verifies that a file has been supplied and it is an image.
219+ *
220+ * @return Image The image instance
221+ * @throws type ImageException
222+ */
223+ protected function image (): ?Image
224+ {
225+ if ($ this ->verified )
226+ {
227+ return $ this ->image ;
228+ }
229+
230+ // Verify withFile has been called
231+ if (empty ($ this ->image ))
232+ {
233+ throw ImageException::forMissingImage ();
234+ }
235+
236+ // Verify the loaded image is an Image instance
237+ if (! $ this ->image instanceof Image)
238+ {
239+ throw ImageException::forInvalidPath ();
240+ }
241+
242+ // File::__construct has verified the file exists - make sure it is an image
243+ if (! is_int ($ this ->image ->imageType ))
244+ {
245+ throw ImageException::forFileNotSupported ();
246+ }
247+
248+ // Note that the image has been verified
249+ $ this ->verified = true ;
250+
251+ return $ this ->image ;
252+ }
253+
209254 //--------------------------------------------------------------------
210255
211256 /**
@@ -236,7 +281,7 @@ public function getResource()
236281 public function resize (int $ width , int $ height , bool $ maintainRatio = false , string $ masterDim = 'auto ' )
237282 {
238283 // If the target width/height match the source, then we have nothing to do here.
239- if ($ this ->image ->origWidth === $ width && $ this ->image ->origHeight === $ height )
284+ if ($ this ->image () ->origWidth === $ width && $ this ->image () ->origHeight === $ height )
240285 {
241286 return $ this ;
242287 }
@@ -302,7 +347,7 @@ public function crop(int $width = null, int $height = null, int $x = null, int $
302347 */
303348 public function convert (int $ imageType )
304349 {
305- $ this ->image ->imageType = $ imageType ;
350+ $ this ->image () ->imageType = $ imageType ;
306351 return $ this ;
307352 }
308353
@@ -359,8 +404,8 @@ public function rotate(float $angle)
359404 */
360405 public function flatten (int $ red = 255 , int $ green = 255 , int $ blue = 255 )
361406 {
362- $ this ->width = $ this ->image ->origWidth ;
363- $ this ->height = $ this ->image ->origHeight ;
407+ $ this ->width = $ this ->image () ->origWidth ;
408+ $ this ->height = $ this ->image () ->origHeight ;
364409
365410 return $ this ->_flatten ();
366411 }
@@ -538,11 +583,11 @@ public function getEXIF(string $key = null, bool $silent = false)
538583 }
539584
540585 $ exif = null ; // default
541- switch ($ this ->image ->imageType )
586+ switch ($ this ->image () ->imageType )
542587 {
543588 case IMAGETYPE_JPEG :
544589 case IMAGETYPE_TIFF_II :
545- $ exif = exif_read_data ($ this ->image ->getPathname ());
590+ $ exif = exif_read_data ($ this ->image () ->getPathname ());
546591 if (! is_null ($ key ) && is_array ($ exif ))
547592 {
548593 $ exif = $ exif [$ key ] ?? false ;
@@ -576,8 +621,8 @@ public function getEXIF(string $key = null, bool $silent = false)
576621 */
577622 public function fit (int $ width , int $ height = null , string $ position = 'center ' )
578623 {
579- $ origWidth = $ this ->image ->origWidth ;
580- $ origHeight = $ this ->image ->origHeight ;
624+ $ origWidth = $ this ->image () ->origWidth ;
625+ $ origHeight = $ this ->image () ->origHeight ;
581626
582627 list ($ cropWidth , $ cropHeight ) = $ this ->calcAspectRatio ($ width , $ height , $ origWidth , $ origHeight );
583628
@@ -749,9 +794,9 @@ protected abstract function process(string $action);
749794 */
750795 public function __call (string $ name , array $ args = [])
751796 {
752- if (method_exists ($ this ->image , $ name ))
797+ if (method_exists ($ this ->image () , $ name ))
753798 {
754- return $ this ->image ->$ name (...$ args );
799+ return $ this ->image () ->$ name (...$ args );
755800 }
756801 }
757802
@@ -772,11 +817,11 @@ public function __call(string $name, array $args = [])
772817 protected function reproportion ()
773818 {
774819 if (($ this ->width === 0 && $ this ->height === 0 ) ||
775- $ this ->image ->origWidth === 0 ||
776- $ this ->image ->origHeight === 0 ||
820+ $ this ->image () ->origWidth === 0 ||
821+ $ this ->image () ->origHeight === 0 ||
777822 ( ! ctype_digit ((string ) $ this ->width ) && ! ctype_digit ((string ) $ this ->height )) ||
778- ! ctype_digit ((string ) $ this ->image ->origWidth ) ||
779- ! ctype_digit ((string ) $ this ->image ->origHeight )
823+ ! ctype_digit ((string ) $ this ->image () ->origWidth ) ||
824+ ! ctype_digit ((string ) $ this ->image () ->origHeight )
780825 )
781826 {
782827 return ;
@@ -790,7 +835,7 @@ protected function reproportion()
790835 {
791836 if ($ this ->width > 0 && $ this ->height > 0 )
792837 {
793- $ this ->masterDim = ((($ this ->image ->origHeight / $ this ->image ->origWidth ) - ($ this ->height / $ this ->width )) < 0 ) ? 'width ' : 'height ' ;
838+ $ this ->masterDim = ((($ this ->image () ->origHeight / $ this ->image () ->origWidth ) - ($ this ->height / $ this ->width )) < 0 ) ? 'width ' : 'height ' ;
794839 }
795840 else
796841 {
@@ -805,11 +850,11 @@ protected function reproportion()
805850
806851 if ($ this ->masterDim === 'width ' )
807852 {
808- $ this ->height = (int ) ceil ($ this ->width * $ this ->image ->origHeight / $ this ->image ->origWidth );
853+ $ this ->height = (int ) ceil ($ this ->width * $ this ->image () ->origHeight / $ this ->image () ->origWidth );
809854 }
810855 else
811856 {
812- $ this ->width = (int ) ceil ($ this ->image ->origWidth * $ this ->height / $ this ->image ->origHeight );
857+ $ this ->width = (int ) ceil ($ this ->image () ->origWidth * $ this ->height / $ this ->image () ->origHeight );
813858 }
814859 }
815860
0 commit comments