Jump to content

Rotate images


Dss.Lexius

Recommended Posts

Hi,

 

There are 10 images in a folder called "Images".

All images are named like 1.jpg, 2.jpg, 3.gif, 4.png, and so on...

 

I am using these images on my website with this link: MyDomain.com/Images/1.jpg

Now what i want to do is change the image every minute.

 

from 1... to ...10 all images one by one, every minute...

 

Link should remain same, but it should give different image every time.

and if someone copies my link of image, it still gets changed every minute.

 

i hope to get some help, better with some example code...

 

Thanks!

 

Link to comment
Share on other sites

To have that effect you would need to have an image called 'current.jpg'.  That would obviously be the current image.

 

Then you need a cron job that will, each minute, take one of your 10 images and 'overwrite' the 'current.jpg'. 

 

This will make it so (1) new image every minute and (2) links to that image will display the current image.

 

Also IF you want the image to change while the site is being viewed, you would probably need ajax/javascrpt with some timer element

Link to comment
Share on other sites

If there are just 10 images, you could do it time based.

By getting using date('i'); you get the currents minutes, and you get the last digit and use that as a reference to an imagem like the following:

<?php
  // Get date minutes
  $imageNumber = date('i');

  // If the length is 2, we just use the last digit
  // If the length is 1, we just use that digit
  if(!$imageNumber[1]) {
    $image = $imageNumber[0];
  } else {
    $image = $imageNumber[1];
  }

  // Set the header
  header('Content-type: image/png');

  // Echo the file into the browser.
  echo file_get_contents('images/'.$image.'.png');
?>

 

You can modify the script by changing the file type if you need to, or use if else statements if different file types exist.

I will display 1.png when ever the time in minutes end with 1

I will display 2.png when ever the time in minutes end with 2

I will display 3.png when ever the time in minutes end with 3

ect, ect...

 

This is the way to do it with PHP, and is very simple.

Alternatively if you want to to dynamically change the image Litebearer is correct with using Ajax or Javascript.

 

Regards, Paul.

Link to comment
Share on other sites

If there are just 10 images, you could do it time based.

By getting using date('i'); you get the currents minutes, and you get the last digit and use that as a reference to an imagem like the following:

<?php
  // Get date minutes
  $imageNumber = date('i');

  // If the length is 2, we just use the last digit
  // If the length is 1, we just use that digit
  if(!$imageNumber[1]) {
    $image = $imageNumber[0];
  } else {
    $image = $imageNumber[1];
  }

  // Set the header
  header('Content-type: image/png');

  // Echo the file into the browser.
  echo file_get_contents('images/'.$image.'.png');
?>

 

You can modify the script by changing the file type if you need to, or use if else statements if different file types exist.

I will display 1.png when ever the time in minutes end with 1

I will display 2.png when ever the time in minutes end with 2

I will display 3.png when ever the time in minutes end with 3

ect, ect...

 

This is the way to do it with PHP, and is very simple.

Alternatively if you want to to dynamically change the image Litebearer is correct with using Ajax or Javascript.

 

Regards, Paul.

Thanks Paul, because i am new to PHP, i want to know how do i use it.

should i copy this script and save as current.png (i mean PHP code but PNG ext)

 

Link to comment
Share on other sites

... and if someone copies my link of image, it still gets changed every minute.

 

By changing the 'image content' the above requirement will be met.

Well, you could still take a similar approach. I think creating a cron job is pretty unnecessary for a task like this. You could create a file like this:

 

someimage.php -

<?php
header('Content-type: image/png');
readfile('images/' . ((date('i') % 10) + 1) . '.png');
?>

 

Then in your html for the image use:

 

<img src="someimage.php" alt="" />

 

And if anyone copies the url, it will change also.

Link to comment
Share on other sites

 

 

... and if someone copies my link of image, it still gets changed every minute.

 

 

By changing the 'image content' the above requirement will be met.

 

Well, you could still take a similar approach. I think creating a cron job is pretty unnecessary for a task like this. You could create a file like this:

 

someimage.php -

 

<?phpheader('Content-type: image/png');readfile('images/' . ((date('i') % 10) + 1) . '.png');?>

 

 

Then in your html for the image use:

 

 

<img src="someimage.php" alt="" />

 

 

And if anyone copies the url, it will change also.

 

i am getting the following error when trying to run that script

 

Internal Server Error

 

The server encountered an internal error or misconfiguration and was unable to complete your request.

 

Please contact the server administrator, webmaster@MyDomainNameHere.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

 

More information about this error may be available in the server error log.

 

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.