php – 验证网址是Bing Search API的直接图片网址?
我正在尝试使用Bing Search API来允许用户将bing图像与网站上的帖子联系起来,一旦他们从那里选择图像搜索就将其保存到服务器中以供使用..但有时使用Bing搜索结果’ MediaUrl’返回的不是nessecarily图像.
示例1)搜索关键字Nascar 示例2)搜索关键字Jason Aldean <?php // Search Bing Api $articles = sitesearch('Jason Aldean',$_SERVER['HTTP_HOST'],$accountKey,4); $i = 0; // Process Results starting with reszing image within aspect ration to display to user foreach ($articles['d']['results'] as $article) { $i++; $dimensions = array( $article['Width'],$article['Height'] ); $dimensionsNew = array( 125,125 ); // What scale do we need to go to $scaleRequired = min($dimensionsNew[0] / $dimensions[0],$dimensionsNew[1] / $dimensions[1]); if ($scaleRequired < 1) { $twidth = $dimensions[0] * $scaleRequired; $theight = $dimensions[1] * $scaleRequired; // Resize to $finalDimensions } else { $twidth = $article['Width']; $theight = $article['Height']; } // Display images resized within ration to =< 125PX echo "<img alt='bimage' width='$twidth' height='$theight' src='" . $article['MediaUrl'] . "' style='magin: 10px !important; border: thin solid #666666;' /> "; } ?> 有没有办法验证给定的网址是或不是直接图片网址?! 解决方法
您也可以分析响应本身.它看起来像这样: /** * * @return boolean Whether image is "pure" */ function is_image_url($url,array $extensions){ $response = get_headers($url,1); foreach($response as $key => $value){ foreach($extension as $ext){ if (stripos($value,$ext) !== FALSE){ return TRUE; } } } return FALSE; } //Usage: $extensions = array('jpg','gif','bmp'); if ( is_image_url('http://exampe.com/image.jpg',$extensions) === TRUE ){ // It is. Do some stuff here } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |