Skip to content

Commit 77cd0d2

Browse files
committed
Add class to retrieve image-info with Guzzle5
1 parent 713f6c1 commit 77cd0d2

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/ImageInfo/Guzzle5.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Embed\ImageInfo;
3+
4+
use Embed\ImageInfo\ImageInfoInterface;
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+
$result = [ ];
22+
23+
foreach ($urls as $url) {
24+
$response = $client->get($url['value']);
25+
26+
if (($size = @getimagesizefromstring($response->getBody())) !== false) {
27+
$result[] = [
28+
'width' => $size[0],
29+
'height' => $size[1],
30+
'size' => $size[0] * $size[1],
31+
'mime' => $response->getHeader('Content-Type'),
32+
] + $url;
33+
}
34+
}
35+
36+
return $result;
37+
}
38+
}

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' => 'http://www.mixdecultura.ro/wp-content/uploads/2013/03/galicia-locuinte-celtice.jpg',
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)