我正在尝试使用必应搜索 API 来允许用户将必应图像与网站上的内联帖子关联起来,一旦他们从那里搜索选择图像,它就会将其保存到服务器以供使用。但有时使用必应搜索结果返回的 MediaUrl' 不一定是图像。
示例 1) 搜索关键字Nascar
返回两个图像:
1) www.betbigdc.com/wp-content/uploads/2010/02/nascar1.jpg <- 实际上是图像,我可以使用,在<img src=''>
.
2) http://images4.fanpop.com/image/photos/23900000/NASCAR-nascar-23962589-425-425.jpg .. 该网址实际上加载到 www.fanpop.com/clubs/nascar/images/23962589/标题/nascar-photo?ir=true..
示例 2) 搜索关键字Jason Aldean
返回两个图像:
1) www.greenobles.com/data_images/jason-aldean/jason-aldean-01.jpg <- 在<img src=''>
.
2) http://www.cmtradiolive.com/wp-content/uploads/2009/10/jason-aldean.jpg实际加载网页的有趣网址。
<?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;' /> ";
}
?>