2013-04-11 113 views
2

我正在使用picasa api來進行社區搜索,現在我也想在搜索結果中加上縮略圖。我該怎麼做呢?我希望結果具有縮略圖,並單擊縮略圖應該可以將您帶到網絡相冊中的圖像。picasa api的縮略圖

<?php 

require_once 'Zend/Loader.php'; 

Zend_Loader::loadClass('Zend_Gdata_Photos'); 
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 
Zend_Loader::loadClass('Zend_Gdata_AuthSub'); 
Zend_Loader::loadClass('Zend_Gdata_Query'); 

$search=$_REQUEST['id']; 

$serviceName = Zend_Gdata_Photos::AUTH_SERVICE_NAME; 
$user = "[email protected]"; 
$pass = "password"; 


$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $serviceName); 

$gp = new Zend_Gdata_Photos($client, "Google-DevelopersGuide-1.0"); 

// Community search queries aren't currently supported through a separate 
// class in the PHP client library. However, we can use the generic 
// Zend_Gdata_Query class as an alternative 

// URL for a community search request 
// Creates a Zend_Gdata_Query 
$query = $gp->newQuery("https://picasaweb.google.com/data/feed/api/all"); 

// looking for photos in the response 
$query->setParam("kind", "photo"); 

// full text search 
$query->setQuery($search); 

// maximum of 10 results 
$query->setMaxResults("6"); 

// There isn't a specific class for representing a feed of community 
// search results, but the Zend_Gdata_Photos_UserFeed understands 
// photo entries, so we'll use that class 
$userFeed = $gp->getUserFeed(null, $query); 
foreach ($userFeed as $photoEntry) 
{ 
echo $photoEntry->getTitle()->getText() . "</br > "; 
// The 'alternate' link on a photo represents the link to 
// the image page on Picasa Web Albums 
$link=$photoEntry->getLink('alternate')->getHref(); 
echo "<img src='".$link."'>"; 
echo '<a href="'.$link.'">'.$photoEntry->getLink('alternate')->getHref().'</a> <br />'; 
echo "<br />\n"; 

} 

?> 

回答

0

算法:

  1. 獲取照片的URL。
  2. 將URL解析爲其組成部分(方案,主機,路徑,文件名)。
  3. 在文件名之前注入縮略圖限定詞。
  4. 重新組裝零部件。

例如:

// Define thumbnail properties (w = width, 200 = pixels). 
$THUMBMAIL = "w200"; 

// Get the "source" URL for the image. 
$imgSrc = $photoEntry->content->getSrc(); 

// Extract the component parts. 
$url = parse_url($imgSrc); 

// Extract the path and inject the thumbnail size. 
$url["path"] = dirname($path) . "/$THUMBNAIL/" . basename($path); 

// Reassemble the URL. 
$thumbnail = build_url($url); 

您應該能夠使用http_build_url,但我有命名衝突,所以寫了這個:

function build_url($url) { 
    return $url["scheme"] . "://" . $url["host"] . "/" . $url["path"]; 
} 

閱讀this article瞭解更多詳情。