我的函数使用 file_get_contents,我使用它从网页获取图像 src URL,然后将这些图像打印到不同的 php/html 页面上。但有时图像可以正确渲染,有时则不能。这是我的代码:
//function to get the contents from a URL
function getSiteOG( $functionurl, $specificTags=0 ){
$doc = new DOMDocument();
@$doc->loadHTML(file_get_contents($functionurl));
$res['title'] = $doc->getElementsByTagName('title')->item(0)->nodeValue;
foreach ($doc->getElementsByTagName('meta') as $m){
$tag = $m->getAttribute('name') ?: $m->getAttribute('property');
if(in_array($tag,['description','keywords']) || strpos($tag,'og:')===0) $res[str_replace('og:','',$tag)] = $m->getAttribute('content');
}
return $specificTags? array_intersect_key( $res, array_flip($specificTags) ) : $res;
}
//use function on a variety of URLs passed through $url_db variable
$og_details = getSiteOG($url_db);
//echo image onto page
echo "<img class='preview_image_file image-fit-img' src='".@$og_details['image']."' alt='Preview image'>";
有时,当我通过 $url_db 变量传递 URL 并将图像回显到另一个页面时(如上面的最后一行代码所示),它可以正常工作并显示图像,但有时会导致“图像损坏”图标
正确呈现图像的示例 URL:https ://simplifaster.com/
显示损坏图像的示例 URL:https ://store.simplifaster.com/