@@ -41,43 +41,46 @@ public static function getImagesInfo(array $images, array $config = null)
4141 return [];
4242 }
4343
44- if (count ($ images ) === 1 ) {
45- $ info = self ::getImageInfo ($ images [0 ], $ config );
46-
47- return empty ($ info ) ? [] : [array_merge ($ images [0 ], $ info )];
48- }
49-
5044 $ finfo = finfo_open (FILEINFO_MIME_TYPE );
5145 $ connections = [];
5246 $ curl = curl_multi_init ();
47+ $ result = [];
5348
5449 foreach ($ images as $ k => $ image ) {
50+ if (strpos ($ image ['value ' ], 'data: ' ) === 0 ) {
51+ if ($ info = static ::getEmbeddedImageInfo ($ image ['value ' ])) {
52+ $ result [] = array_merge ($ image , $ info );
53+ }
54+
55+ continue ;
56+ }
57+
5558 $ connections [$ k ] = new static ($ image ['value ' ], $ finfo , $ config );
5659
5760 curl_multi_add_handle ($ curl , $ connections [$ k ]->getConnection ());
5861 }
5962
60- do {
61- $ return = curl_multi_exec ($ curl , $ active );
62- } while ($ return === CURLM_CALL_MULTI_PERFORM );
63-
64- while ($ active && $ return === CURLM_OK ) {
65- if (curl_multi_select ($ curl ) === -1 ) {
66- usleep (100 );
67- }
68-
63+ if ($ connections ) {
6964 do {
7065 $ return = curl_multi_exec ($ curl , $ active );
7166 } while ($ return === CURLM_CALL_MULTI_PERFORM );
72- }
7367
74- $ result = [];
68+ while ($ active && $ return === CURLM_OK ) {
69+ if (curl_multi_select ($ curl ) === -1 ) {
70+ usleep (100 );
71+ }
7572
76- foreach ($ connections as $ k => $ connection ) {
77- curl_multi_remove_handle ($ curl , $ connection ->getConnection ());
73+ do {
74+ $ return = curl_multi_exec ($ curl , $ active );
75+ } while ($ return === CURLM_CALL_MULTI_PERFORM );
76+ }
7877
79- if (($ info = $ connection ->getInfo ())) {
80- $ result [] = array_merge ($ images [$ k ], $ info );
78+ foreach ($ connections as $ k => $ connection ) {
79+ curl_multi_remove_handle ($ curl , $ connection ->getConnection ());
80+
81+ if (($ info = $ connection ->getInfo ())) {
82+ $ result [] = array_merge ($ images [$ k ], $ info );
83+ }
8184 }
8285 }
8386
@@ -87,30 +90,6 @@ public static function getImagesInfo(array $images, array $config = null)
8790 return $ result ;
8891 }
8992
90- /**
91- * Get the info of only one image.
92- *
93- * @param string $image
94- * @param null|array $config
95- *
96- * @return array|null
97- */
98- private static function getImageInfo ($ image , array $ config = null )
99- {
100- $ finfo = finfo_open (FILEINFO_MIME_TYPE );
101- $ img = new static ($ image ['value ' ], $ finfo , $ config );
102-
103- $ curl = $ img ->getConnection ();
104- curl_exec ($ curl );
105- curl_close ($ curl );
106-
107- $ info = $ img ->getInfo ();
108-
109- finfo_close ($ finfo );
110-
111- return $ info ;
112- }
113-
11493 /**
11594 * Init the curl connection.
11695 *
@@ -190,4 +169,22 @@ public function writeCallback($connection, $string)
190169
191170 return -1 ;
192171 }
172+
173+ protected static function getEmbeddedImageInfo ($ content )
174+ {
175+ $ pieces = explode ('; ' , $ content , 2 );
176+
177+ if ((count ($ pieces ) !== 2 ) || (strpos ($ pieces [0 ], 'image/ ' ) === false ) || (strpos ($ pieces [1 ], 'base64, ' ) !== 0 )) {
178+ return false ;
179+ }
180+
181+ $ info = getimagesizefromstring (base64_decode (substr ($ pieces [1 ], 7 )));
182+
183+ return [
184+ 'width ' => $ info [0 ],
185+ 'height ' => $ info [1 ],
186+ 'size ' => $ info [0 ] * $ info [1 ],
187+ 'mime ' => $ info ['mime ' ],
188+ ];
189+ }
193190}
0 commit comments