File tree Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace Embed \ImageInfo ;
3
3
4
- use Embed \ ImageInfo \ ImageInfoInterface ;
4
+ use GuzzleHttp \ Pool ;
5
5
6
- /**
6
+ /**
7
7
* Class to retrieve the size and mimetype of images using Guzzle5
8
8
*/
9
9
class Guzzle5 implements ImageInfoInterface
@@ -14,22 +14,30 @@ class Guzzle5 implements ImageInfoInterface
14
14
public static function getImagesInfo (array $ urls , array $ config = null )
15
15
{
16
16
if ($ config === null || !isset ($ config ['client ' ]) || !($ config ['client ' ] instanceof \GuzzleHttp \Client)) {
17
- throw new RuntimeException ('Guzzle client not passed in config. ' );
17
+ throw new \ RuntimeException ('Guzzle client not passed in config. ' );
18
18
}
19
19
20
20
$ client = $ config ['client ' ];
21
- $ result = [ ];
22
21
22
+ // Build parallel requests
23
+ $ requests = [];
23
24
foreach ($ urls as $ url ) {
24
- $ response = $ client ->get ($ url ['value ' ]);
25
+ $ requests [] = $ client ->createRequest ('GET ' , $ url ['value ' ]);
26
+ }
27
+
28
+ // Execute in parallel
29
+ $ responses = Pool::batch ($ client , $ requests );
25
30
31
+ // Build result set
32
+ $ result = [];
33
+ foreach ($ responses as $ i => $ response ) {
26
34
if (($ size = @getimagesizefromstring ($ response ->getBody ())) !== false ) {
27
35
$ result [] = [
28
36
'width ' => $ size [0 ],
29
37
'height ' => $ size [1 ],
30
38
'size ' => $ size [0 ] * $ size [1 ],
31
39
'mime ' => $ response ->getHeader ('Content-Type ' ),
32
- ] + $ url ;
40
+ ] + $ urls [ $ i ] ;
33
41
}
34
42
}
35
43
You can’t perform that action at this time.
0 commit comments