Dss.Lexius Posted October 10, 2010 Share Posted October 10, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/215543-rotate-images/ Share on other sites More sharing options...
litebearer Posted October 10, 2010 Share Posted October 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/215543-rotate-images/#findComment-1120782 Share on other sites More sharing options...
dragon_sa Posted October 10, 2010 Share Posted October 10, 2010 One way is to put the attached script in the directory of the images and then include it on the page you want the images on, there are plenty of configs you can play around with. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/215543-rotate-images/#findComment-1120785 Share on other sites More sharing options...
PaulRyan Posted October 10, 2010 Share Posted October 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/215543-rotate-images/#findComment-1120796 Share on other sites More sharing options...
litebearer Posted October 10, 2010 Share Posted October 10, 2010 ... 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. Quote Link to comment https://forums.phpfreaks.com/topic/215543-rotate-images/#findComment-1120808 Share on other sites More sharing options...
Dss.Lexius Posted October 10, 2010 Author Share Posted October 10, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/215543-rotate-images/#findComment-1120826 Share on other sites More sharing options...
Alex Posted October 10, 2010 Share Posted October 10, 2010 ... 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. Quote Link to comment https://forums.phpfreaks.com/topic/215543-rotate-images/#findComment-1120836 Share on other sites More sharing options...
Dss.Lexius Posted October 11, 2010 Author Share Posted October 11, 2010 ... 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, [email protected] 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. Quote Link to comment https://forums.phpfreaks.com/topic/215543-rotate-images/#findComment-1121029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.