Php 使用domcrawler(Goutte)获取刮取图像的大小

Php 使用domcrawler(Goutte)获取刮取图像的大小,php,laravel,web-scraping,web-crawler,html-parsing,Php,Laravel,Web Scraping,Web Crawler,Html Parsing,对于我的网站,用户可以提交链接 我想要的是,当一个链接被提交时,它解析被链接页面的DOM,找到最大的图像,最大的图像是总宽度+总高度,make保存该页面上最大图像的缩略图 这样可以在链接旁边显示缩略图 为了实现这一点,我使用了Goutte软件包和带有Laravel的图像干预软件包 这就是我到目前为止所做的: $goutteClient = new Client(); $guzzleClient = new GuzzleClient(array( 'timeout' => 15, )

对于我的网站,用户可以提交链接

我想要的是,当一个链接被提交时,它解析被链接页面的DOM,找到最大的图像,最大的图像是总宽度+总高度,make保存该页面上最大图像的缩略图

这样可以在链接旁边显示缩略图

为了实现这一点,我使用了Goutte软件包和带有Laravel的图像干预软件包

这就是我到目前为止所做的:

$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,但我不知道如何查看图像的属性

最好的方法是什么?保存页面上的所有图像,然后查看其宽度/高度不是我的选择。

我相信您可以使用

getimagesize

它将返回您正在查找的属性数组。包括高度和宽度。它要求在服务器配置中将allow_url_fopen设置为true。假设图像是远程的

你的情况也是如此。它可能看起来像

    $files = [];

// maybe pass this by reference as &$image and store the totals in the same array, otherwise
foreach ($images as $image) {
    $attributes = getimagesize($image);

    $height = $attributes[0];
    $width = $attributes[1];

    $total = $height + $width;

    // bind the total as the id of the array, if there are multiple matching totals, it will always be the last
    // image that matches that is selected.
    $files[$total] = $image;
}

// then you can use any standard logic to extract the data from the new array.