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...

Wednesday, March 19, 2008

Customize ASP.NET Wizard SideBar Template [Snippet]

If you have used the ASP.NET Wizard Control and have tried to use the default Sidebar template, you know that it does not allow you to get rid of the LinkButton controls. Some of us, however, don't want users to skip all around our wizard, so we need a way to show the users where they are but prevent them from screwing it all up.

Here's a quick hack that will do just that. Basically, the theory is that the DataList control in the Sidebar template has to be there along with a LinkButton. But you don't have to show it to the user, do you? All we do is attach a PreRender event handler on the DataList to grab all the wizard steps and duplicate them as labels in a separate label control.

Page with Wizard Control
<SideBarTemplate> <asp:Label ID="SideBarInfo" OnPreRender="InitSideBarLbl" runat="server" /> <asp:DataList ID="SideBarList" OnPreRender="LoadSideBar" RepeatLayout="Flow" Style="display:none;" runat="server"> <ItemTemplate> <asp:LinkButton ID="SideBarButton" Visible="false" runat="server"></asp:LinkButton> </ItemTemplate> <SelectedItemStyle Font-Bold="True" CssClass="Wizard-Sidebar-Selected" /> </asp:DataList> </SideBarTemplate>
Then, in your Code-Behind:
Label SideBarInfo; // Hide all LinkButton controls and duplicate into labels! protected void LoadSideBar(object s, EventArgs e) { DataList me = (DataList)s; // Reset label (postbacks) SideBarInfo.Text = ""; // Loop through datalistitems foreach (DataListItem item in me.Items) { // Each list item LinkButton btn = (LinkButton)item.FindControl("SideBarButton"); if (btn != null) { Label newLabel = new Label(); newLabel.Text = btn.Text; // Current step? if (me.SelectedItem == item) { newLabel.CssClass = me.SelectedItemStyle.CssClass; } // Styled divs SideBarInfo.Text += "<div class='" + newLabel.CssClass + "'>" + newLabel.Text + "</div>"; } } } protected void InitSideBarLbl(object s, EventArgs e) { SideBarInfo = (Label)s; }

See? Easy as pie. Feel free to improve it! And if you do, let me know how in the comments.

Labels: , , ,

» Read more...

Return to Navigation