-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagesCrawler.php
More file actions
150 lines (120 loc) · 3.48 KB
/
ImagesCrawler.php
File metadata and controls
150 lines (120 loc) · 3.48 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Bit&Black Document Crawler.
*
* @author Tobias Köngeter
* @copyright Copyright © Bit&Black
* @link https://www.bitandblack.com
* @license MIT
*/
namespace BitAndBlack\DocumentCrawler\Crawler;
use BitAndBlack\DocumentCrawler\DTO\Image;
use BitAndBlack\DocumentCrawler\ResourceHandler\PassiveResourceHandler;
use BitAndBlack\DocumentCrawler\ResourceHandler\ResourceHandlerInterface;
use BitAndBlack\PathInfo\PathInfo;
use Symfony\Component\DomCrawler\Crawler;
/**
* Crawl and extract all defined images in a document, that have been declared with `<img ... />`.
*/
class ImagesCrawler implements CrawlerInterface
{
/**
* @var array<int, Image>
*/
private array $images = [];
private int $imageCrawlingLimit = 5;
private ResourceHandlerInterface $resourceHandler;
/**
* @var array<int, string>
*/
private array $knownImageFormats = [
'gif',
'png',
'jpg',
'jpeg',
];
public function __construct(
private readonly Crawler $crawler,
) {
$this->resourceHandler = new PassiveResourceHandler();
}
public function crawlContent(): void
{
$eachNode = static function (Crawler $node): Image|null {
$src = $node->attr('src');
if ('' === $src || null === $src) {
return null;
}
$alt = $node->attr('alt');
if ('' === $alt) {
$alt = null;
}
$title = $node->attr('title');
if ('' === $title) {
$title = null;
}
return new Image(
resource: $src,
alt: $alt,
title: $title,
);
};
/** @var array<int, Image|null> $images */
$images = $this->crawler
->filter('img')
->each($eachNode)
;
/**
* Remove null values.
*
* @var array<int, Image> $images
*/
$images = array_filter($images);
$imageCounter = 0;
foreach ($images as $image) {
$imageResource = $image->getResource();
$pathInfo = new PathInfo($imageResource);
$extension = strtolower((string) $pathInfo->getExtension());
if (!in_array($extension, $this->knownImageFormats, true)) {
continue;
}
$imageResourceHandled = $this->resourceHandler->handleResource(
$imageResource,
$this->crawler->getUri()
);
if (false === $imageResourceHandled) {
continue;
}
$this->images[] = new Image(
$imageResourceHandled,
$image->getAlt(),
$image->getTitle(),
);
++$imageCounter;
if ($imageCounter === $this->imageCrawlingLimit) {
break;
}
}
}
/**
* @return array<int, Image>
*/
public function getImages(): array
{
return $this->images;
}
public function getImageCrawlingLimit(): int
{
return $this->imageCrawlingLimit;
}
public function setImageCrawlingLimit(int $imageCrawlingLimit): self
{
$this->imageCrawlingLimit = $imageCrawlingLimit;
return $this;
}
public function setResourceHandler(ResourceHandlerInterface $resourceHandler): self
{
$this->resourceHandler = $resourceHandler;
return $this;
}
}