Intrepid Studios, Inc. Blog - Minneapolis, MN - .NET, XHTML, CSS, Adobe, web, graphic, media, and software design services

Learn more:

Overview

Intrepid Studios, Inc. is a software and web studio located in Minneapolis, MN that provides professional services for the web and desktop. We offer web design and development, graphic design, application design and development, and a host of other solutions.

Our Network

Previous Posts

Archives

 Subscribe in a reader

Powered by Blogger

Blog

Friday, August 8, 2008

How To: Integrate Google Picasa Onto Your Own Website Using PHP

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>

Labels: , ,

» Read more...

Monday, August 4, 2008

How To: Use PHP to Grab Event Date from Google Calendar RSS [Tips & Tricks]

Recently I finished a site that required me to use Google Calendar to manage events. I thought that it would be nice to show what events are up and coming, so I used SimplePie to grab and show their calendar feed.

However, the RSS date was the date the event was added, not the date that the event happens. This makes sense but makes it a bit harder to be useful in the website.

I used some simple RegEx to get the event date… it’s rough but it works and I’ve tested it with events spanning multiple dates, all day, and specific times.

<dl>
    <?php
    // Feed: http://www.google.com/calendar/feeds/[google account]/public/basic?max-results=5
    foreach ($feed->get_items() as $item):
        
        // Grab When: date.
        $content = $item->get_content();
        $content = strip_tags($content);
        
        if(preg_match("/When: (.*!?)/", $content, $matches)) {
            $content = $matches[1];
        } else {
            $content = "No Date Found";
        }
    ?>
        <dt>Event:</dt>
        <dd><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></dd>
        <dt>Event Date:</dt>
        <dd><?php echo $content; ?></dd>
    <?php endforeach; ?>
</dl>

This assumes you have a SimplePie feed… you can just use the RegEx in any other implementation instead…

Labels: , ,

» Read more...

Website Release - BAM

bam-screencap

I am proud to announce that Intrepid Studios has finished another student group site! The Business Association for Multicultural Students is a student group at the Carlson School of Business at the University of Minnesota.

You can check out the site or read my detailed blog post on how I went about creating the site, including neat PHP and JavaScript scripts I utilized.

Labels: , ,

» Read more...

Return to Navigation