1010 *
1111 * MIT Licensed
1212 * @version 0.1
13+ *
14+ * Modified for Embed library by Oscar Otero (https://github.com/oscarotero/Embed)
1315 */
1416
1517namespace Embed ;
@@ -29,6 +31,11 @@ public function __construct($uri = null)
2931 }
3032 }
3133
34+ /**
35+ * Opens an image stream
36+ *
37+ * @param string $uri
38+ */
3239 public function load ($ uri )
3340 {
3441 if ($ this ->handle ) {
@@ -48,21 +55,37 @@ public function load($uri)
4855 }
4956 }
5057
58+ /**
59+ * Closes the stream
60+ */
5161 public function close ()
5262 {
53- if ($ this ->handle ) fclose ($ this ->handle );
63+ if ($ this ->handle ) {
64+ fclose ($ this ->handle );
65+ }
5466 }
5567
68+ /**
69+ * Gets the image size
70+ *
71+ * @return array|false
72+ */
5673 public function getSize ()
5774 {
5875 $ this ->strpos = 0 ;
76+
5977 if ($ this ->getType () && ($ size = $ this ->parseSize ())) {
6078 return array_values ($ size );
6179 }
6280
6381 return false ;
6482 }
6583
84+ /**
85+ * Gets the image type
86+ *
87+ * @return string|false
88+ */
6689 public function getType ()
6790 {
6891 $ this ->strpos = 0 ;
@@ -88,6 +111,11 @@ public function getType()
88111 return $ this ->type ;
89112 }
90113
114+ /**
115+ * Parses the image type
116+ *
117+ * @return array|null
118+ */
91119 private function parseSize ()
92120 {
93121 $ this ->strpos = 0 ;
@@ -106,20 +134,35 @@ private function parseSize()
106134 return null ;
107135 }
108136
137+ /**
138+ * Parses the image size for PNG
139+ *
140+ * @return array
141+ */
109142 private function parseSizeForPNG ()
110143 {
111144 $ chars = $ this ->getChars (25 );
112145
113146 return unpack ("N* " , substr ($ chars , 16 , 8 ));
114147 }
115148
149+ /**
150+ * Parses the image size for GIF
151+ *
152+ * @return array
153+ */
116154 private function parseSizeForGIF ()
117155 {
118156 $ chars = $ this ->getChars (11 );
119157
120158 return unpack ("S* " , substr ($ chars , 6 , 4 ));
121159 }
122160
161+ /**
162+ * Parses the image size for BMP
163+ *
164+ * @return array
165+ */
123166 private function parseSizeForBMP ()
124167 {
125168 $ chars = $ this ->getChars (29 );
@@ -129,6 +172,11 @@ private function parseSizeForBMP()
129172 return (reset ($ type ) == 40 ) ? unpack ('L* ' , substr ($ chars , 4 )) : unpack ('L* ' , substr ($ chars , 4 , 8 ));
130173 }
131174
175+ /**
176+ * Parses the image size for JPEG
177+ *
178+ * @return array
179+ */
132180 private function parseSizeForJPEG ()
133181 {
134182 $ state = null ;
@@ -179,6 +227,13 @@ private function parseSizeForJPEG()
179227 }
180228 }
181229
230+ /**
231+ * Gets the stream characters until a specific position
232+ *
233+ * @param integer $n The end position of characters
234+ *
235+ * @return false|string
236+ */
182237 private function getChars ($ n )
183238 {
184239 $ response = null ;
@@ -206,6 +261,11 @@ private function getChars($n)
206261 return mb_convert_encoding ($ result , "8BIT " );
207262 }
208263
264+ /**
265+ * Returns the current byte
266+ *
267+ * @return false|string
268+ */
209269 private function getByte ()
210270 {
211271 $ c = $ this ->getChars (1 );
@@ -214,18 +274,30 @@ private function getByte()
214274 return reset ($ b );
215275 }
216276
277+ /**
278+ * Returns an integer
279+ *
280+ * @return integer
281+ */
217282 private function readInt ($ str )
218283 {
219284 $ size = unpack ("C* " , $ str );
220285
221- return ($ size [1 ] << 8 ) + $ size [2 ];
286+ return ($ size [1 ] << 8 ) + $ size [2 ];
222287 }
223288
224289 public function __destruct ()
225290 {
226291 $ this ->close ();
227292 }
228293
294+ /**
295+ * Sort an array of images by their size
296+ *
297+ * @param array $images
298+ *
299+ * @return array
300+ */
229301 public static function sortImagesBySize (array $ images )
230302 {
231303 if (count ($ images ) < 2 ) {
0 commit comments