Designs | Resume | KamranAyub.com | email: ayubx003 (@umn.edu)

« I Guess I Am Now a BAM Board Member [More Work] | Main | Braid, Flogging Molly, Football, and so on… [Fun Weekends] »

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:

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."&amp;albumName=".$albumName."&amp;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 . "&amp;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;'>&nbsp;</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>


Filed Under: How To Tips and Tricks Web & Internet

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

Oops, forgot to copy the folder to the new site.

Should work now!

Hey, thank you for the source. Great content here in thios website! :)

thank you for sharing.
i choose script in next project.

It's not working right now....

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

How do you restrict this script to only public albums?

Where do you get the Zend folder? It would be helpful to include that information in your post. Thanks!

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

Tnx man. I was looking for solution like this for almost 2 days. I can finaly get client of my back.

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.

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!

Its working for mee tooo.
Pls help me to find to Integrate Flickr Onto our Own Website Using PHP

Hey! this is nice article & script are great. thanks for sharing.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About the Author

Kamran
Divide by Zero is Kamran Ayub's personal blog. Kamran owns and operates Intrepid Studios, a web design and development firm based in Minneapolis. Despite the above picture, Kamran is usually an intelligent person. Please forgive him.

View Kamran Ayub's profile on LinkedIn

Add to Technorati Favorites