topical media & game development

talk show tell print

mashup-flickr-14-Step-3-lib-FlickrGallery.php / php



  <?php
  
  class FlickrGallery extends FlickrAuthenticator
  {
    var nsid = '50317659@N00';
  
    var connection = 0;  
  
    var useDB = 1;
  
    var dbhost;
    var dbname;
    var dbuser;
    var dbpasswd;
  
    function FlickrGallery()
    {
      global flickrApiKey;
      global flickrApiSecret;
  
      this->FlickrAuthenticator(flickrApiKey, flickrApiSecret);
  
      global dbhost;
      global dbname;
      global dbuser;
      global dbpasswd; 
  
      this->dbhost = dbhost;
      this->dbname = dbname;
      this->dbuser = dbuser;
      this->dbpasswd = dbpasswd;
    }
  
    function findRecentPhotos(n = 20)
    {
      recentPhotos = array();
      if (this->useDB)
      {
        sql = "SELECT * FROM photo ORDER BY photo_date_upload DESC LIMIT n";
        res = mysql_query(sql, this->getConnection())
          or die("Invalid query: " . sql);
  
       while(row = mysql_fetch_assoc(res))
       {
         photo = this->buildPhotoFromRow(row);
         array_push(recentPhotos, photo);
       }
      }
      else
      {
        args = array(
          'user_id' => this->nsid,
          'sort' => 'date-posted-desc',
          'page' => 1, 
          'per_page' => n,
          'extras' => 'owner_name'
          );
  
        p = this->flickr->photos_search(args);
        if (this->flickr->getErrorCode())
        {
          echo ("Error fetching photos: " . this->flickr->getErrorMsg());
        }
  
        recentPhotos = p['photo'];
      }
  
      return recentPhotos;
    }
    
    function getPhotoInfo(id)
    {
      p = this->flickr->photos_getInfo(id);
      if (this->flickr->getErrorCode())
      {
        echo ("Error getting photo info: " . this->flickr->getErrorMsg());
      }
  
      return p;
    }
  
    function showSmartSetThumbnail(linkPage, title = "Set", n = 20, tags = "", 
      tagMode = "all", sort = "date-posted-desc")
    {
      s = "";
      url = linkPage . '?title=' . urlencode(title) . '&tags=' . urlencode(tags) 
        . "&n=n&tagMode=tagMode&sort=sort";
      // Get image to display
      photos = this->getSmartSet(1, tags, tagMode, sort);
  
      if (is_array(photos) && count(photos) > 0)
      {
        photo = photos[0];
        img = 'http://static.flickr.com/' . photo['server'] . '/' . photo['id'] 
          . '_' . photo['secret'] . '_s.jpg';
        s .= "<a href=\"url\"><img src=\"img\" /></a>";
        s .= "<p class=\"smart-set-caption\">" . title . "</p>";
      }
      return s;
    }
  
    function getSmartSet(n = 20, tags = "", tagMode = "all", 
      sort = "date-posted-desc")
    {
      ret = array();
  
      args = array(
        'user_id' => this->nsid,
        'sort' => sort,
        'page' => 1,
        'per_page' => n,
        'extras' => 'owner_name'
      );
  
      if (!empty(tags))
      {
        args['tags'] = tags;
        args['tag_mode'] = tagMode;
      }
  
      p = this->flickr->photos_search(args);
      if (this->flickr->getErrorCode())
      {
        echo ("Error fetching photos: " . this->flickr->getErrorMsg());
      }
  
      if (is_array(p['photo']) && count(p['photo']) > 0)
      {
        ret = p['photo'];
      }
  
      return ret;
    }
  
    function setMeta(id, title, description)
    {
      this->flickr->photos_setMeta(id, title, description);
  
      if (this->flickr->getErrorCode())
      {
        echo ("Error setting metadata: " . this->flickr->getErrorMsg());
      }
    }
  
    function checkAuthenticatedUser()
    {
      return (this->nsid == this->auth['user']['nsid']);
    }
    
    function setTags(id, tags)
    {
      this->flickr->photos_setTags(id, tags);
  
      if (this->flickr->getErrorCode())
      {
        echo ("Error setting tags: " . this->flickr->getErrorMsg());
      }
    }
  
    function uploadPhoto(file, title = null, description = null, tags = null, 
      isPublic = null, isFriend = null, isFamily = null)
    {
      id = this->flickr->sync_upload(file, title, description, tags, isPublic,
        isFriend, isFamily);
  
      if (this->flickr->getErrorCode())
      {
        echo ("Error uploading photo: " . this->flickr->getErrorMsg());
      }
  
      return id;
    }
  
    function replacePhoto(file, photoId)
    {
      id = this->flickr->replace(file, photoId);
  
      if (this->flickr->getErrorCode())
      {
        echo ("Error replacing photo: " . this->flickr->getErrorMsg());
      }
  
      return id;
    }
  
    function getWithGeoData(bbox = NULL)
    {
      if (bbox == NULL)
      {
        bbox = '-180, -90, 180, 90';
      }
  
      args = array(
        'user_id' => this->nsid,
        'sort' => 'interestingness-desc',
        'bbox' => bbox,
        'extras' => 'geo',
        'per_page' => 500,
        );
  
      p = this->flickr->photos_search(args);
      if (this->flickr->getErrorCode())
      {
        echo ("Error fetching photos with GeoData: " . this->flickr->getErrorMsg());
      }
  
      return p['photo'];
    }
    
    function getConnection()
    {
      if (empty(this->connection))
      {
        this->connection = mysql_connect(this->dbhost, 
          this->dbuser, this->dbpasswd);
  
        if (!this->connection)
        {
          die ("Cannot connect to database server: " . this->dbhost);
        }
  
        ret = mysql_select_db(this->dbname, this->connection);
  
        if (!ret)
        {
          die ("Cannot access database " . this->dbname . " on " . this->dbhostname);
        }
      }
  
      return this->connection;
    }
  
    function flushDBPhotos()
    {
      sql = "DELETE FROM photo";
      mysql_query(sql, this->getConnection()) or die("Invalid query: " . sql); 
    }
  
    function populateDBPhotos()
    {
      this->flushDBPhotos();
      set_time_limit(240);
  
      q = "INSERT INTO photo" 
        . " (photo_id, photo_secret, photo_server, photo_owner, photo_owner_name,"
        . " photo_is_public, photo_is_friend, photo_is_family,"
        . " photo_date_taken, photo_date_taken_granularity, photo_date_upload,"
        . " photo_title, photo_latitude, photo_longitude, photo_accuracy)"
        . " VALUES" 
        . " ('\%s', '\%s', \%d, '\%s', '\%s', \%d, \%d, \%d, '\%s', \%d, \%d, '\%s',"
        . " \%f, \%f, \%d)";
  
      keepGoing = 1;
      page = 1;
      while (keepGoing)
      {
        args = array(
          'user_id' => this->nsid,
          'sort' => 'date-posted-desc',
          'page' => page, 
          'per_page' => 500,
          'extras' => 'date_upload, date_taken, owner_name, geo'
        );
  
        p = this->flickr->photos_search(args);
        if (this->flickr->getErrorCode())
        {
          echo ("Error fetching photos: " . this->flickr->getErrorMsg());
        }
  
        if (count(p['photo']) == 0)
        {
          // no more photos
          break;
        }
  
        foreach(p['photo'] as photo)
        {
          sql = sprintf(q, photo['id'], photo['secret'], photo['server'], 
            photo['owner'], mysql_escape_string(photo['ownername']), 
            photo['ispublic'], photo['isfriend'], photo['isfamily'], 
            photo['datetaken'], photo['datetakengranularity'], 
            photo['dateupload'],
            mysql_escape_string(photo['title']), photo['latitude'], 
            photo['longitude'], photo['accuracy']);
  
          mysql_query(sql, this->getConnection()) 
            or die("Invalid query: " . sql);
        }
        page++;
      }
    }
  
    function buildPhotoFromRow(row)
    {
      photo = array();
  
      photo['id'] = row['photo_id'];
      photo['secret'] = row['photo_secret'];
      photo['server'] = row['photo_server'];
      photo['owner'] = row['photo_owner'];
      photo['ownername'] = row['photo_owner_name'];
      photo['title'] = row['photo_title'];
      photo['ispublic'] = row['photo_is_public'];
      photo['isfriend'] = row['photo_is_friend'];
      photo['isfamily'] = row['photo_is_family'];
      photo['dateupload'] = row['photo_date_upload'];
      photo['datetaken'] = row['photo_date_taken'];
      photo['datetakengranularity'] = row['photo_date_taken_granularity'];
      photo['latitude'] = row['photo_latitude'];
      photo['longitude'] = row['photo_longitude'];
      photo['accuracy'] = row['photo_accuracy'];
  
      return photo;
    }
  
  }
  
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.