-
Notifications
You must be signed in to change notification settings - Fork 345
/
images.php
57 lines (48 loc) · 1.61 KB
/
images.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace spouts\rss;
use SimplePie;
use spouts\Item;
/**
* Spout for fetching images from given rss feed
*
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <tobias.zeising@aditu.de>
*/
class images extends feed {
public string $name = 'RSS Feed Images';
public string $description = 'Fetch images from given rss feed.';
/**
* @return \Generator<Item<SimplePie\Item>> list of items
*/
public function getItems(): iterable {
foreach (parent::getItems() as $item) {
$thumbnail = $this->findThumbnail($item->getExtraData());
if ($thumbnail !== null) {
yield $item->withThumbnail($thumbnail);
} else {
yield $item;
}
}
}
private function findThumbnail(SimplePie\Item $item): ?string {
// search enclosures (media tags)
if (($firstEnclosure = $item->get_enclosure(0)) !== null) {
// thumbnail given?
if ($firstEnclosure->get_thumbnail()) {
return $firstEnclosure->get_thumbnail();
}
// link given?
elseif ($firstEnclosure->get_link()) {
return $firstEnclosure->get_link();
}
} else { // no enclosures: search image link in content
$image = \helpers\ImageUtils::findFirstImageSource((string) $item->get_content());
if ($image !== null) {
return $image;
}
}
return null;
}
}