how to create a simple automatic image rotator using jQuery

Rotate II by FatalBite on deviantART

I’ll explain a simple way to create an automatic image rotator using jQuery. First have a look at the demo page (opens in a new tab) to understand what we’re trying to accomplish. You can also download the tutorial’s source code.

In the example, the initial image will be displayed on the left of a paragraph containing some sample text:

<div id="test">
  <img id="myImage" src="test1.png" alt="image test" />
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>

First, we have to store the filenames of our images in an array. We also have to initialise a counter.

var images = new Array ('test1.png', 'test2.png', 'test3.png');
var index = 1;

The next step is to create the function which will rotate the images. We’ll call it rotateImage(). At the beginning, the currently displayed image fades out. Then, we load the next image from the images array (the counter we introduced before is being used here) and we fade it in. At the end of the function, we deal with the counter (either increment it, or reset it if all the imaged were displayed).

function rotateImage()
{
  $('#myImage').fadeOut('fast', function()
  {
    $(this).attr('src', images[index]);

    $(this).fadeIn('fast', function()
    {
      if (index == images.length-1)
      {
        index = 0;
      }
      else
      {
        index++;
      }
    });
  });
}

The last step is to call the rotateImage function repeatedly inside the $(document).ready function using setInterval. In the example, the function rotateImage() is being executed every 2.5 seconds (2500 milliseconds).

$(document).ready(function()
{
  setInterval (rotateImage, 2500);
});

The complete JavaScript code:

var images = new Array ('test1.png', 'test2.png', 'test3.png');
var index = 1;

function rotateImage()
{
  $('#myImage').fadeOut('fast', function()
  {
    $(this).attr('src', images[index]);

    $(this).fadeIn('fast', function()
    {
      if (index == images.length-1)
      {
        index = 0;
      }
      else
      {
        index++;
      }
    });
  });
} 

$(document).ready(function()
{
  setInterval (rotateImage, 2500);
});

HTML:

<div id="test">
  <img id="myImage" src="test1.png" alt="image test" />
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>

If you haven’t done it already, have a look at the demo page (opens in a new tab). You can also download the tutorial’s source code.

 

how to display your flickr photos using php

A Division of Me by toxictwo on deviantART
Photo by ~toxictwo on deviantART

In order to complete this tutorial you will need to apply for a flickr API key. You will also need to download a copy of phpFlickr API Kit. The use of an API Kit makes the use of flickr’s API methods easier. You can also view the demo page of the tutorial (opens in a new tab).

Let’s begin. First, we need to include the phpFlickr main file and then create a new phpFlickr object which is stored in the $phpFlickrObj variable. Make sure you enter the correct path (where you extracted the phpFlickr files) and also enter your flickr API key.

require_once("phpFlickr/phpFlickr.php");
$phpFlickrObj = new phpFlickr('Your_Flickr_API_Key');

The next step is to obtain your NSID by using your flickr username (in the example I’m using my flickr username), which is stored in the $user variable. Using this NSID, we get the url to the specific user’s photos and we store it in the $user_url variable. Finally, we get an array ($photos) containing all the relevant information (id, title, etc.) of the last four (if you want to change the number of photos, just change the number) photos appearing in the user’s photostream.

$user = $phpFlickrObj->people_findByUsername('stathisg');
$user_url = $phpFlickrObj->urls_getUserPhotos($user['id']);
$photos = $phpFlickrObj->people_getPublicPhotos($user['id'], NULL, NULL, 4);

In this example we use only the id and the title of each photo. If you want to view all the information contained in the array, you can use print_r($photos).

The final step to display the photos, is to convert the multidimensional array into an array of one dimension and use a loop to display everything. The following code, displays the photos. The photos can be displayed in six different sizes. To change the size, you need to alter the “square” value. The available sizes are: square, thumbnail, small, medium, large and original.

foreach ($photos['photos']['photo'] as $photo)
{
  echo '<a href="'.$user_url.$photo['id'].'" title="'.$photo['title'].' (on Flickr)" target="_blank">';
  echo '<img alt="'.$photo['title'].'" src="'.$phpFlickrObj->buildPhotoURL($photo, "square").'" />';
  echo '</a>';
}

The complete code is diplayed below. You can also view the demo page of the tutorial (opens in a new tab).

require_once("phpFlickr/phpFlickr.php");
$phpFlickrObj = new phpFlickr('46dea5f6abb12aed7a823adb7ecf5816');

$user = $phpFlickrObj->people_findByUsername('stathisg');
$user_url = $phpFlickrObj->urls_getUserPhotos($user['id']);
$photos = $phpFlickrObj->people_getPublicPhotos($user['id'], NULL, NULL, 4);

foreach ($photos['photos']['photo'] as $photo)
{
  echo '<a href="'.$user_url.$photo['id'].'" title="'.$photo['title'].' (on Flickr)" target="_blank">';
  echo '<img alt="'.$photo['title'].'" src="'.$phpFlickrObj->buildPhotoURL($photo, "square").'" />';
  echo '</a>';
}
 

how to convert twitter mentions and URLs to active links using regular expressions in php

Twitter style bird by dhuse on deviantART

Creation by ~dhuse on deviantART

If you are using twitter’s API to display your tweets in your Website you have probably faced the problem that your mentions (e.g. @burnmind) and URLs are displayed as plain text and not as active links. You can fix this in a very easy way, using regular expressions in php.

Assuming that you have stored your tweet in the variable named $tweet, you’ll only need the following two lines of code to convert the mentions and URLs to links:

$tweet = preg_replace("/((http)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $tweet );
$tweet = preg_replace("/[@]+([A-Za-z0-9-_]+)/", "<a href=\"http://twitter.com/\\1\" target=\"_blank\">\\0</a>", $tweet );

If you don’t know how to retrieve your tweet using the twitter’s API you can learn how by reading the following tutorials:

how to retrieve and display your latest tweet using PHP
how to store and retrieve your latest tweet from an XML file