Create a rotating image with dynamic links

Adam09:40 pm - 23/10/2009 Send a PM Report Post Edit Post
Share |
Administrator

Posts: 355
XP: 318

The final product

http://ludatha.com/signature/test.php

Take a look at the above example, it loads an image and points to one url, however, the image is random and where it takes you is dependent on the image.

Click it a few times, each time reloading the page to get a new image, and you will see that it takes you where the image is telling you to go! But it links to the same url? How does it do this? Let me show you. Remember that if you want to use it as a rotating signature, some forums (including pixel2life)  do NOT allow it.

How it works

The image is loaded from a php file, which randomly selects an image and stores its name in a session, so when the user clicks the image, the php script reads the variable from the session and sends the user to the correct place.

The Code

The code is quite simple, first create a file called index.php(or whatever you want to call it) and input this code. You need to create a directory called images with each image labelled 1.png, 2.png etc.

Edit the variables in the top to suit your needs. Also you need to edit the switch statements to match the urls.


/*

 

Dynamic rotating image with url

Use this script any way you want to

 

If you do use this script, please link to http://ludatha.com on your site :)

 

*/

//**********

// EDIT HERE

//**********

$num_images = 3;

$image_type = ‘png’;

 

session_start();

 

// read session information

if(isset($_GET[‘click’])){

         ;        // redirects

         ;        if(isset($_SESSION[‘image’])){

         ;       &nbs p;       &nb sp;        switch ($_SESSION[‘image’]) {

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;        //**********

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;        // EDIT HERE

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;        //**********

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;        case 1:

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;                 ;       &nbs p; header(‘Location: http://ludatha.com/forum/’);

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;                 ;       &nbs p; break;

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;        case 2:

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;                 ;       &nbs p; header(‘Location: http://ludatha.com/arcade.html’);

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;                 ;       &nbs p; break;

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;        case 3:

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;                 ;       &nbs p; header(‘Location: http://ludatha.com/portal/’);

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;                 ;       &nbs p; break;

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;                 ;       &nbs p; // The default statement is the one that gets executed if something went wrong, so it will lways redirect.

         ;       &nbs p;       &nb sp;       &n bsp;       & nbsp;         default:

                ;       &nbs p;       &nb sp;       &n bsp;       & nbsp; header(‘Location: http://ludatha.com/’);

         ;       &nbs p;       &nb sp;        }

         ;        }else{

         ;       &nbs p;       &nb sp;        header(‘Location: http://ludatha.com/’);         ;  

         ;        }

         ;        exit();

}

 

// pick an image

$image = rand(1,$num_images);

 

 

// Set session information

$_SESSION[‘image’] = $image;

 

header(‘Content-Type: image/’.$image_type);

readfile(‘images/’.$image.’.’.$image_type);

?>



Example Usage

<a href="index.php?click"><img src="index.php" alt=" " /></a>


If you can’t get it to work, download the example, or reply to this thread.

Example: http://ludatha.com/signature/signature.zip

coolman807:51 pm - 28/10/2009 Send a PM Report Post Edit Post
User

Posts: 61
XP: 58


Awesome.

Lizard02:11 pm - 01/11/2009 Send a PM Report Post Edit Post
User

Posts: 3
XP: 3

You could even do this on a much larger scale, with hundreds of different phrases, using a basic template image then dynamically writing text to the image, using various PHP funtions.


Let’s say you have a folder called image, inside this folder is a “template.png”, which is the background image you’d write text onto. In the folder is also a font file, such as “impact.ttf”, which PHP will call on for the shapes of the letters, and finally, an “image.php” which will be the final image, containing the code;


//Dynamic text writing to an image in PHP


//First, create $my_img from the template.png and 

//make sure that it’s worked before continuing

if($my_img = imagecreatefrompng(“template.png”)){

//The text we’re going to add, which could be loaded externally
$text = “Hello there!”;

//Specifying the path of the font file
$font_file = ‘./impact.ttf’;

//Setting the colour that the text will be with RGB values
$white = imagecolorallocate($my_img, 255, 255, 255);

//Setting the size, in pixels, of the font
$size = 45;

//x and y position of the text
$text_x = 10;
$text_y = 10;


//putting the text into $my_img with all the variables

//the 0 in here is Angle, leaving it horizontal
imagefttext($my_img, $size, 0, $text_x, $text_y, $white, $font_file, $text);

//Finally, set the header so that the PHP is read by the browser as an image
header( “Content-type: image/png” );


//Then use imagepng to output the image, which is currently just stored in variables and memory
imagepng( $my_img );
}
?>



The downside to this is that it’s a bit more complicated, more things can go wrong, it uses more of the server’s resources because it’s creating the image every time the file is loaded. Other than that, this is a great way to make a dynamic image, bearing in mind that you can load template.png and the text to be displayed from external places.

Adam03:02 pm - 01/11/2009 Send a PM Report Post Edit Post
Administrator

Posts: 355
XP: 318

That is a much more complicated way, and it does use more system resources and it does have it’s limitations :P


But thanks for the input…

Page generated in 0.1147, using 3.31MB