How To: Integrate Google Picasa Onto Your Own Website Using PHP
I mentioned when I posted about finishing a website that I had made some custom scripts, and this was one of them.
This assumes you have all the Google PHP scripts ready on your site, comments have been made at appropriate areas. The HTML included is meant to be styled and is just the structure I used to make it. You can see the implementation right over yonder.
Features:
- Show all Picasa albums
- Page through photos
- View in Lightbox-style popups, thanks to Paul Armstrong’s AwesomeBox script and YUI.
It’s tested and working, make sure you configure it properly. As always, feel free to add on as you please.
This was a quick script so I’d add some more error checking if I were you. It doesn’t use classes or anything, it’s just down and dirty.
Download Source Code (.zip)
gallery.php
<?php
/************************************************************
**** Picasa and PHP Integration Script
**** Created by: Kamran Ayub (c)2008 Intrepid Studios, Inc.
**** http://www.intrepidstudios.com/
**** This is just a very barebones implementation! No fancy
**** classes and whatnot. Feel free to improve it.
****
**************************************************************/
//set_include_path("/local/home/"); // set if Zend folder isn't in the same directory as gallery.php, e.g. it's below it
// Vars
$user = "YOUR_GOOGLE_USERNAME";
$pass = "YOUR_PASSWORD";
$albumId = $_REQUEST['albumId'];
$albumName = $_REQUEST['albumName']; // This is just lazy, because when you are using the newAlbumQuery method, the name of the album can be gotten anyway.
$page = $_REQUEST['page'];
$maxResults = 15; // Max Results per page
$maxImageSize = 800; // don't go over 800 otherwise you won't be able to embed the larger photos
$zend_dir = "Zend/Loader.php"; // should point to your Zend directory
// Functions
function Paginate($numPages,$currentPage,$albumName,$albumId) {
// Create page links
$s = "<ul class='page-nav'>\n";
for($i=1;$i<=$numPages;$i++) {
$class = "";
// Current page?
if($i == $currentPage) {
$class = " class='selected'";
}
$s .= "<li".$class.">";
$s .= "<a href='?albumId=".$albumId."&albumName=".$albumName."&page=".$i."'>".$i."</a></li>\n";
}
$s .= "</ul>\n";
return $s;
}
// Picasa Web Albums
require_once($zend_dir);
Zend_Loader::loadClass('Zend_Gdata_Photos');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
// Authenticate
$serviceName = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $serviceName);
// update the second argument to be CompanyName-ProductName-Version
$gp = new Zend_Gdata_Photos($client, "Google-DevelopersGuide-1.0");
// Default page
if(!isset($page)) {
$page=1;
}
if(isset($albumName)) {
$albumName = urldecode($albumName);
}
?>
<html>
<head>
<title>Integrate Picasa and PHP Demo</title>
<link rel="stylesheet" type="text/css" href="base.css" media="all" />
<link rel="stylesheet" type="text/css" href="gallery.css" media="all" />
<link rel="stylesheet" type="text/css" href="awesomebox.css" media="all" />
<!-- YUI, older -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/animation/animation-min.js"></script>
<script src="awesomebox.js" type="text/javascript"></script>
</head>
<body>
<h1>Photo Gallery</h1>
<div class="albums">
<h3>Albums</h3>
<?php
try {
$userFeed = $gp->getUserFeed("default");
echo "<ul>\n";
foreach ($userFeed as $userEntry) {
echo "<li><a href='gallery.php?albumId=". $userEntry->gphotoId->text . "&albumName=". urlencode($userEntry->title->text) ."'>". $userEntry->title->text . "</a></li>\n";
}
echo "</ul>\n";
//print_r($userFeed); // Debug
} catch (Zend_Gdata_App_HttpException $e) {
echo "Error: " . $e->getMessage() . "<br />\n";
if ($e->getResponse() != null) {
echo "Body: <br />\n" . $e->getResponse()->getBody() .
"<br />\n";
}
// In new versions of Zend Framework, you also have the option
// to print out the request that was made. As the request
// includes Auth credentials, it's not advised to print out
// this data unless doing debugging
// echo "Request: <br />\n" . $e->getRequest() . "<br />\n";
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage() . "<br />\n";
}
?>
</div>
<?php
// List photos from album
if(!isset($albumId)) {
?>
<div class="grid">
<p>Choose a photo album to view.</p>
</div>
<?php
} else {
$query = $gp->newAlbumQuery();
$query->setUser("default");
$query->setAlbumId($albumId);
$query->setImgMax($maxImageSize);
$query->setMaxResults($maxResults);
if(isset($page)) {
$query->setStartIndex((($page-1) * $maxResults)+1);
}
?>
<h3>Photos from <?= $albumName ?></h3>
<div class="grid">
<?php
try {
$albumFeed = $gp->getAlbumFeed($query);
// Number of results
$numResults = $albumFeed->gphotoNumPhotos->text;
// You should probably check if $numResults is a number...
// If there are more than $maxResults, we need to paginate this...
$numPages = ceil($numResults / $maxResults);
if($numPages > 1) {
echo Paginate($numPages,$page,$albumName,$albumId);
}
foreach ($albumFeed as $photoEntry) {
$contentUrl = "";
$thumbnailUrl = "";
if ($photoEntry->getMediaGroup()->getContent() != null) {
$mediaContentArray = $photoEntry->getMediaGroup()->getContent();
$contentUrl = $mediaContentArray[0]->getUrl();
}
if ($photoEntry->getMediaGroup()->getThumbnail() != null) {
$mediaThumbnailArray = $photoEntry->getMediaGroup()->getThumbnail();
$thumbnailUrl = $mediaThumbnailArray[1]->getUrl();
}
echo "<div class='photo'><a rel='gallery' href='".$contentUrl."'><img src='" . $thumbnailUrl . "' alt='" . $photoEntry->title->text ."' title='" . $albumName ."' /></a></div>\n";
}
//print_r($albumFeed);
echo "<div style='clear:both;margin-bottom:6px;'> </div>";
if($numPages > 1) {
echo Paginate($numPages,$page,$albumName,$albumId);
}
} catch (Zend_Gdata_App_HttpException $e) {
echo "Error: " . $e->getMessage() . "<br />\n";
if ($e->getResponse() != null) {
echo "Body: <br />\n" . $e->getResponse()->getBody() .
"<br />\n";
}
// In new versions of Zend Framework, you also have the option
// to print out the request that was made. As the request
// includes Auth credentials, it's not advised to print out
// this data unless doing debugging
// echo "Request: <br />\n" . $e->getRequest() . "<br />\n";
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage() . "<br />\n";
}
?>
</div>
<?php
}
?>
</body>
</html>
