PHP
  Home arrow PHP arrow Enhancing Dynamic Twitter Signature Images with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Enhancing Dynamic Twitter Signature Images with PHP
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2009-08-13


    Table of Contents:
  • Enhancing Dynamic Twitter Signature Images with PHP
  • Rendering an image
  • Embedding the pieces
  • Implementing the class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Enhancing Dynamic Twitter Signature Images with PHP
    ( Page 1 of 4 )

    In my last article we began putting together a solution that will allow us to display dynamic Twitter signature images in forum posts and emails. In this article we’ll continue where we left off by adding the functions that will harness the power of GD to create the actual image.

    Before we get too involved, let’s take a moment to look at the code that we’ve built so far.

    class SignatureImage

    {

        private $screen_name;

        private $profile_image;

        private $status_text;

        private $local_avatar;

       

        public function __construct($name, $bg_image, $adir)

        {

            $this->fetchUserInfo(strtolower($name));

            $this->fetchAvatar($this->profile_image, $adir);

        }

     

        private function fetchUserInfo($name)

        {

            $url = "http://twitter.com/statuses/user_timeline/{$name}.xml?count=1";

            $xml = $this->curlRequest($url);

            if ($xml === false) {

                // User feed unavailable.

            }

            $statuses = new SimpleXMLElement($xml);

            if (!$statuses || !$statuses->status) {

                // Invalid user channel.

            }

           foreach ($statuses->status as $status) {

                $this->status_text   = (string) $status->text;

                $this->profile_image = (string) $status->user->profile_image_url;

                $this->screen_name   = (string) $status->user->screen_name;

                break;

            }

        }

     

        private function curlRequest($url)

        {

            if (!extension_loaded('curl')) {

                // PHP extension CURL is not loaded.

            }

            $curl = curl_init($url);

            curl_setopt($curl, CURLOPT_HEADER, false);

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

            $result = curl_exec($curl);

            if (curl_errno($curl) !== 0 || curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) {

                $result === false;

            }

            curl_close($curl);

            return $result;

        }

     

        private function fetchAvatar($url, $adir)

        {

            $fname      = end(explode('/', $url));

            $adir       = preg_match('#^(.*?)/$#i', $url) ? $adir : "{$adir}/";

            $fname      = $adir . $fname;

            if ( !file_exists($fname) ) {

                $img = $this->curlRequest($url);

                $fp = fopen($fname, 'w');

                fwrite($fp, $img);

                fclose($fp);

            }

            $this->local_avatar = $fname;

        }

    }

    The code we have so far will accept a user name and retrieve the XML status feed using the Twitter API.  Then it parses that information, assigns some class-level variables, and creates a local copy of the user’s Twitter avatar.  Now we need to assemble these pieces into a single “signature” image.  To do that we’ll create a function called renderImage.  So let’s go ahead and complete the class’s constructor with a call to this function.

        public function __construct($name, $bg_image, $adir)

        {

            $this->fetchUserInfo(strtolower($name));

            $this->fetchAvatar($this->profile_image, $adir);

            $this->renderImage();

        }



     
     
    >>> More PHP Articles          >>> More By Nilpo
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek