Random Image Rotation (Fun with php)

page: 1
0

log in

join

posted on Aug, 21 2006 @ 09:03 PM
link   
There are lots of sites where you can have a random image hosted (i.e. randomimage.net) That's not what this thread is here to discuss. This thread is for those of you who want to know exactly how this might be done. Come on and step up. I know there are some nerds out there who might want to discuss this. If you're new to php this might be a bit over your head, but it could be a good place to learn how things are done as well.

Let's start off with a list of things you'll need to start with.


  • A web host that supports php
  • Knowledge of php (or a friend with nerd skills)
  • Knowledge of some HTML
  • A simple text editor (Word Pad, etc.)
  • A ftp client to upload the files to your server


If you have these items, then you can join me and say "We don't need no stinkin random image site. We can do it ourself" Really there's not a lot to this. If anyone is interested speak up. Or post your own code to get us started.

Basically what we're going to do is open a folder and find all the images that meet our criteria. Once we get a list of all the images, we'll pick one at random (sort of) and the return that image to the user. If you get lost in any of this refer to php.net for some detailed talk of the syntax.

[edit on 21-8-2006 by dbates]




posted on Aug, 21 2006 @ 09:13 PM
link   
Mmmmmmm......
this bone tast good !!!


rand()

Thats is the function you can use to select your random images.

Now all you need is the html with php embeded:
Heres something I think may work -


I was going to post code but ats's board sucks and it wont allow it.
Will make page to download.

www.photonmimic.com...

Have to rename it to .php extension.

This will pic a random image (out of 20) from its current directory.
the images must have .jpg as an extenstion and they
must be named image1.jpg to image20.jpg

any questions ?




[edit on 21-8-2006 by R3KR]

[edit on 21-8-2006 by R3KR]



posted on Aug, 21 2006 @ 10:30 PM
link   
Well I could have just posted a link to the source code, but I thought some members might want an explination of how this works. Let's take a simple script such as one I found at atomiclabs.com (while serching for image rotate). They'll give you enough information to get the script up and running, but how does it work exactly?

Let's break the code down into smaller bytes (Hee hee) and examine it. I'm going to skip over the simple configuration part since that's detailed well. The lines beginning with "//" are my comments on how the code works.



// --------------------- END CONFIGURATION -----------------------
// This variable will point to the image we're going to return. It is null or empty now
$img = null;
// If the folder path doesn't end with a slash then add the slash on the end
if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}


// I'm going to gloss over this next little section since it's beyond the scope of this discussion, but the code checks to see if a specific image was requested.


if (isset($_GET['img'])) {


The next bit of code opens the directory and loops through all the files in the directory. If the file extension matches one of the ones we specified in the configuration we add the file name to the list of possible files. Once we read all the files (if the file count is greater than 0) we pick a random file by dividing the time by the number of seconds since Jan 1970. Actually we use the remainder of the division. the '%' operator does the division and returns the remainder. We use this number to get an index in the array.

Once we know what image we want to use we add the folder path and the image name together. The period or dot is a way of appending an item onto another item in php. The full item, in our case the path the the image file, is stored in the $img variable.


else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);

if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}


Now we have the image, but how do we return this to the caller? How does the caller know that the php file is an image? We use the special php function call header to send a raw HTTP header to the caller. We tell them "Hey! I'm sending a jpg (or gif) file to you now. Treat everything else I send as an image. We then send the file information with the readfile function call.


if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} }



The little bit left over is extra code to create an image if there are no images available in the folder. Let's skip that for now since it clouds the issue a bit. That's all there is to it. Not that bad really and of course you don't have to understand any of this code to make it work. Just put the php rotator file in the directory with your images and you can add images, and take away images without touching the rotator code ever again.



posted on Aug, 22 2006 @ 02:13 AM
link   
You really dont need all that, for something this simple.
Plus would be faster and you could save the internet a few bits
if its smaller. Anyways, just a thought.



posted on Aug, 22 2006 @ 07:35 AM
link   
Well, you're version is somewhat simple, but I was thinking more along the lines of a random avatar (Like mine). You're code would work for displaying a random image in a web page, but you can't display a web page as an avatar.


<html>
<body>
<img src="image<?php echo rand(1,20) ?>.jpg">
</body>
</html>




p.s. The board doesn't suck, you just have to enter the code correctly


[edit on 22-8-2006 by dbates]



posted on Aug, 22 2006 @ 08:52 AM
link   
thanks this has been a great help



posted on Aug, 22 2006 @ 10:25 AM
link   
I use the same one posted above:
automaticlabs.com...

and for some reason ATS doesn’t allow me to link to a .php file in the avatar link. I had to add this line to my .htaccess file



AddType application/x-httpd-php .jpg .png


which allows you to save your PHP file as a .jpg, which ATS accepts.


[edit on 22/8/06 by ConspiracyNut23]



posted on Aug, 22 2006 @ 10:50 AM
link   
Exactly, but I'd be careful about adding that at the root of your web server. Once you do that it will send every jpg or png through the php pre-processor. That could be costly in server execution time. The best thing to do is to create a new .htaccess file just for the directory the rotator is in.



posted on Aug, 27 2006 @ 11:00 PM
link   
[edit on 2006/8/27 by GradyPhilpott]



posted on Aug, 27 2006 @ 11:50 PM
link   
Here's a Perl version I created a while ago for my own use:



#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

opendir AVDIR, "../img/" or die "Can't open rotateavatars directory: $!";
@avatars = grep !/^\.\.?$/, readdir AVDIR;

$file = $avatars[rand $#avatars + 1];

open AV, "../img/$file" or die "Can't open $file $!";

if ($file =~ /\.jpg$/) {
$type = "jpeg";
} else {
$type = "gif";
}


$filesize = read AV, $graphic, -s AV;
print "Content-type: image/$type\r\n";
print "Content-length: " . $filesize . "\r\n\r\n";
print $graphic;


No guarantees on it, but I haven't had problems when I used it...of course you need to edit the open statement to point to the directory where your images are.

[edit on 8/27/2006 by djohnsto77]



posted on Oct, 21 2006 @ 12:19 PM
link   
A script I had help on a long long time ago which helped me show stats on a radio station I ran for about 8 months.








This is the area where they helped me get this up and running. I am guessing you can gut this code and use it how you feel fit.

I dont care what you do with this code.. Since my internet name changed over the last few yrs, you can claim this code as your own for all I care...

Ahhh Open Source, the greatest thing next to sex...
thats a joke above.

anyway site is here where you can learn almost step by step on how to set this up.
forum.powweb.com...





new topics
top topics
 
0

log in

join