Skip to content

Commit f956986

Browse files
committed
modified imageInfo class to be compatible with php5
1 parent 1896918 commit f956986

File tree

11 files changed

+141
-80
lines changed

11 files changed

+141
-80
lines changed

src/Adapters/Adapter.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public function getProviderIconsUrls()
312312
*/
313313
public function getProviderIcons()
314314
{
315-
return call_user_func("{$this->imageClass}::getImagesInfo", $this->getProviderIconsUrls(), $this->imageConfig, $this->imageRequests);
315+
return static::imagesInfo($this->getProviderIconsUrls());
316316
}
317317

318318
/**
@@ -378,7 +378,7 @@ public function getImagesUrls()
378378
*/
379379
public function getImages()
380380
{
381-
return call_user_func("{$this->imageClass}::getImagesInfo", $this->getImagesUrls(), $this->imageConfig, $this->imageRequests);
381+
return static::imagesInfo($this->getImagesUrls());
382382
}
383383

384384
/**
@@ -474,4 +474,31 @@ public function getLicense()
474474
{
475475
return Utils::getFirstValue(Utils::getData($this->providers, 'license', $this->request));
476476
}
477+
478+
/**
479+
* Get images info.
480+
*
481+
* @param array $urls
482+
*
483+
* @return array
484+
*/
485+
protected function imagesInfo($urls)
486+
{
487+
$requests = call_user_func("{$this->imageClass}::getImagesInfo", $urls, $this->imageConfig);
488+
array_replace($this->imageRequests, $requests);
489+
490+
$result = [];
491+
492+
foreach ($urls as $url => $value) {
493+
$info = isset($requests[$url]) ? $requests[$url]->getInfo() : false;
494+
495+
if ($info === false) {
496+
continue;
497+
}
498+
499+
$result[$url] = array_replace($value, $info);
500+
}
501+
502+
return $result;
503+
}
477504
}

src/Adapters/AdapterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(Request $request, array $config = null);
3535
public function getRequest();
3636

3737
/**
38-
* Returns all images Requests
38+
* Returns all images Requests.
3939
*
4040
* @return array
4141
*/

src/DataInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function getHeight();
124124
public function getPublishedTime();
125125

126126
/**
127-
* Gets the license info
127+
* Gets the license info.
128128
*
129129
* @return string|null
130130
*/

