我正在尝试使用Bing搜索API,允许用户将bing图像与网站上的帖子内联进行组合,一旦他们从那里选择了一个图像,就会将其保存到服务器上供使用。但有时,在必应搜索结果中,返回的'MediaUrl‘绝对不是一幅图像。
Nascar 示例1)搜索关键字
返回两个图像:
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 .该url实际上加载到www.fanpop.com/clubs/nascar/images/23962589/title/nascar-photo?ir=true..中。
搜索关键字Jason Aldean 示例2)
返回两个图像:
1) www.greenobles.com/data_images/jason-aldean/jason-aldean-01.jpg <-在<img src=''>中工作的实际图像url。
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?!
发布于 2013-03-21 17:19:20
是否有任何方法来验证给定的url是或不是一个直接的图像url?!
您也可以分析响应本身。它看起来是这样的:
/**
*
* @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
}发布于 2013-03-21 16:25:09
您可以使用curl来确定这一点。
CURLOPT_FOLLOWLOCATION设置为curl_setopt中的false,以使curl不遵循重定向。CURLOPT_HEADER到true以包含返回头信息。Location头集。Content-type判断内容是否是图像。关于curl:http://www.php.net/manual/en/book.curl.php的详细信息
https://stackoverflow.com/questions/13827851
复制相似问题