May 16th, 2011 - By Bowo

Ok enough talking loser like me :D today we back on topic, now right now I want talk about images and radomizer, sometimes in some case we want rotate our image to make our blog look unique and fresh everytime people came to our site, but how we make image showing random each time they open it?

Well we can use javascript to manipulate it, but remember no all visitor browser activate their javascript, but don’t worry I have solution for it, we can use random function on php and create the most simple random images rotator.

Ok lets try, first we need the ingredient LOL, I mean we need prepare all thing

  1. image collection, and rename it to 1.jpg, 2.jpg, 3.jpg, 4.jpg, and remember all extension must same (all jpg, or all gif, or all png)
  2. folder, create folder for example imgrotator folder
  3. the code (I will show later)

Ok now put all images on imgrotator folder that you already create, and on the page you want to put the rotator images, paste this this code


<img src="/path/to/<?php echo rand(1,n);?>.jpg" alt="Random Image" />

See this part /path/to/ change to the path of image rotator folder, in this case folder imgrotator and for the rand(1,n); you just put total of image, in this case I put 4 image, so we change the “n” to 4 and became like this rand(1,4); and now your image rotator ready to upload… :) the finish code for this case is like this


<img src="/imgrotator/<?php echo rand(1,4);?>.jpg" alt="Random Image" />

Now try yours :) good luck..

nb: Ill try to give another way to use this as background rotator for your site header on next post, just wait :)

Comment list

  1. [...] I promise you about Simple Random Images PHP Script using for header background, like you can see some of site that using wordpress have interesting [...]

RSS feed for comments on this post. TrackBack URL

Leave a comment