Home arrow PHP arrow Page 4 - Caching Dynamic Twitter Signature Images with PHP

Finishing up the application - PHP

Welcome to fifth and final part of this series on creating a dynamic Twitter signature image in PHP. In the last segment, I showed you how to implement custom PHP exceptions as an error-handling mechanism in your signature image application. Today were going to wrap up our signature image application by adding a caching feature. This is a two-fold solution that both boosts performance and overcomes a pitfall in the Twitter API.

  1. Caching Dynamic Twitter Signature Images with PHP
  2. Implementing caching
  3. Understanding the logic
  4. Finishing up the application
By: Nilpo
Rating: starstarstarstarstar / 1
September 01, 2009

print this article



Now that caching has been added, the Twitter Signature Image application is now complete and fully operational.  The only thing left to do is upload it to a web server and start using it.  However, you may wish to have some idea of how many times your image is being displayed.  So let's take a minute to quickly add a database logging system.  (The remaining part of this article is completely optional).

First, you'll need to create a MySQL database in which to store your information.  You can simply import the SQL code below, using phpMyAdmin to create one.


USE `tlog`;




  `counter` INT(10) UNSIGNED NOT NULL

) engine=MyISAM default charset=latin1;

This will create a database with two fields.  One will store a Twitter user name, and the second will store a simple hit count.

Adding this to your application is extremely simple.  The signature image is only displayed if the SignatureImage class is correctly executed without error.  Because of this, we'll place our logging code in the SignatureImage class destructor (since the destructor is only executed if the class is correctly created.)

    public function __destruct()


        @mysql_connect('localhost', 'user', 'password');


        $query = "INSERT INTO tlog VALUES (LOWER('{$this->screen_name}'), 1)"

               . " ON DUPLICATE KEY UPDATE counter = counter + 1";


This code works very efficiently only executing a single query:

INSERT INTO tlog VALUES (LOWER('someusername'), 1)

  ON DUPLICATE KEY UPDATE counter = counter + 1";

This insert query inserts a user name into the database table with a value of 1 for the counter field.  The magic of its simplicity is in the ON DUPLICATE KEY UPDATE portion of the query.  If the user name already exists in the table, the counter field is incremented instead of a new row being inserted.

        if (file_exists($this->local_avatar)) {




Finally, we add a line to delete the local avatar image.  Since this image is downloaded every time the code is executed, the local image can be deleted to prevent the avatar directory from becoming unnecessarily bloated.

Thanks for tuning in for this article series.  I hope you had fun creating your own Twitter Signature Image application and that you learned a bit about PHP 5's exceptions as a means of error handling.  Take a moment to download my fully commented source code for this series below.  You'll be able to see how I've pieced together my files along with full phpDocumentor compatible comments.  Until next time, keep coding!


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: