A while back, I decided to use the Picasa system as a way of managing my photographs on my home PC and publishing the photos to the web.
I was planning on using the API for the system as provided by google, but I am limited to using PHP4 by my web host. (The PHP API classes rely on PHP5). So, I decided to create a PHP4 class for reading my Picasa web albums, ready to be dropped onto my website.
The class is basically a custom XML paser - quite a fun little project for a boring weekend!
<?php
/*
Picasa Front-End Class (picasa.php)
============================================================================
Author: Richard Scrivens (webmaster@flashbulldog.co.uk)
Date: 14/06/2008
Revision Date: 14/06/2008
Copyright(c) Richard Scrivens 2008.
============================================================================
Properties:
$this->picasaUser
Picasa user to return items for. Set on class creation.
Methods:
picasa($user)
Class constructor. $user is a Picasa user to work with.
picasaGetAlbums($order = "date")
Returns an array of albums, with the elements listed below. $order can be "date" or "name". "date" is the modified date for the
album. FALSE is returned on failure.
["id"]
["published"]
["updated"]
["title"]
["location"]
["summary"]
["thumb"]
["image count"]
picasaGetAlbum($album)
Returns an arrays of photo details, with the elements listed below, or FALSE on failure.
["published"]
["title"]
["summary"]
["image full"]
["image thumb"]
*/
class picasa {
var $picasaUser = FALSE;
function picasa($user){
$this->picasaUser = $user;
}
function picasaGetAlbums($order = "date"){
$output = FALSE;
//Grab XML feed
if(($feed = @file_get_contents("http://picasaweb.google.com/data/feed/api/user/{$this->picasaUser}?kind=album")) !== FALSE){
//Split into entries
$entryArr = @explode("<entry>", $feed);
//Step through each entry
if(is_array($entryArr) && count($entryArr) > 1){
unset($entryArr[0]); //Throw away the first element
//Tags to grab in each entry
$tags = array(
"id"=>"|<gphoto:name>(.*)</gphoto:name>|U",
"published"=>"|<published>(.*)</published>|U",
"updated"=>"|<updated>(.*)</updated>|U",
"title"=>"|<title type='text'>(.*)</title>|U",
"location"=>"|<gphoto:location>(.*)</gphoto:location>|U",
"summary"=>"|<summary type='text'>(.*)</summary>|U",
"thumb"=>"|<media:thumbnail url='(.*)'|U",
"image count"=>"|<gphoto:numphotos>(.*)</gphoto:numphotos>|U"
);
//Step on
foreach($entryArr as $entryStr){
$entry = array();
//Find tags
foreach($tags as $tag=>$expr){
preg_match($expr, $entryStr, $matches);
if(!empty($matches[1])) $entry[$tag] = $matches[1];
}
//Add the entry to output?
if(!empty($entry) && $entry["image count"] > 0){
$entryNum++;
//Prep dates
$entry["published"] = substr(str_replace("T", " ", $entry["published"]), 0, 19);
$entry["updated"] = substr(str_replace("T", " ", $entry["updated"]), 0, 19);
//Make a sorting key
$sortKey = ($order == "name") ? "title" : "published";
//Drop in
$output[$entry[$sortKey] . $entryNum] = $entry;
}
}
//Sort array if there's some output
if(is_array($output)){
if($order == "name") ksort($output);
else krsort($output);
}
}
}
return $output;
}
function picasaGetAlbum($album){
$output = FALSE;
//Grab XML feed
if(($feed = @file_get_contents("http://picasaweb.google.com/data/feed/api/user/{$this->picasaUser}/album/{$album}?kind=photo")) !== FALSE){
//Split into entries
$entryArr = @explode("<entry>", $feed);
//Step through each entry
if(is_array($entryArr) && count($entryArr) > 1){
unset($entryArr[0]); //Throw away the first element
//Set up things to grab in each entry
$tags = array(
"published"=>"|<published>(.*)</published>|U",
"title"=>"|<title type='text'>(.*)</title>|U",
"summary"=>"|<summary type='text'>(.*)</summary>|U",
"image full"=>"|<media:content url='(.*)'|U",
"image thumb"=>"|<media:thumbnail url='(.*)'|U"
);
//Step on
foreach($entryArr as $entryStr){
$entry = array();
//Find tags
foreach($tags as $tag=>$expr){
preg_match_all($expr, $entryStr, $matches);
$valAdd = ($tag !== "image thumb") ? $matches[1][0] : $matches[1][1];
if(!empty($valAdd)) $entry[$tag] = $valAdd;
}
//Add the entry to output?
if(!empty($entry)){
//Prep date
$entry["published"] = substr(str_replace("T", " ", $entry["published"]), 0, 19);
//Prep full image
$lastSlash = strrpos($entry["image full"], "/");
$entry["image full"] = substr($entry["image full"], 0, $lastSlash) . "/s800" . substr($entry["image full"], $lastSlash);
//Drop in
$output[] = $entry;
}
}
}
}
return $output;
}
}
?>