@@ -60,15 +60,11 @@ class Image implements \JsonSerializable, \Stringable
6060 */
6161 public function __construct (string $ filename )
6262 {
63- $ realpath = \realpath ($ filename );
64- if ($ realpath === false || !\is_file ($ realpath ) || !\is_readable ($ realpath )) {
65- throw new InvalidArgumentException ('File does not exists or is not readable: ' . $ filename );
66- }
67- $ this ->filename = $ realpath ;
68- $ info = \getimagesize ($ this ->filename );
63+ $ this ->setFilename ($ filename );
64+ $ info = \getimagesize ($ this ->getFilename ());
6965 if ($ info === false ) {
7066 throw new RuntimeException (
71- 'Could not get image info from the given filename: ' . $ this ->filename
67+ 'Could not get image info from the given filename: ' . $ this ->getFilename ()
7268 );
7369 }
7470 if (!(\imagetypes () & $ info [2 ])) {
@@ -77,10 +73,10 @@ public function __construct(string $filename)
7773 $ this ->setType ($ info [2 ]);
7874 $ this ->setMime ($ info ['mime ' ]);
7975 $ instance = match ($ this ->getType ()) {
80- \IMAGETYPE_PNG => \imagecreatefrompng ($ this ->filename ),
81- \IMAGETYPE_JPEG => \imagecreatefromjpeg ($ this ->filename ),
82- \IMAGETYPE_GIF => \imagecreatefromgif ($ this ->filename ),
83- \IMAGETYPE_AVIF => \imagecreatefromavif ($ this ->filename ),
76+ \IMAGETYPE_PNG => \imagecreatefrompng ($ this ->getFilename () ),
77+ \IMAGETYPE_JPEG => \imagecreatefromjpeg ($ this ->getFilename () ),
78+ \IMAGETYPE_GIF => \imagecreatefromgif ($ this ->getFilename () ),
79+ \IMAGETYPE_AVIF => \imagecreatefromavif ($ this ->getFilename () ),
8480 default => throw new RuntimeException ('Image type is not acceptable: ' . $ this ->getType ()),
8581 };
8682 if (!$ instance instanceof GdImage) {
@@ -96,6 +92,21 @@ public function __toString() : string
9692 return $ this ->getDataUrl ();
9793 }
9894
95+ public function getFilename () : string
96+ {
97+ return $ this ->filename ;
98+ }
99+
100+ protected function setFilename (string $ filename ) : static
101+ {
102+ $ realpath = \realpath ($ filename );
103+ if ($ realpath === false || !\is_file ($ realpath ) || !\is_readable ($ realpath )) {
104+ throw new InvalidArgumentException ('File does not exists or is not readable: ' . $ filename );
105+ }
106+ $ this ->filename = $ realpath ;
107+ return $ this ;
108+ }
109+
99110 /**
100111 * Gets the GdImage instance.
101112 *
@@ -321,7 +332,7 @@ public function create(int | string $type, string $filename) : Image
321332 */
322333 public function save (?string $ filename = null ) : bool
323334 {
324- $ filename ??= $ this ->filename ;
335+ $ filename ??= $ this ->getFilename () ;
325336 return match ($ this ->getType ()) {
326337 \IMAGETYPE_PNG => \imagepng ($ this ->getInstance (), $ filename , $ this ->getQuality ()),
327338 \IMAGETYPE_JPEG => \imagejpeg ($ this ->getInstance (), $ filename , $ this ->getQuality ()),
0 commit comments