src/ImageInfo/Curl.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
/**
66
* Class to retrieve the size and mimetype of images using curl.
77
*/
8-
class Curl implements ImageInfoInterface
8+
class Curl implements ImageInfoInterface, ImagesInfoInterface
99
{
10-
use UtilsTrait;
11-
1210
protected static $mimetypes = [
1311
'image/jpeg',
1412
'image/png',
@@ -39,7 +37,7 @@ class Curl implements ImageInfoInterface
3937
/**
4038
* {@inheritdoc}
4139
*/
42-
public static function getImagesInfo(array $urls, array $config = null, array &$connections = [])
40+
public static function getImagesInfo(array $urls, array $config = null)
4341
{
4442
if (empty($urls)) {
4543
return [];
@@ -48,13 +46,11 @@ public static function getImagesInfo(array $urls, array $config = null, array &$
4846
$finfo = finfo_open(FILEINFO_MIME_TYPE);
4947
$curl = curl_multi_init();
5048
$result = [];
49+
$connections = [];
5150

5251
foreach ($urls as $k => $url) {
5352
if (strpos($url['value'], 'data:') === 0) {
54-
if ($info = static::getEmbeddedImageInfo($url['value'])) {
55-
$result[$k] = array_merge($url, $info);
56-
}
57-
53+
$result[$k] = new EmbeddedImage($url['value']);
5854
continue;
5955
}
6056

@@ -80,18 +76,13 @@ public static function getImagesInfo(array $urls, array $config = null, array &$
8076

8177
foreach ($connections as $k => $connection) {
8278
curl_multi_remove_handle($curl, $connection->getConnection());
83-
84-
if (($info = $connection->getInfo())) {
85-
$result[$k] = array_merge($urls[$k], $info);
86-
}
79+
$result[$k] = $connection;
8780
}
8881
}
8982

9083
finfo_close($finfo);
9184
curl_multi_close($curl);
9285

93-
ksort($result, SORT_NUMERIC);
94-
9586
return $result;
9687
}
9788

@@ -132,13 +123,11 @@ public function getConnection()
132123
}
133124

134125
/**
135-
* Get the image info with the format [$width, $height, $mimetype].
136-
*
137-
* @return null|array
126+
* {@inheritdoc}
138127
*/
139128
public function getInfo()
140129
{
141-
return $this->info;
130+
return $this->info ?: false;
142131
}
143132

144133
/**
@@ -158,10 +147,10 @@ public function getUrl()
158147
}
159148

160149
/**
161-
* Callback used to save the headers
150+
* Callback used to save the headers.
162151
*
163152
* @param resource $connection
164-
* @param string $string
153+
* @param string $string
165154
*
166155
* @return int
167156
*/

src/ImageInfo/EmbeddedImage.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Embed\ImageInfo;
4+
5+
/**
6+
* Class to retrieve the size and mimetype of embedded images.
7+
*/
8+
class EmbeddedImage implements ImageInfoInterface
9+
{
10+
private $url;
11+
12+
/**
13+
* @param string $url The image url
14+
*/
15+
public function __construct($url)
16+
{
17+
$this->url = $url;
18+
}
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function getHeaders()
24+
{
25+
return [];
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function getUrl()
32+
{
33+
return $this->url;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getInfo()
40+
{
41+
$pieces = explode(';', $this->url, 2);
42+
43+
if ((count($pieces) !== 2) || (strpos($pieces[0], 'image/') === false) || (strpos($pieces[1], 'base64,') !== 0)) {
44+
return false;
45+
}
46+
47+
if (($info = getimagesizefromstring(base64_decode(substr($pieces[1], 7)))) !== false) {
48+
return [
49+
'width' => $info[0],
50+
'height' => $info[1],
51+
'size' => $info[0] * $info[1],
52+
'mime' => $info['mime'],
53+
];
54+
}
55+
56+
return false;
57+
}
58+
}

src/ImageInfo/Guzzle5.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
/**
1111
* Class to retrieve the size and mimetype of images using Guzzle5.
1212
*/
13-
class Guzzle5 implements ImageInfoInterface
13+
class Guzzle5 implements ImageInfoInterface, ImagesInfoInterface
1414
{
15-
use UtilsTrait;
16-
1715
protected $response;
1816

1917
protected static $config = [
@@ -32,21 +30,20 @@ class Guzzle5 implements ImageInfoInterface
3230
/**
3331
* {@inheritdoc}
3432
*/
35-
public static function getImagesInfo(array $urls, array $config = null, array &$connections = [])
33+
public static function getImagesInfo(array $urls, array $config = null)
3634
{
3735
$client = isset($config['client']) ? $config['client'] : new Client([
3836
'defaults' => static::$config,
3937
]);
4038

39+
$requests = [];
4140
$result = [];
4241

4342
// Build parallel requests
4443
$requests = [];
4544
foreach ($urls as $k => $url) {
4645
if (strpos($url['value'], 'data:') === 0) {
47-
if ($info = static::getEmbeddedImageInfo($url['value'])) {
48-
$result[$k] = array_merge($url, $info);
49-
}
46+
$result[$k] = new EmbeddedImage($url['value']);
5047
continue;
5148
}
5249

@@ -62,16 +59,9 @@ public static function getImagesInfo(array $urls, array $config = null, array &$
6259
continue;
6360
}
6461

65-
$connection = new static($response);
66-
$connections[$k] = $connection;
67-
68-
if (($info = $connection->getInfo())) {
69-
$result[$k] = array_merge($urls[$k], $info);
70-
}
62+
$result[$k] = new static($response);
7163
}
7264

73-
ksort($result, SORT_NUMERIC);
74-
7565
return $result;
7666
}
7767

@@ -96,6 +86,9 @@ public function getUrl()
9686
$this->response->getEffectiveUrl();
9787
}
9888

89+
/**
90+
* {@inheritdoc}
91+
*/
9992
public function getInfo()
10093
{
10194
if (($size = getimagesizefromstring($this->response->getBody())) !== false) {
@@ -106,5 +99,7 @@ public function getInfo()
10699
'mime' => $size['mime'],
107100
];
108101
}
102+
103+
return false;
109104
}
110105
}

src/ImageInfo/ImageInfoInterface.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,29 @@ interface ImageInfoInterface
1212
*
1313
* @param array $urls
1414
* @param null|array $config
15-
* @param array &$connections
1615
*
1716
* @return array
1817
*/
19-
public static function getImagesInfo(array $urls, array $config = null, array &$connections = []);
18+
public static function getImagesInfo(array $urls, array $config = null);
2019

2120
/**
22-
* Returns the headers
21+
* Returns the headers.
2322
*
2423
* @return array
2524
*/
2625
public function getHeaders();
2726

2827
/**
29-
* Returns the url
28+
* Returns the url.
3029
*
3130
* @return string
3231
*/
3332
public function getUrl();
33+
34+
/**
35+
* Returns the image info.
36+
*
37+
* @return array|false
38+
*/
39+
public function getInfo();
3440
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Embed\ImageInfo;
4+
5+
/**
6+
* Inteface used by all imageinfo interfaces.
7+
*/
8+
interface ImagesInfoInterface
9+
{
10+
/**
11+
* Get the info of multiple images at once.
12+
*
13+
* @param array $urls
14+
* @param null|array $config
15+
*
16+
* @return array
17+
*/
18+
public static function getImagesInfo(array $urls, array $config = null);
19+
}

src/ImageInfo/UtilsTrait.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Providers/Html.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Html extends Provider implements ProviderInterface
1515
{
1616
protected $config = [
1717
'maxImages' => -1,
18-
'externalImages' => false
18+
'externalImages' => false,
1919
];
2020

2121
/**
@@ -313,7 +313,7 @@ protected function extractImages(\DOMElement $html)
313313
}
314314

315315
/**
316-
* Check whether a image url is valid or not
316+
* Check whether a image url is valid or not.
317317
*
318318
* @param Url $url
319319
*

0 commit comments

Comments
 (0)