1+ <?php
2+
3+ namespace Iconscout \ImageResizer ;
4+
5+ class ImageFile
6+ {
7+ /**
8+ * Mime type
9+ *
10+ * @var string
11+ */
12+ public $ mime ;
13+
14+ /**
15+ * Original Name given by user of current file
16+ *
17+ * @var string
18+ */
19+ public $ originalname ;
20+
21+ /**
22+ * Filename of current file (without extension)
23+ *
24+ * @var string
25+ */
26+ public $ filename ;
27+
28+ /**
29+ * File extension of current file
30+ *
31+ * @var string
32+ */
33+ public $ extension ;
34+
35+ /**
36+ * File dimensions of current file
37+ *
38+ * @var string
39+ */
40+ public $ dimensions ;
41+
42+ /**
43+ * Full Location of current file
44+ *
45+ * @var string
46+ */
47+ public $ fullpath ;
48+
49+
50+ public function __construct (string $ path = null )
51+ {
52+ if (! empty ($ path )) {
53+ $ this ->setFileInfoFromPath ($ path );
54+ }
55+ }
56+
57+ /**
58+ * File name of current file
59+ *
60+ * @var string
61+ */
62+
63+ public function getBaseName ()
64+ {
65+ return "{$ this ->filename }. {$ this ->extension }" ;
66+ }
67+
68+ /**
69+ * Sets all instance properties from given path
70+ *
71+ * @param string $path
72+ */
73+ public function setFileInfoFromPath ($ path )
74+ {
75+ $ info = pathinfo ($ path );
76+ $ this ->fullpath = $ path ;
77+ $ this ->dirname = array_key_exists ('dirname ' , $ info ) ? $ info ['dirname ' ] : null ;
78+ $ this ->originalname = array_key_exists ('basename ' , $ info ) ? $ info ['basename ' ] : null ;
79+ $ this ->filename = array_key_exists ('filename ' , $ info ) ? $ info ['filename ' ] : null ;
80+ $ this ->extension = array_key_exists ('extension ' , $ info ) ? $ info ['extension ' ] : null ;
81+
82+ if (file_exists ($ path ) && is_file ($ path )) {
83+ $ this ->dimensions = $ this ->dimensions ($ path );
84+ $ this ->mime = finfo_file (finfo_open (FILEINFO_MIME_TYPE ), $ path );
85+ }
86+
87+ return $ this ;
88+ }
89+
90+ /**
91+ * Get file size
92+ *
93+ * @return mixed
94+ */
95+ public function filesize ()
96+ {
97+ $ path = $ this ->basePath ();
98+
99+ if (file_exists ($ path ) && is_file ($ path )) {
100+ return filesize ($ path );
101+ }
102+
103+ return false ;
104+ }
105+
106+ /**
107+ * Get image file size
108+ *
109+ * @return mixed
110+ */
111+ public function dimensions ()
112+ {
113+ if (empty ($ this ->dimensions )){
114+ $ path = $ this ->fullpath ;
115+
116+ if (file_exists ($ path ) && is_file ($ path )) {
117+
118+ $ sizes = getimagesize ($ path );
119+ $ dimensions ['width ' ] = $ sizes [0 ];
120+ $ dimensions ['height ' ] = $ sizes [1 ];
121+
122+ // We need to use exif data as getimagesize provides invalid width, height if image is rotated
123+ $ exif = @exif_read_data ($ path );
124+
125+ if (! empty ($ exif ['Orientation ' ])) {
126+ if ($ exif ['Orientation ' ] === 8 || $ exif ['Orientation ' ] === 6 ) {
127+ // 8 = CW Rotate Image to get original
128+ // 6 = CCW Rotate Image to get original
129+
130+ // Store width as height & height as width
131+ $ height = $ dimensions ['width ' ];
132+ $ width = $ dimensions ['height ' ];
133+
134+ $ dimensions ['width ' ] = $ width ;
135+ $ dimensions ['height ' ] = $ height ;
136+ }
137+ }
138+
139+ $ this ->dimensions = $ dimensions ;
140+ }
141+ }
142+
143+ return $ this ->dimensions ;
144+ }
145+
146+ /**
147+ * Check image file is valid or not
148+ *
149+ * @return string
150+ */
151+ public function isValid ()
152+ {
153+ return exif_imagetype ($ this ->fullpath );
154+ }
155+
156+ /**
157+ * Get file path by string
158+ *
159+ * @return string
160+ */
161+ public function __toString ()
162+ {
163+ return $ this ->getBaseName ();
164+ }
165+ }
0 commit comments