How To: Integrate Google Picasa Onto Your Own Website Using PHP | Posted at 12:19 AM
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>

Comments
Hi,
The link to the source code is broken. Do you still have the source for the Picasa thing Im interested in giving it a try.
Thanks and regards
Posted by: Neil Terry
On February 6, 2009 5:24 AM
Oops, forgot to copy the folder to the new site.
Should work now!
Posted by: Kamran
On February 6, 2009 9:07 AM
Hey, thank you for the source. Great content here in thios website! :)
Posted by: Daniel Su
On February 9, 2009 2:19 AM
thank you for sharing.
i choose script in next project.
Posted by: 9neo
On February 17, 2009 3:18 PM
It's not working right now....
Posted by: eleven
On February 24, 2009 2:12 PM
I have downloaded the zip. In requirements txt file you have mentioned that it will need Zend folder present to run the demo..
What does that mean??
pls explain
Posted by: Suyash
On August 24, 2009 6:18 AM
How do you restrict this script to only public albums?
Posted by: themoviebuff
On September 1, 2009 11:12 PM
Where do you get the Zend folder? It would be helpful to include that information in your post. Thanks!
Posted by: jes
On November 12, 2009 3:39 PM
The only Zend library I have access to is located here for me:
/usr/local/Zend/lib/Optimizer-3.3.9/php-5.2.x/ZendOptimizer.so
Though I am getting this error from your script.
Parse error: syntax error, unexpected T_VARIABLE in /usr/local/Zend/lib/Optimizer-3.3.9/php-5.2.x/ZendOptimizer.so on line 686
Since I don't have direct access to the Zend libraries to change them. Any suggestions..? Thanks
Posted by: David Wiedeman
On November 1, 2010 10:07 AM
Tnx man. I was looking for solution like this for almost 2 days. I can finaly get client of my back.
Posted by: John G
On December 11, 2010 9:12 AM
I was looking for this code since long and today i got it. very well done for posting this if i have any problem in integrating i will post again.
thanks.
Posted by: php scripts
On February 16, 2011 12:09 PM
Wow! Really useful information for those who are interested in incorporating Picasa to a website using PHP. PHP codes can be really intimidating for beginners who wanted to have their own website but with bloggers like you, people were able to get what they wanted! Continue making these cool stuff!
Posted by: Swiss Web Hosting
On March 18, 2011 2:31 AM
Its working for mee tooo.
Pls help me to find to Integrate Flickr Onto our Own Website Using PHP
Posted by: preethi
On December 13, 2011 10:12 PM
Hey! this is nice article & script are great. thanks for sharing.
Posted by: Kevin
On April 4, 2012 3:57 AM