Skip to content

Commit b23b77f

Browse files
committed
Merge pull request #79 from younes0/master
handle getimagesizefromstring errors, embbeded images + request exceptions for guzzle5
2 parents ca49bdf + e4c99df commit b23b77f

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/ImageInfo/Guzzle5.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
namespace Embed\ImageInfo;
44

55
use GuzzleHttp\Pool;
6+
use GuzzleHttp\Exception\RequestException;
67

78
/**
89
* Class to retrieve the size and mimetype of images using Guzzle5.
910
*/
1011
class Guzzle5 implements ImageInfoInterface
1112
{
13+
use UtilsTrait;
14+
1215
/**
1316
* {@inheritdoc}
1417
*/
@@ -23,6 +26,13 @@ public static function getImagesInfo(array $urls, array $config = null)
2326
// Build parallel requests
2427
$requests = [];
2528
foreach ($urls as $url) {
29+
if (strpos($url['value'], 'data:') === 0) {
30+
if ($info = static::getEmbeddedImageInfo($url['value'])) {
31+
$result[] = array_merge($url, $info);
32+
}
33+
continue;
34+
}
35+
2636
$requests[] = $client->createRequest('GET', $url['value']);
2737
}
2838

@@ -32,6 +42,10 @@ public static function getImagesInfo(array $urls, array $config = null)
3242
// Build result set
3343
$result = [];
3444
foreach ($responses as $i => $response) {
45+
if ($response instanceof RequestException) {
46+
continue;
47+
}
48+
3549
if (($size = @getimagesizefromstring($response->getBody())) !== false) {
3650
$result[] = [
3751
'width' => $size[0],

src/ImageInfo/UtilsTrait.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ protected static function getEmbeddedImageInfo($content)
1919
return false;
2020
}
2121

22-
$info = getimagesizefromstring(base64_decode(substr($pieces[1], 7)));
22+
if (($info = @getimagesizefromstring(base64_decode(substr($pieces[1], 7)))) !== false) {
23+
return [
24+
'width' => $info[0],
25+
'height' => $info[1],
26+
'size' => $info[0] * $info[1],
27+
'mime' => $info['mime'],
28+
];
29+
}
2330

24-
return [
25-
'width' => $info[0],
26-
'height' => $info[1],
27-
'size' => $info[0] * $info[1],
28-
'mime' => $info['mime'],
29-
];
31+
return false;
3032
}
31-
}
33+
}

0 commit comments

Comments
 (0)