Thursday, December 11, 2008

Embed Picasa albums in your own site

Recently I spent some significant time looking for a way to embed online photo albums into the North Dakota Junior Chamber website. There was a great option called PictoBrowser if our hosting service had been flickr. However as we already use an embedded Google calendar it made much more sense for us to be using the Google alternative Picasa.

Try as I might, I could not find a widget or script to pull Picasa albums and photos. So I wrote my own. It's not pretty, and pure php so no fancy java or anything. But it pulls the xml from Picasa and embeds everything inside our website. For now I'm happy.



<?php

/*** Variables *************************************************************************************/
$user = 'juspar'; //user account name
$thumbsize = '160c'; //possible values:
//32, 48, 64, 72, 144, 160 with a 'u' uncropped or 'c' cropped suffix
//200, 288, 320, 400, 512, 576, 640, 720, 800 only uncropped, no sufix necessary
$outputsize = '512'; //possible values:
//200, 288, 320, 400, 512, 576, 640, 720, 800
/*******************************************************************************************************/

if($photo)
{
// build photos feed URL
$urlFeed = "http://picasaweb.google.com/data/feed/api/user/$user/album/$album?thumbsize=$outputsize";

// read feed into SimpleXML object
$sxml = simplexml_load_file($urlFeed);

foreach ($sxml->entry as $entry)
{
// get nodes in media: and gphoto: namespace
$media = $entry->children('http://search.yahoo.com/mrss/');
$gphoto = $entry->children('http://schemas.google.com/photos/2007');

//read thumbnail attributes
$attribs = $media->group->thumbnail[0]->attributes();

//assign url attribute to variable
$thumbnail = $attribs['url'];

//assign photo id variable
$gphotoid = $gphoto->id;

//asign photo caption to a variable
$caption = $entry->summary;

//output match
if($gphotoid==$photo)
{
echo"<p class='photo'><a href='http://picasaweb.google.com/$user/$album#$photo' target='_blank'>"
."<img src='$thumbnail' alt='$caption' /><br />$caption</a></p>";
}
}
}

if ($album)
{
//build album feed URL
$urlFeed = "http://picasaweb.google.com/data/feed/api/user/$user/album/$album?thumbsize=$thumbsize";

// read feed into SimpleXML object
$sxml = simplexml_load_file($urlFeed);

// get album names and number of photos in each
foreach ($sxml->entry as $entry)
{
// get nodes in media: and gphoto: namespace
$media = $entry->children('http://search.yahoo.com/mrss/');
$gphoto = $entry->children('http://schemas.google.com/photos/2007');

//read thumbnail attributes
$attribs = $media->group->thumbnail[0]->attributes();

//assign url attribute to variable
$thumbnail = $attribs['url'];

//assign photo id variable
$photo = $gphoto->id;

echo "<p class='thumbnail'><a href='../index.php?type=guest&page=photos&user=$user&album=$album&photo=$photo'>"
."<img src='$thumbnail' alt='' border=0 /></a></p>";
}
}
else
{
//build albums feed URL
$urlFeed = "http://picasaweb.google.com/data/feed/api/user/$user?kind=album&thumbsize=$thumbsize";

// read feed into SimpleXML object
$sxml = simplexml_load_file($urlFeed);

// get album names and number of photos in each
foreach ($sxml->entry as $entry)
{
// get nodes in media: and gphoto: namespace
$media = $entry->children('http://search.yahoo.com/mrss/');
$gphoto = $entry->children('http://schemas.google.com/photos/2007');

//assign variables from xml
$album = $entry->title;
$attribs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attribs['url'];
$numphotos = $gphoto->numphotos;

//tidy up album names for link
$albumlink = str_replace(" ","",$album);
$albumlink = strtolower($albumlink);

echo "<p class='thumbnail'><a href='../index.php?type=guest&page=photos&user=$user&album=$albumlink'>"
."<img src='$thumbnail' alt='$album' border=0 /><br />$album<br />$numphotos photo(s)</a></p>";
}
}
?>