【发布时间】:2019-12-23 03:39:56
【问题描述】:
对于我的网站,用户可以提交链接。
我想要的是,当提交链接时,它会解析被链接页面的 DOM,找到最大的图像(最大的是总宽度 + 总高度),并保存该页面上最大图像的缩略图.
这样可以在链接旁边显示缩略图。
为了实现这一点,我使用了 Goutte 包和 Laravel 的 Image Intervention 包。
这是我到目前为止所做的:
$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
'timeout' => 15,
));
$goutteClient->setClient($guzzleClient);
$crawler = $goutteClient->request('GET', 'https://www.reddit.com');
$result = $crawler
->filterXpath('//img')
->extract(array('src'));
foreach ($result as $image) {
//get the width and height of each $image
}
//$file = image with the biggest width + height
$thumbnail = Image::make($file);
$large->resize(900, 900, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
注释掉的部分是我正在努力解决的问题。
foreach会返回图片的src,但是不知道怎么查看图片的属性。
最好的方法是什么?保存页面上的所有图像然后查看它们的宽度/高度对我来说不是一个选项。
【问题讨论】:
标签: php laravel web-scraping web-crawler html-parsing