Advertisement
  1. Code
  2. JavaScript
  3. jQuery

How to Create an Image Gallery Powered by Picasa

Scroll to top
Read Time: 11 min

In this tutorial we are going to create a simple image gallery powered by Google's Picasa Web Albums. In order to enhance the user's experience, we'll throw some jQuery into the mix and create a scrollable album carousel.

Final ProductFinal ProductFinal Product

Overview

We are going to use PHP's SimpleXML extension to sort and access the data inside the XML feed provided by Picasa Web Albums. jQuery will be responsible for the DOM manipulation and AJAX request. We are also going to use the Scrollable library, which is part of jQuery Tools to create the carousel. Next, we'll use jQuery's MouseWheel plug-in to allow for cross-browser mouse wheel support. Finally, we'll use the Fancybox plug-in to each image in a "lightbox."

What We Need

Getting Started

We'll begin by downloading the files and putting them into the same folder. I also combined all of the Fancybox images with jQuery Tools ones, and placed them inside the img folder.

PHP files

For our project, we are going to use following PHP files:

  • index.php
  • _ajax.php
  • _conf.php
  • index.php will be responsible for displaying albums, images, and sending requests to _ajax.php.
  • _ajax.php will be handling dynamic request and returning formatted thumbnails
  • _conf.php, as you may have guessed, will contain some configuration information that will be used by both files.

_code.php

This file is very simple and short.

1
2
// First we need to set some defaults

3
$gTitle=""; // title of your gallary, if empty it will show: "your nickname' Photo Gallary"

4
$uName= "kozlov.m.a"; // your picasaweb user name

5
$tSize="72c"; // thumbnail size can be 32, 48, 64, 72, 144, 160. cropt (c) and uncropt (u)

6
$maxSize="720u"; // max image size can be 200, 288, 320, 400, 512, 576, 640, 720, 800. These images are available as only uncropped(u) sizes.

Basically, in this file we set the username (Picasa Web Album ID), thumbnail size, and max image size that we are going to show in our gallery.

index.php

This file requires a bit more to make the gallery work. We begin with referencing our configuration file (_conf.php):

1
2
<?php
3
include './_conf.php'; // getting constants

Next we need to load the album feed. We are only retrieving publicly available albums, so our request will look something like: "http://picasaweb.google.com/data/feed/api/user/user_name?kind=album&access=public".

1
<code>
2
$file = file_get_contents("http://picasaweb.google.com/data/feed/api/user/".$uName."?kind=album&access=public&thumbsize=".$tSize);
3
</code>

file_get_contents will load content from the XML feed into $file variable. As you can see, we used the $uName variable defined in _conf.php to get the right feed. We also passed the additional parameter "thumbsize;" so that the returned feed will contain thumbnails of our chosen size.

Now, let's convert the contents of the feed into a SimpleXml object and define the namespaces we are going to use:

1
2
$xml = new SimpleXMLElement($file);
3
$xml->registerXPathNamespace('gphoto', 'http://schemas.google.com/photos/2007');
4
$xml->registerXPathNamespace('media', 'http://search.yahoo.com/mrss/');

You can find all namespaces used in the API feeds by visiting http://code.google.com/apis/..., but we'll only be using "media" and "gphoto" in our tutorial; you do not have to worry about the rest of them.

Next, we'll get the web album's name in case we did not already set one in __conf.php file:

1
2
if($gTitle == null){ // if empty Gallery title will be "user id's  Photo Gallery"

3
$nickname = $xml->xpath('//gphoto:nickname'); // Mikhail

4
$gTitle =$nickname[0]."'s Photo Gallery";
5
}
6
?>

Finally, it is time for some simple HTML. We'll set our header and reference a few CSS files.

1
2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3
<html>
4
<head>
5
<title><?php echo $gTitle; ?></title>
6
<link rel="stylesheet" href="reset.css" type="text/css" />
7
<link rel="stylesheet" href="960.css" type="text/css" />
8
<link rel="stylesheet" href="style.css" type="text/css" />
9
<link rel="stylesheet" href="fancybox.css" type="text/css" />
10
</head>
11
<body>
12
<div class="container_16">
13
<div class="grid_16">
14
<?php echo "<h1>". $gTitle ."</h1>";?>

As you can see, we've set the page title to $gTitle and have added some CSS to make things pretty.

Style Files

I do not think that reset.css needs any additional explanation, so let's skip over it and take a closer look at the other stylesheet.

  • 960.css allows for a more grid-like layout.
  • style.css comes from the provided stylesheet from jQuery Tools.
  • And fancybox.css is part of the FancyBox plug-in.

Note: Please make sure that you change image path in both fancybox.css and style.css, so all background images point to img folder.

