【发布时间】:2012-12-11 20:19:47
【问题描述】:
我正在尝试使用必应搜索 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/title/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;' /> ";
}
?>
有什么方法可以验证给定的 url 是否是直接图片 url?!
【问题讨论】:
标签: php javascript jquery api bing