亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 domcrawler (Goutte) 獲取抓取圖像的大小

使用 domcrawler (Goutte) 獲取抓取圖像的大小

PHP
夢里花落0921 2021-11-26 16:27:28
對于我的網站,用戶可以提交鏈接。我想要的是,當一個鏈接被提交時,它會解析被鏈接頁面的 DOM,找到最大的圖像(最大的是總寬度 + 總高度),并保存該頁面上最大圖像的縮略圖。這樣可以在鏈接旁邊放一個縮略圖。為了實現這一點,我在 Laravel 中使用了 Goutte 包和 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的形象,但我不知道如何查看圖像的屬性。做這個的最好方式是什么?保存頁面上的所有圖像然后查看它們的寬度/高度對我來說不是一個選擇。
查看完整描述

1 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

我相信你可以使用,


getimagesize()


https://www.php.net/manual/en/function.getimagesize.php


它將返回您正在尋找的屬性數組。包括高度和寬度。它要求在您的服務器配置中將 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.


查看完整回答
反對 回復 2021-11-26
  • 1 回答
  • 0 關注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號