Album Holder and Navigational Elements

It is time to create our album holder and navigational elements. This is where jQuery Tools is a huge help. For the album navigation, we'll be using the Scrollable library. If you visit the Scrollable reference page and take a look at some of the examples, you'll see that we're using it almost without any modifications.

1
2
<div>
3
<a id="prev">&nbsp;</a> <!-- Prev controll-->
4
</div>
5
<div id="albums">
6
<div>
7
<!-- php code will go here -->
8
</div>
9
</div>
10
<div>
11
<a id="next">&nbsp;</a><!-- Next controll-->
12
</div>
13
<div>&nbsp;</div>
14
<div id="navi"></div> <!-- Pagination holder-->
15
<div>&nbsp;</div>

We'll also need a holder for the album picture thumbnails, and the album title that will be loaded via AJAX:

1
2
<h2 id="a_title"></h3>
3
<div id="pic_holder">
4
</div>
5
</div>
6
</div>

JavaScript

Let's finish our page by referencing the JavaScripts we'll be using.

1
2
<script type="text/javascript" language="JavaScript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
3
<script type="text/javascript" language="JavaScript" src="jquery.tools.min.js"></script>
4
<script type="text/javascript" language="JavaScript" src="jquery.easing.1.3.js"></script>
5
<script type="text/javascript" language="JavaScript" src="jquery.fancybox-1.2.1.pack.js"></script>
6
</body>
7
</html>

PHP

Now it is time to go through the XML file and sift the album thumbnails out. Place the following PHP code inside the </div class="items"> element.

1
2
<?php
3
foreach($xml->entry as $feed){
4
$group = $feed->xpath('./media:group/media:thumbnail');
5
$a = $group[0]->attributes(); // we need thumbnail path

6
$id = $feed->xpath('./gphoto:id'); // and album id for our thumbnail

7
echo '<img src="'.$a[0].'" alt="'.$feed->title.'" title="'.$feed->title.'" ref="'.$id[0].'" />';
8
?>
9
}

Our plan was to load album pictures once visitors click on a specific thumbnail, therefore we have to create some kind of reference to connect the two. For this purpose, we are putting a ref attribute into each album's img tag; so it will look something like this when compiled:

1
2
	<img ref="5364767538132778657" title="2009-07-31 - Beach Ocean City, MD" alt="2009-07-31 - Beach Ocean City, MD" src="http://lh4.ggpht.com/_X7imT2xUAEM/SnN7pvTzfqE/AAAAAAAAHmg/DNWeIS7JGzg/s72-c/20090731BeachOceanCityMD.jpg" />

AJAX

Finally, we'll spice it all up with some jQuery. Firstly, we need to initialize the jQuery Tools plug-in with some additional parameters:

1
2
$("div.scrollable").scrollable({
3
	size: 10, // number of pictures per "page"

4
	next: '#next', // id of next control element

5
	prev: '#prev', // id of prev control element

6
	navi:'#navi' // id of navigation control holder

7
});

The code above will automatically add scrollable controls.

Note: It is better to set the scrollable size to an odd number. This way, selected images will appear right in the middle.

Next we'll create on click event for the album thumbnail:

1
2
$("#albums img").bind("click", function(){
3
	$("#a_title").text($(this).attr('title'));
4
	$("#pic_holder").html('<div><img src="/images/loading.gif" alt="Loading..."></div>').load("_ajax.php",{"aID":$(this).attr("ref")},function(){
5
	$("#pic_holder").find("a").fancybox();
6
	});
7
});

Let's take close look at what we are doing here. First we define our click event trigger:

