Skip to content

Commit 0da94bf

Browse files
committed
considerate data-based urls to get images
1 parent 03458d7 commit 0da94bf

File tree

2 files changed

+64
-45
lines changed

2 files changed

+64
-45
lines changed

src/ImageInfo/Curl.php

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,43 +41,46 @@ public static function getImagesInfo(array $images, array $config = null)
4141
return [];
4242
}
4343

44-
if (count($images) === 1) {
45-
$info = self::getImageInfo($images[0], $config);
46-
47-
return empty($info) ? [] : [array_merge($images[0], $info)];
48-
}
49-
5044
$finfo = finfo_open(FILEINFO_MIME_TYPE);
5145
$connections = [];
5246
$curl = curl_multi_init();
47+
$result = [];
5348

5449
foreach ($images as $k => $image) {
50+
if (strpos($image['value'], 'data:') === 0) {
51+
if ($info = static::getEmbeddedImageInfo($image['value'])) {
52+
$result[] = array_merge($image, $info);
53+
}
54+
55+
continue;
56+
}
57+
5558
$connections[$k] = new static($image['value'], $finfo, $config);
5659

5760
curl_multi_add_handle($curl, $connections[$k]->getConnection());
5861
}
5962

60-
do {
61-
$return = curl_multi_exec($curl, $active);
62-
} while ($return === CURLM_CALL_MULTI_PERFORM);
63-
64-
while ($active && $return === CURLM_OK) {
65-
if (curl_multi_select($curl) === -1) {
66-
usleep(100);
67-
}
68-
63+
if ($connections) {
6964
do {
7065
$return = curl_multi_exec($curl, $active);
7166
} while ($return === CURLM_CALL_MULTI_PERFORM);
72-
}
7367

74-
$result = [];
68+
while ($active && $return === CURLM_OK) {
69+
if (curl_multi_select($curl) === -1) {
70+
usleep(100);
71+
}
7572

76-
foreach ($connections as $k => $connection) {
77-
curl_multi_remove_handle($curl, $connection->getConnection());
73+
do {
74+
$return = curl_multi_exec($curl, $active);
75+
} while ($return === CURLM_CALL_MULTI_PERFORM);
76+
}
7877

79-
if (($info = $connection->getInfo())) {
80-
$result[] = array_merge($images[$k], $info);
78+
foreach ($connections as $k => $connection) {
79+
curl_multi_remove_handle($curl, $connection->getConnection());
80+
81+
if (($info = $connection->getInfo())) {
82+
$result[] = array_merge($images[$k], $info);
83+
}
8184
}
8285
}
8386

@@ -87,30 +90,6 @@ public static function getImagesInfo(array $images, array $config = null)
8790
return $result;
8891
}
8992

90-
/**
91-
* Get the info of only one image.
92-
*
93-
* @param string $image
94-
* @param null|array $config
95-
*
96-
* @return array|null
97-
*/
98-
private static function getImageInfo($image, array $config = null)
99-
{
100-
$finfo = finfo_open(FILEINFO_MIME_TYPE);
101-
$img = new static($image['value'], $finfo, $config);
102-
103-
$curl = $img->getConnection();
104-
curl_exec($curl);
105-
curl_close($curl);
106-
107-
$info = $img->getInfo();
108-
109-
finfo_close($finfo);
110-
111-
return $info;
112-
}
113-
11493
/**
11594
* Init the curl connection.
11695
*
@@ -190,4 +169,22 @@ public function writeCallback($connection, $string)
190169

191170
return -1;
192171
}
172+
173+
protected static function getEmbeddedImageInfo($content)
174+
{
175+
$pieces = explode(';', $content, 2);
176+
177+
if ((count($pieces) !== 2) || (strpos($pieces[0], 'image/') === false) || (strpos($pieces[1], 'base64,') !== 0)) {
178+
return false;
179+
}
180+
181+
$info = getimagesizefromstring(base64_decode(substr($pieces[1], 7)));
182+
183+
return [
184+
'width' => $info[0],
185+
'height' => $info[1],
186+
'size' => $info[0] * $info[1],
187+
'mime' => $info['mime'],
188+
];
189+
}
193190
}

src/Url.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public function match($patterns)
6565
return false;
6666
}
6767

68+
/**
69+
* Return the content of the url (for embedded images).
70+
*
71+
* @return string The content or null
72+
*/
73+
public function getContent()
74+
{
75+
return isset($this->info['content']) ? $this->info['content'] : null;
76+
}
77+
6878
/**
6979
* Return the extension of the url (html, php, jpg, etc).
7080
*
@@ -395,9 +405,14 @@ protected function buildUrl()
395405
{
396406
$url = '';
397407

408+
if (isset($this->info['content'])) {
409+
return 'data:'.$this->info['content'];
410+
}
411+
398412
if (isset($this->info['scheme'])) {
399413
$url .= $this->info['scheme'].'://';
400414
}
415+
401416
if (isset($this->info['host'])) {
402417
$url .= $this->info['host'];
403418
}
@@ -481,6 +496,12 @@ public function getAbsolute($url)
481496
*/
482497
private function setPath($path)
483498
{
499+
if ($this->getScheme() === 'data') {
500+
$this->info['content'] = $path;
501+
$this->info['path'] = $this->info['file'] = $this->info['extension'] = null;
502+
return;
503+
}
504+
484505
$parts = pathinfo($path);
485506

486507
$this->info['path'] = [];
@@ -495,5 +516,6 @@ private function setPath($path)
495516

496517
$this->info['file'] = isset($parts['filename']) ? $parts['filename'] : null;
497518
$this->info['extension'] = isset($parts['extension']) ? $parts['extension'] : null;
519+
$this->info['content'] = null;
498520
}
499521
}

0 commit comments

Comments
 (0)