Skip to content

Commit 1baa3e0

Browse files
committed
improvements in image info retrievement
1 parent 912e53d commit 1baa3e0

17 files changed

+322
-197
lines changed

demo/index.php

+22-15
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,28 @@ function printCode($code, $asHtml = true)
8383
'publishedTime' => 'printText',
8484
];
8585

86-
$adapterData = $providerData;
87-
88-
function array_insert(&$array, $position, $insert_array)
89-
{
90-
$first_array = array_splice($array, 0, $position);
91-
$array = array_merge($first_array, $insert_array, $array);
92-
}
93-
94-
array_insert($adapterData, 4, ['images' => 'printArray']);
95-
array_insert($adapterData, 4, ['image' => 'printImage']);
96-
array_insert($adapterData, 5, ['imageWidth' => 'printText']);
97-
array_insert($adapterData, 6, ['imageHeight' => 'printText']);
98-
array_insert($adapterData, 12, ['aspectRatio' => 'printText']);
99-
array_insert($adapterData, 15, ['providerIcons' => 'printArray']);
100-
array_insert($adapterData, 15, ['providerIcon' => 'printImage']);
86+
$adapterData = [
87+
'title' => 'printText',
88+
'description' => 'printText',
89+
'url' => 'printUrl',
90+
'type' => 'printText',
91+
'image' => 'printImage',
92+
'imageWidth' => 'printText',
93+
'imageHeight' => 'printText',
94+
'images' => 'printArray',
95+
'code' => 'printCode',
96+
'source' => 'printUrl',
97+
'width' => 'printText',
98+
'height' => 'printText',
99+
'aspectRatio' => 'printText',
100+
'authorName' => 'printText',
101+
'authorUrl' => 'printUrl',
102+
'providerIcon' => 'printImage',
103+
'providerIcons' => 'printArray',
104+
'providerName' => 'printText',
105+
'providerUrl' => 'printUrl',
106+
'publishedTime' => 'printText',
107+
];
101108
?>
102109

103110
<!DOCTYPE html>

src/Adapters/Adapter.php

+41-32
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ public function __get($name)
150150
*/
151151
public function getTitle()
152152
{
153-
return Utils::getFirstData($this->providers, 'title') ?: $this->request->url->getUrl();
153+
return Utils::getFirstValue(Utils::getData($this->providers, 'title')) ?: $this->request->url->getUrl();
154154
}
155155

156156
/**
157157
* {@inheritdoc}
158158
*/
159159
public function getDescription()
160160
{
161-
return Utils::getFirstData($this->providers, 'description');
161+
return Utils::getFirstValue(Utils::getData($this->providers, 'description'));
162162
}
163163

164164
/**
@@ -172,7 +172,7 @@ public function getType()
172172
return 'video';
173173
}
174174

175-
if (($type = Utils::getMostPopular(Utils::getAllData($this->providers, 'type'))) && ($type !== 'link')) {
175+
if (($type = Utils::getMostPopularValue(Utils::getData($this->providers, 'type'))) && ($type !== 'link')) {
176176
return $type;
177177
}
178178

@@ -192,15 +192,15 @@ public function getType()
192192
*/
193193
public function getSource()
194194
{
195-
return Utils::getFirstUrlData($this->request->url, $this->providers, 'source');
195+
return Utils::getFirstValue(Utils::getData($this->providers, 'source', $this->request->url));
196196
}
197197

198198
/**
199199
* {@inheritdoc}
200200
*/
201201
public function getCode()
202202
{
203-
if ($code = Utils::getFirstData($this->providers, 'code')) {
203+
if ($code = Utils::getFirstValue(Utils::getData($this->providers, 'code'))) {
204204
if (strpos($code, '</iframe>') !== false) {
205205
return preg_replace('|^.*(<iframe.*</iframe>).*$|Us', '$1', $code);
206206
}
@@ -222,36 +222,43 @@ public function getCode()
222222
*/
223223
public function getUrl()
224224
{
225-
return Utils::getFirstUrlData($this->request->url, $this->providers, 'url') ?: $this->request->url->getUrl();
225+
return Utils::getFirstValue(Utils::getData($this->providers, 'url', $this->request->url)) ?: $this->request->url->getUrl();
226226
}
227227

228228
/**
229229
* {@inheritdoc}
230230
*/
231231
public function getAuthorName()
232232
{
233-
return Utils::getFirstData($this->providers, 'authorName');
233+
return Utils::getFirstValue(Utils::getData($this->providers, 'authorName'));
234234
}
235235

236236
/**
237237
* {@inheritdoc}
238238
*/
239239
public function getAuthorUrl()
240240
{
241-
return Utils::getFirstUrlData($this->request->url, $this->providers, 'authorUrl');
241+
return Utils::getFirstValue(Utils::getData($this->providers, 'authorUrl', $this->request->url));
242242
}
243243

244244
/**
245245
* {@inheritdoc}
246246
*/
247247
public function getProviderIconsUrls()
248248
{
249-
$icons = Utils::getAllUrlData($this->request->url, $this->providers, 'providerIconsUrls');
249+
$icons = Utils::getData($this->providers, 'providerIconsUrls', $this->request->url);
250250

251-
$icons[] = $this->request->url->getAbsolute('/favicon.ico');
252-
$icons[] = $this->request->url->getAbsolute('/favicon.png');
251+
Utils::unshiftValue($icons, [
252+
'value' => $this->request->url->getAbsolute('/favicon.ico'),
253+
'providers' => ['adapter']
254+
]);
253255

254-
return array_unique($icons);
256+
Utils::unshiftValue($icons, [
257+
'value' => $this->request->url->getAbsolute('/favicon.png'),
258+
'providers' => ['adapter']
259+
]);
260+
261+
return $icons;
255262
}
256263

257264
/**
@@ -268,36 +275,34 @@ public function getProviderIcons()
268275
public function getProviderIcon()
269276
{
270277
if ($this->config['getBiggerIcon']) {
271-
return Utils::getBiggerImage($this->providerIcons);
272-
}
273-
274-
if (($icons = $this->providerIcons) && reset($icons)) {
275-
return key($icons);
278+
return Utils::getBiggerValue($this->providerIcons);
276279
}
280+
281+
return Utils::getFirstValue($this->providerIcons);
277282
}
278283

279284
/**
280285
* {@inheritdoc}
281286
*/
282287
public function getProviderName()
283288
{
284-
return Utils::getFirstData($this->providers, 'providerName') ?: $this->request->url->getDomain();
289+
return Utils::getFirstValue(Utils::getData($this->providers, 'providerName')) ?: $this->request->url->getDomain();
285290
}
286291

287292
/**
288293
* {@inheritdoc}
289294
*/
290295
public function getProviderUrl()
291296
{
292-
return Utils::getFirstUrlData($this->request->url, $this->providers, 'providerUrl') ?: ($this->request->url->getScheme().'://'.$this->request->url->getDomain(true));
297+
return Utils::getFirstValue(Utils::getData($this->providers, 'providerUrl', $this->request->url)) ?: ($this->request->url->getScheme().'://'.$this->request->url->getDomain(true));
293298
}
294299

295300
/**
296301
* {@inheritdoc}
297302
*/
298303
public function getImagesUrls()
299304
{
300-
return Utils::getAllUrlData($this->request->url, $this->providers, 'imagesUrls');
305+
return Utils::getData($this->providers, 'imagesUrls', $this->request->url);
301306
}
302307

303308
/**
@@ -314,20 +319,24 @@ public function getImages()
314319
public function getImage()
315320
{
316321
if ($this->config['getBiggerImage']) {
317-
if (($src = Utils::getBiggerImage($this->images))) {
322+
if (($src = Utils::getBiggerValue($this->images, true)) !== null) {
318323
$image = $this->images[$src];
319324

320-
if (($image[0] >= $this->config['minImageWidth']) && ($image[1] >= $this->config['minImageHeight'])) {
325+
if (($image['width'] >= $this->config['minImageWidth']) && ($image['height'] >= $this->config['minImageHeight'])) {
321326
return $src;
322327
}
323328
}
324329

325330
return;
326331
}
327332

328-
foreach ($this->images as $src => $image) {
329-
if (($image[0] >= $this->config['minImageWidth']) && ($image[1] >= $this->config['minImageHeight'])) {
330-
return $src;
333+
foreach (Utils::sortByProviders($this->images) as $images) {
334+
if (($key = Utils::getBiggerValue($images, true)) !== null) {
335+
$image = $this->images[$key];
336+
337+
if (($image['width'] >= $this->config['minImageWidth']) && ($image['height'] >= $this->config['minImageHeight'])) {
338+
return $image['value'];
339+
}
331340
}
332341
}
333342
}
@@ -337,8 +346,8 @@ public function getImage()
337346
*/
338347
public function getImageWidth()
339348
{
340-
if ($this->image) {
341-
return $this->images[$this->image][0];
349+
if (($image = Utils::searchValue($this->images, $this->image)) !== false) {
350+
return $image['width'];
342351
}
343352
}
344353

@@ -347,8 +356,8 @@ public function getImageWidth()
347356
*/
348357
public function getImageHeight()
349358
{
350-
if ($this->image) {
351-
return $this->images[$this->image][1];
359+
if (($image = Utils::searchValue($this->images, $this->image)) !== false) {
360+
return $image['height'];
352361
}
353362
}
354363

@@ -357,15 +366,15 @@ public function getImageHeight()
357366
*/
358367
public function getWidth()
359368
{
360-
return Utils::getFirstData($this->providers, 'width');
369+
return Utils::getFirstValue(Utils::getData($this->providers, 'width'));
361370
}
362371

363372
/**
364373
* {@inheritdoc}
365374
*/
366375
public function getHeight()
367376
{
368-
return Utils::getFirstData($this->providers, 'height');
377+
return Utils::getFirstValue(Utils::getData($this->providers, 'height'));
369378
}
370379

371380
/**
@@ -386,6 +395,6 @@ public function getAspectRatio()
386395
*/
387396
public function getPublishedTime()
388397
{
389-
return Utils::getFirstData($this->providers, 'publishedTime');
398+
return Utils::getFirstValue(Utils::getData($this->providers, 'publishedTime'));
390399
}
391400
}

src/Adapters/Archive.php

+26-23
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010

1111
class Archive extends Webpage implements AdapterInterface
1212
{
13-
public $api;
14-
1513
/**
1614
* {@inheritdoc}
1715
*/
1816
public static function check(Request $request)
1917
{
2018
return $request->match([
21-
'http://archive.org/details/*',
19+
'https?://archive.org/details/*',
2220
]);
2321
}
2422

@@ -91,22 +89,23 @@ public function getType()
9189
*/
9290
public function getCode()
9391
{
94-
switch ($this->getMetadata('mediatype')) {
95-
case 'movies':
96-
$embed_url = str_replace('/details/', '/embed/', $this->getUrl());
97-
98-
return Utils::iframe($embed_url);
99-
100-
case 'audio':
101-
$embed_url = str_replace('/details/', '/embed/', $this->getUrl());
102-
103-
return Utils::iframe($embed_url, 0, 30);
92+
return Utils::iframe(str_replace('/details/', '/embed/', $this->getUrl()), $this->getWidth(), $this->getHeight());
93+
}
10494

105-
case 'texts':
106-
$embed_url = str_replace('/details/', '/stream/', $this->getUrl()).'?ui=embed';
95+
/**
96+
* {@inheritdoc}
97+
*/
98+
public function getWidth()
99+
{
100+
return 640;
101+
}
107102

108-
return Utils::iframe($embed_url, 480, 430);
109-
}
103+
/**
104+
* {@inheritdoc}
105+
*/
106+
public function getHeight()
107+
{
108+
return 480;
110109
}
111110

112111
/**
@@ -122,17 +121,21 @@ public function getAuthorName()
122121
*/
123122
public function getImagesUrls()
124123
{
125-
$images = [];
124+
$images = parent::getImagesUrls();
126125

127-
if (($image = $this->api->get('misc', 'image'))) {
128-
$images[] = $image;
126+
if (($url = $this->api->get('misc', 'image'))) {
127+
Utils::unshiftValue($images, [
128+
'value' => $this->request->url->getAbsolute($url),
129+
'providers' => ['api']
130+
]);
129131
}
130132

131133
if (is_array($files = $this->api->get('files'))) {
132134
foreach ($files as $url => $info) {
133-
if ($info['format'] === 'Thumbnail') {
134-
$images[] = $url;
135-
}
135+
Utils::unshiftValue($images, [
136+
'value' => $this->request->url->getAbsolute($url),
137+
'providers' => ['api']
138+
]);
136139
}
137140
}
138141

src/Adapters/Facebook.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,18 @@ public function getImagesUrls()
177177
{
178178
$images = parent::getImagesUrls();
179179

180-
$cover = $this->api->get('cover');
181-
182-
if ($cover && !empty($cover['source'])) {
183-
array_unshift($images, $cover['source']);
180+
if (($cover = $this->api->get('cover')) && !empty($cover['source'])) {
181+
Utils::unshiftValue($images, [
182+
'value' => $cover['source'],
183+
'providers' => ['api']
184+
]);
184185
}
185186

186-
$id = $this->api->get('id');
187-
188-
if ($id) {
189-
array_unshift($images, 'https://graph.facebook.com/'.$id.'/picture');
187+
if (($id = $this->api->get('id'))) {
188+
Utils::unshiftValue($images, [
189+
'value' => 'https://graph.facebook.com/'.$id.'/picture',
190+
'providers' => ['api']
191+
]);
190192
}
191193

192194
return $images;

src/Adapters/File.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ public function getCode()
8282
public function getImagesUrls()
8383
{
8484
if ($this->getType() === 'photo') {
85-
return [$this->getUrl()];
85+
return [
86+
[
87+
'value' => $this->getUrl(),
88+
'providers' => ['adapter']
89+
]
90+
];
8691
}
8792

8893
return [];

src/Adapters/Mit.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function getCode()
2727
$url = $this->getUrl();
2828
$url = preg_replace('|(/watch/[\w-]+)-([\d]+)|', '/embed/$2', $url);
2929

30-
return Utils::iframe($url, $this->getWidth(), $this->getHeight());
30+
return Utils::iframe($url, $this->width, $this->height);
3131
}
3232

3333
/**

0 commit comments

Comments
 (0)