1
2
$("#albums img").bind("click", function(){

We use bind instead of the simple click because we do not want to interrupt the work of the scrollable plug-in that we just initiated above.

Next, we'll apply the album title into the h2 tag with id "a_title" from the title attribute of the anchor tag:

1
2
	$("#a_title").text($(this).attr('title'));

Finally, we send an AJAX request to _ajax.php and let Fancybox re-index the freshly loaded images:

1
2
$("#pic_holder").html('<div><img src="/images/loading.gif" alt="Loading..."></div>').load("_ajax.php",{"aID":$(this).attr("ref")},function(){
3
 	$("#pic_holder").find("a").fancybox();
4
});

As you probably noticed, we are inserting a "loading image" inside "pic_holder" before sending the AJAX request. This will provide the user with some feedback to let them know that their request is currently being processed. Once we receive a response from the server, jQuery will replace the contents of the "pic_holder" with data that came from _ajax.php.

_ajax.php

Now it is time to serve the contents of the album to our visitors. Our plan is to show thumbnails linked to originals on the Picasa server. Once a thumbnail is clicked, Fancybox will take over and create a lightbox-like image gallery. We'll start with the entire contents of the file, and then go over each line:

1
2
<?php
3
	include './_conf.php'; // getting constants

4
	if(isset($_POST['aID'])){
5
		$aID = $_POST['aID']; // let's put album id here so it is easie to use later

6
		$file = file_get_contents('http://picasaweb.google.com/data/feed/api/user/'.$uName.'/albumid/'.$aID.'?kind=photo&access=public&thumbsize=72c&imgmax='.$maxSize); // get the contents of the album

7
		$xml = new SimpleXMLElement($file); // convert feed into SimpleXML object

8
		$xml->registerXPathNamespace('media', 'http://search.yahoo.com/mrss/'); // define namespace media

9
		foreach($xml->entry as $feed){ // go over the pictures

10
		$group = $feed->xpath('./media:group/media:thumbnail'); // let's find thumbnail tag

11
		$description = $feed->xpath('./media:group/media:description'); // file name appended by image captioning

12
		if(str_word_count($description[0]) > 0){ // if picture has description, we'll use it as title

13
			$description = $feed->title. ": ". $description[0];
14
		}else{
15
			$description =$feed->title; // if not will use file name as title

16
		}
17
		$a = $group[0]->attributes(); // now we need to get attributes of thumbnail tag, so we can extract the thumb link

18
		$b = $feed->content->attributes(); // now we convert "content" attributes into array

19
		echo '<a rel="'.$aID.'" href="'.$b['src'].'" title="'.$description.'"><img src="'.$a['url'].'" alt="'.$feed->title.'" width="'.$a['width'].'" height="'.$a['height'].'"/></a>';
20
		}
21
	}else{
22
		echo 'Error! Please provide album id.';
23
	}
24
?>

First, we are going to reference our configuration file, so we can have access to the constant parameters: Picasa ID and thumbnail size.

1
2
include './_conf.php'; // getting constants

Then we'll check if the album ID was sent via POST request:

1
2
if(isset($_POST['aID'])){

If we did not find an album ID, we're simply going to print an error message:

1
2
}else{
3
   echo 'Error! Please provide album ID.';
4
}

If _ajax.php received the album ID, we'll get an XML feed and start working on it, so let's create a link to the correct XML feed:

1
2
 $aID = $_POST['aID']; // let's put the album id here so it is easier to use later

3
 $file = file_get_contents('http://picasaweb.google.com/data/feed/api/user/'.$uName.'/albumid/'.$aID.'?kind=photo&access=public&thumbsize=72c&imgmax='.$maxSize); // get the contents of the album

As you can see, we use the album ID that came via the POST request as well as constants from _conf.php file. Again, we are using file_get_contents to load the XML feed and store it in the $file variable. Next we convert it to a SimpleXMLElement object cycle through each entry nodes that contain information about each picture. To do so, we'll use a simple foreach() loop.

1
2
foreach($xml->entry as $feed){ // go over the pictures

Next, we are ready to extract data needed for our link and thumbnail. I've commented every line and hopefully it is enough to understand what is going on:

1
2
$group = $feed->xpath('./media:group/media:thumbnail'); // let's find the thumbnail tag

3
$description = $feed->xpath('./media:group/media:description');  // let's find the description tag

4
if(str_word_count($description[0]) > 0){ // if the picture has description, we'll use it as the title

5
	$description = $feed->title. ": ". $description[0]; // file name appended by image captioning

6
}else{
7
	$description =$feed->title; // if not, will use file name as title

8
}
9
$a = $group[0]->attributes(); // now we need to get attributes of thumbnail tag, so we can extract the thumb link

10
$b = $feed->content->attributes(); // now we convert "content" attributes into array

Finally, we are putting it all into HTML context. We'll echo a link to the original image and thumbnail image:

1
2
echo '<a rel="'.$aID.'" href="'.$b['src'].'" title="'.$description.'"><img src="'.$a['url'].'" alt="'.$feed->title.'" width="'.$a['width'].'" height="'.$a['height'].'"/></a>';

To force Fancybox to organize all of the images into a gallery, we are adding the rel attribute to each link. You can simply put same number or string as value, but I'm going to use the album ID.

Styling

As I mentioned before, most of the styling was taken straight from examples at the jQuery Tools website. All you must do here is simply adjust the height and width to suit the design of your website.

Mouse Wheel Scroll

Mouse Wheel Scroll is another beauty that you can easily use. As some may have noticed, we never initiated this plug-in; yet, if you hover over the album carousel and try to scroll with your mouse wheel, it will work thanks to jQuery Tools.

Conclusion

We've learned how to combine PHP's SimpleXML extension with a handful of plugins and Picasa to create a beautiful and dynamic image gallery. I hope you enjoyed and learned from it!


Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.