From a26b6f973421ae5f0b122da0e607f0467f6a2dee Mon Sep 17 00:00:00 2001 From: beenlee Date: Sat, 14 Sep 2024 17:21:34 +0800 Subject: [PATCH] get real URL of lazyload images --- src/UsesContent.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/UsesContent.php b/src/UsesContent.php index afcf472..dcd1018 100644 --- a/src/UsesContent.php +++ b/src/UsesContent.php @@ -529,9 +529,13 @@ public function images(): array $images = $this->filter('//img')->images(); /** @var \Symfony\Component\DomCrawler\Image $image */ - foreach ($images as $image) { - $result[] = $image->getUri(); - } + foreach ($images as $image) { + $node = $image->getNode(); + if ($node) { + $result[] = !empty($node->getAttribute('data-src')) ? $this->makeUrlAbsolute($node->getAttribute('data-src')) : $image->getUri(); + } + } + return $result; } @@ -551,7 +555,7 @@ public function imagesWithDetails(): array // Collect the URL and commonly interesting attributes $result[] = [ // Re-generate the proper uri using the Symfony's image class - 'url' => (new DomCrawlerImage($image, $this->currentBaseHost()))->getUri(), + 'url' => !empty($image->getAttribute('data-src')) ? $this->makeUrlAbsolute($image->getAttribute('data-src')) : (new DomCrawlerImage($image, $this->currentBaseHost()))->getUri(), 'alt' => $image->getAttribute('alt'), 'width' => $image->getAttribute('width') === '' ? null : $image->getAttribute('width'), 'height' => $image->getAttribute('height') === '' ? null : $image->getAttribute('height'),