Skip to content

Commit 160a1d2

Browse files
committed
Use parallel requests for multiple images
1 parent 6df8330 commit 160a1d2

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/ImageInfo/Guzzle5.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
namespace Embed\ImageInfo;
33

4-
use Embed\ImageInfo\ImageInfoInterface;
4+
use GuzzleHttp\Pool;
55

6-
/**
6+
/**
77
* Class to retrieve the size and mimetype of images using Guzzle5
88
*/
99
class Guzzle5 implements ImageInfoInterface
@@ -14,22 +14,30 @@ class Guzzle5 implements ImageInfoInterface
1414
public static function getImagesInfo(array $urls, array $config = null)
1515
{
1616
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.');
1818
}
1919

2020
$client = $config['client'];
21-
$result = [ ];
2221

22+
// Build parallel requests
23+
$requests = [];
2324
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);
2530

31+
// Build result set
32+
$result = [];
33+
foreach ($responses as $i => $response) {
2634
if (($size = @getimagesizefromstring($response->getBody())) !== false) {
2735
$result[] = [
2836
'width' => $size[0],
2937
'height' => $size[1],
3038
'size' => $size[0] * $size[1],
3139
'mime' => $response->getHeader('Content-Type'),
32-
] + $url;
40+
] + $urls[$i];
3341
}
3442
}
3543

0 commit comments

Comments
 (0)