Building Your Own Picasa Gallery

Sunday, April 13, 2008 at 11:29 PM
Since the release of version 3 of my Picasa API I've received some feedback from users that it would be helpful to have some sample code for those who want to do the basics: host their own gallery. I can relate with this, since it's the reason I built the API in the first place. Sure, you can do a lot of other cool stuff with the API, like uploading and managing photos, but certainly the typical adopter is just going to want their own Picasa pictures viewable from their personal homepage. So here is a quick rundown of how to do a basic gallery using the Lightweight PHP Picasa API, version 3.0.

As you might already know, my gallery consists of three types of pages, which I will refer to as an account page (seen here), an album page (for instance, here), and an image page (for instance, over here). It's pretty self explanatory, the account page lists all the albums, the album page lists all the photos in an album, the image page shows a larger version of the selected image. For each page type, you'll need some kind of information that you can store in a constant or get from GET/POST data. I'll go over each page type and list source code along with what information is required.

Account Page
For the account page, the only thing you'll need is the username. I recommend storing the username in a constant variable somewhere where all your code can access. This is so that if your username changes, or you want to give your code to someone else, you only have to change the username in one place. I store mine in a variable called "Cam_Util_PictureUtil::$USERNAME". However, for the sake of readability, I'll just refer to it in the following code as "$username". Here's the code for the account page, which just outputs each album's icon with a link to the album page. I use a URL structure similar to what my site uses, but modified to be closer to the typical setup. And obviously you can change the HTML to suit your needs:
$pic = new Picasa();
$account = $pic->getAlbumsByUsername($username);
$albums = $account->getAlbums();
foreach ($albums as $album) {
print('<a href="http://www.your_web_site.com/album.php?
albumid='.$album->getIdnum().'">
<img src="'.$album->getIcon().'" />');
print('<div>'.$album->getTitle().'</div>');
}


Album Page
For the album page, you'll not only need the username, but you'll also need the album id. If you use the URL structure used in the previous example, you should be able to get the album id from the $_GET superglobal. I'll include how to do that in the code sample. So the album page just prints each image in the album with a link to the Image Page and the album title displayed below:
$albumid = $_GET["albumid"];
$pic = new Picasa();
$album = $pic->getAlbumById($username, $albumid);
$images = $album->getImages();
foreach ($images as $image) {
print('<a href="http://www.your_web_site.com/image.php?
albumid='.$albumid.'&imageid='.$image->getIdnum().'">
<img src="'.$image->getMediumThumb().'"/>');
print('<div>'.$image->getTitle().'</div>');
}

Image Page
Lastly for the image page, as you might have guessed, you'll need the username, album id, and image id. Again, using the above example, you get the image id from the $_GET superglobal. The image page prints the image, the image title, and the description of the image:
$albumid = $_GET["albumid"];
$imageid = $_GET["imageid"];
$pic = new Picasa();
$image = $pic->getImageById($username,$albumid, $imageid);
print ('<img src="$image->getLargeThumb().'" />');
print('<div>'.$image->getTitle());
print('<div>'.$image->getDescription().'</div></div>');

Unfortunately the current implementation of Picasa::getImageById() doesn't support any image sizes other than the defaults (which are pretty small). This was an oversight on my part because there was a workaround that made it not necessary. Unfortunately, due to a change in Google's API, the workaround no longer actually works. I've emailed the Picasa development mailing list to find out if there is another workaround that will work now, but I've also entered an enhancement request in my project's Google Code page. As soon as I fulfill that request, I'll post a quick tutorial on how to get larger sized images. It should be less than a week for that to happen.

Hopefully this tutorial has been helpful. Thanks to everyone who has given me feedback on the API, so far I've only heard good things. If something's not quite working right or it seems there's something wrong with the tutorial here, please let me know. And check back in the next week, I should have the fix for getting larger image sizes available for download, and I'll include a bit of code on displaying and posting comments for your images.

Comments

Forget my last comment... The string seems to determine the size of the image.
Posted by var3tas on Thursday, March 28, 2013 at 8:28 AM.
Correction to my last post!

Replace "s288" for "s640" and the image will be even bigger.
Posted by var3tas on Thursday, March 28, 2013 at 8:26 AM.
There is an "ugly" solution for the problem of the small images! Just do a string replace on the "getLargeThumb()" method and replace s288/ for an empty string.
Posted by var3tas on Thursday, March 28, 2013 at 8:21 AM.
It is a nice API and nice tutorial too.
It helped me a lot to figure out what I want in photoclub. Thank you a lot.
Posted by Sidd on Sunday, August 30, 2009 at 11:24 AM.
Quick reply!
You were right. By default I was setup to run php 4. It was easily fixed. Thanks.
Posted by Gerald on Tuesday, August 11, 2009 at 10:12 AM.
Those errors look a lot like you're using PHP4 instead of PHP5. The other common problem people have is they do not have fopen turned on but you would probably be receiving a different error in that case. Try to find out for sure what version of PHP you're running.
Posted by Cameron on Sunday, August 9, 2009 at 2:53 PM.
I tried using your API and got nothing but Fatal errors. I'm not sure what I did wrong. I'm an intermediate PHP developer and have a solid understanding of PHP syntax. I looked through all of your files and didn't notice anything syntactically wrong so I guess it could be something wrong with my server. I'm hosting on GoDaddy and I think they have the latest version of PHP installed, but I don't know off the top of my head.

Anyway, any assistance would be great.

Here is the code I'm using:

require_once 'Picasa.php';

$username = "erinnefullam";

$pic = new Picasa();
$account = $pic->getAlbumsByUsername($username);
$albums = $account->getAlbums();
foreach ($albums as $album) {
print('<a href="http://www.fullamphotography.com/albums.php?
albumid='.$album->getIdnum().'">
<img src="'.$album->getIcon().'" />');
print('<div>'.$album->getTitle().'</div>');
}


And the Error Message:
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/content/e/f/u/efullam/html/Picasa.php on line 63
Posted by Gerald on Sunday, August 9, 2009 at 2:50 PM.
Thank you Cameron for sharing this.
It works great for me.
Posted by Bruno on Sunday, July 5, 2009 at 2:30 AM.
Thank you Cameron for sharing this.
It works great for me.
Posted by Bruno Simões on Sunday, July 5, 2009 at 2:30 AM.
Very nice api indeed!

Took me a while to understand some of it though, and I can understand that people who are not used to dive into classes could have a hard time getting things to work.

Anyway, to get the larger images (I went with 640) I did this:

$pic = new Picasa();
$username = "Your_Username";

$album = $pic->getAlbumById($username, $albumid);
$images = $album->getImages();

foreach ($images as $image) {

$imagelink = $pic->getImageById($username, $albumid, $image->getIdNum(), 640, 640);

print('<a href="'.$imagelink->getContent().'">';
print('<img src="'.$image->getMediumThumb().'"></a>';
}

Cheers! :D
Posted by Pierre on Sunday, April 26, 2009 at 3:57 PM.
Sorry you're having trouble, Kenneth. Feel free to email me your code and I can take a look.
Posted by Cameron on Tuesday, April 14, 2009 at 8:44 PM.
Thanks for the sample code. But when I tried it, I am not able to make the Account Page sample to work. The getAlbums does not return the albums. The getAlbumsByUsername does work.

I am using PHP 5.16 with API 3.2

Thanks
Posted by Kenneth on Tuesday, April 14, 2009 at 8:17 PM.
I'm having trouble with authorizeWithAuthSub().

When I redirect to Google using: redirectToLoginPage(myUrl), I click 'grant access' at the Google auth page and am returned to my app.

The querystring of the URL I'm returned to has ?token=xxx, the request method is get...but somehow the $_GET array is empty.

That doesn't make any sense.

So I refresh the page manually using the same URL/querystring and get a Picasa_Exception_FailedAuthorizationException.

What's going on?
Posted by jcorry on Friday, February 13, 2009 at 5:30 PM.
Sorted! Just wanted to let you know to save the trouble of posting a reply...

For anyone else who's wondering, you can set the include path as follows:

ini_set('include_path',ini_get('include_path').'.:/home/path/to/picasa/apifolder/');
Posted by JRC on Tuesday, July 8, 2008 at 1:27 AM.
I talked to my hosting company, and they said fopen is off-- which I figure is probably the cause of this and other similar scripts not working for me.

They said I can use curl.
Posted by Patrick on Monday, July 7, 2008 at 3:02 AM.
Hi there

Really excited about using your api, but I just can't get it to work. I'm having the same problem as anonymous - i.e. the paths aren't working. I'm on a shared host so can't place the api within the include path (afaik) - I've tried using ini_set at the top of my page, but that's not working either - can you help?

While I'm here - thanks for your hard work on all this - just hope I can get it working at some point!
Posted by JRC on Sunday, July 6, 2008 at 10:43 PM.
No problem, Pat, it happens to the best of us. And again, I'm available via email if you still want to try to get it to work.
Posted by Cameron on Saturday, July 5, 2008 at 10:23 AM.
I was obviously out of line with that comment-- and I apologize.
Posted by Patrick on Friday, July 4, 2008 at 6:32 PM.
Hmm, I'm not sure what you're doing wrong, I haven't heard of other people having problems like that. If you want me to try to figure out where you're making mistakes, I'm pretty responsive via email.

Sorry you're getting so worked up, I can sense the frustration in your tone. But that's part of being a programmer I guess. Reminds me of a comic I saw on a coworker's wall. Haven't we all been there?
Posted by Cameron on Friday, July 4, 2008 at 5:28 PM.
I thought this was going to be a simple and easy solution to what I wanted to do: Display my picasa galleries on my page.

But I get nothing but errors. After spending an hour fixing your include paths(you have paths to files in different directories without the directory included in the path, and paths to files in the same directory pointing to different directories).. I thought OK now it will work.. include paths are different for everyone.

Nope.

I used your code from this page and got nothing but fatal errors. Not sure why. I can't post the code on here to show you either.

Probably should have left it with the simple version-- which I wanted to download but you changed the link for that one to this bloated and broken version.
Posted by Anonymous on Friday, July 4, 2008 at 2:21 PM.
Great work! I have been looking for this for a very long time!
It works great for me, your gallery example. The only thing is the small size if the pictures. Just letting you know that I love to hear a solution!
Posted by Wen V on Tuesday, July 1, 2008 at 2:17 PM.
Yes, I did look into this with the Picasa mailing list. It is Picasa's policy to only allow embedded images of 800px and less. So to get a larger version of an image you would call Picasa::getImageById() and pass in a valid pixel size for either imgmax or thumbsize. Valid values are, according to the Picasa documentation:

200, 288, 320, 400, 512, 576, 640, 720, 800

For me, this is large enough. If you need a larger version you can still get it, but you won't be able to embed it in your page, you can only have the user explicitly download it to their hard drive and open it. Valid values for that are:

912, 1024, 1152, 1280, 1440, 1600

If you notice on Picasaweb, they follow the same rules. The main image is 720px and I believe the download version is 1600px. Keep in mind that these values represent the longest side of the image, not the top or side. I could update my Pictures section to use larger images but I think the default size is fine for a preview; users can click on the image for a larger one displayed via LightBox.

Unfortunately using thumbnails can be a bit of a hassle with my API (Issue 4 in the issue list). I think the current implementation was born from a 3am coding session. I'm probably going to undeprecate the fields that I previously deprecated to put in place the current system, and I'll add a new way for retrieving them. I'm not sure when I'll have time to do this, particularly with me getting married in 2 months, but I'm pretty confident I'll get back to working on the API soon. I've been working in Eclipse since starting at Nike and am looking forward to some Vim time.
Posted by Cameron on Sunday, June 29, 2008 at 12:08 AM.
hey,
is there anyway to see the normal size image that i would see via google's picasaweb interface? i read that you contacted google about that; just curious if any response since april? it's kind of small the current one :)
thanks,
matthew
Posted by Anonymous on Saturday, June 28, 2008 at 11:20 PM.