Skip to content

Commit e72cfce

Browse files
committed
Merge pull request #77 from mburtscher/guzzle-image-info
Add class to retrieve image-info with Guzzle5
2 parents 713f6c1 + afcf08d commit e72cfce

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

src/ImageInfo/Guzzle5.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Embed\ImageInfo;
3+
4+
use GuzzleHttp\Pool;
5+
6+
/**
7+
* Class to retrieve the size and mimetype of images using Guzzle5
8+
*/
9+
class Guzzle5 implements ImageInfoInterface
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public static function getImagesInfo(array $urls, array $config = null)
15+
{
16+
if ($config === null || !isset($config['client']) || !($config['client'] instanceof \GuzzleHttp\Client)) {
17+
throw new \RuntimeException('Guzzle client not passed in config.');
18+
}
19+
20+
$client = $config['client'];
21+
22+
// Build parallel requests
23+
$requests = [];
24+
foreach ($urls as $url) {
25+
$requests[] = $client->createRequest('GET', $url['value']);
26+
}
27+
28+
// Execute in parallel
29+
$responses = Pool::batch($client, $requests);
30+
31+
// Build result set
32+
$result = [];
33+
foreach ($responses as $i => $response) {
34+
if (($size = @getimagesizefromstring($response->getBody())) !== false) {
35+
$result[] = [
36+
'width' => $size[0],
37+
'height' => $size[1],
38+
'size' => $size[0] * $size[1],
39+
'mime' => $size['mime'],
40+
] + $urls[$i];
41+
}
42+
}
43+
44+
return $result;
45+
}
46+
}

tests/ImageInfoTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
<?php
22
class ImageInfoTest extends TestCaseBase
33
{
4+
const TEST_IMAGE_URL = 'http://www.mixdecultura.ro/wp-content/uploads/2013/03/galicia-locuinte-celtice.jpg';
5+
const TEST_IMAGE_WIDTH = 600;
6+
const TEST_IMAGE_HEIGHT = 408;
7+
const TEST_IMAGE_SIZE = 244800;
8+
const TEST_IMAGE_MIME = 'image/jpeg';
9+
410
public function testOne()
511
{
612
$info = Embed\ImageInfo\Curl::getImageInfo([
7-
'value' => 'http://www.mixdecultura.ro/wp-content/uploads/2013/03/galicia-locuinte-celtice.jpg',
13+
'value' => self::TEST_IMAGE_URL,
814
]);
915

1016
$this->assertEquals($info, [
11-
'width' => 600,
12-
'height' => 408,
13-
'size' => 244800,
14-
'mime' => 'image/jpeg',
17+
'width' => self::TEST_IMAGE_WIDTH,
18+
'height' => self::TEST_IMAGE_HEIGHT,
19+
'size' => self::TEST_IMAGE_SIZE,
20+
'mime' => self::TEST_IMAGE_MIME,
21+
]);
22+
}
23+
24+
public function testGuzzle()
25+
{
26+
$info = Embed\ImageInfo\Guzzle5::getImagesInfo([[
27+
'value' => self::TEST_IMAGE_URL,
28+
]], [
29+
'client' => new \GuzzleHttp\Client(),
30+
]);
31+
32+
$this->assertEquals($info[0], [
33+
'width' => self::TEST_IMAGE_WIDTH,
34+
'height' => self::TEST_IMAGE_HEIGHT,
35+
'size' => self::TEST_IMAGE_SIZE,
36+
'mime' => self::TEST_IMAGE_MIME,
37+
'value' => self::TEST_IMAGE_URL,
1538
]);
1639
}
1740
}

0 commit comments

Comments
 (0)