能够使用php从网站上抓取图像,但我想刮掉高度大于200px和宽度为200px的唯一第一张图像。如何获得第一个图像源的尺寸?这是我的代码..
$html_3 = file_get_contents('http://beignindian.com');
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i',$html_3, $matches );
$main_image_1 = $matches[ 1 ][ 0 ];
答案 0 :(得分:1)
您可以使用getimagesize
功能获取图像的高度和宽度。一旦你得到它,然后添加if条件来执行更多的代码。
list($width, $height) = getimagesize($main_image_1); // I am assuming that $main_image_1 has image source.
echo "width: " . $width . "<br />";
echo "height: " . $height;
if($width > 200 && $height > 200) {
// perform something here.
}
更新:
如果您需要遍历网站上的所有图片,请使用以下代码:
$host = "http://www.beingindian.com/";
$html = file_get_contents($host);
// create new DOMDocument
$document = new DOMDocument('1.0', 'UTF-8');
// set error level
$internalErrors = libxml_use_internal_errors(true);
// load HTML
$document->loadHTML($html);
// Restore error level
libxml_use_internal_errors($internalErrors);
$images = $document->getElementsByTagName('img');
foreach ($images as $image) {
$image_source = $image->getAttribute('src');
// check if image URL is an absolute URL or relative URL
$image_url = (filter_var($image_source, FILTER_VALIDATE_URL))?$image_source:$host.$image_source;
list($width, $height) = getimagesize($image_url);
if($width > 200 && $height > 200) {
// perform something here.
}
else {
// perform something here.
}
}