The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Random Image Rotation

GoldenSeducerGoldenSeducer AAAAAUGH!!Registered User regular
edited March 2008 in Help / Advice Forum
Okay, I'm trying to get a signature that changes images every time you refresh or go to a different page with my posts on it. However, I can't seem to figure out how to get it right.

Apparently, there's some sort of script or file that I have to have in order to do this properly, but I'm not sure what it is. Google has yielded pretty crappy results as far as help goes, so please don't post the picture of Angry Gary Coleman shouting, "GOOGLE, MOTHERFUCKER! DO YOU USE IT?!" Please?

Could anyone offer assistance?

GoldenSeducer on

Posts

  • DaenrisDaenris Registered User regular
    edited February 2008
    Javascript would do it
    http://www.google.com/search?source=ig&hl=en&rlz=&q=javascript+random+image+display&btnG=Google+Search

    Or PHP
    http://www.google.com/search?hl=en&safe=off&q=php+random+image+display&btnG=Search

    Either of those within the first couple of links has complete scripts that show how to do something like this. So you throw a page with a script like that up somewhere and point your sig to it.

    Note: I'm not positive that this will work as I haven't tried putting a non-image link into the sig.

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited February 2008
    Every little bit helps, Daenris. Thanks a bunch! :D

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited February 2008
    It does work in fact.

    I just did a quick thing in php using the second link from that google search as a base and tested it in my signature, and it appears to work.
    <?php
    
    srand(time());
    
    $random = (rand()&#37;4);
    
    $img = 'img' . $random . '.jpg';
    
    $imageInfo = pathinfo($img);
    
    $contentType = 'Content-type: image/jpeg';
    
    header($contentType);
    readfile($img);
    
    ?>
    

    Then you just insert an image in your sig using IMG tags (it doesn't appear to work to just point the sig to the image).

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited February 2008
    Hawt! Thanks a whole bunch! :D

    GoldenSeducer on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited February 2008
    I seem to be hitting a snag somewhere... I guess I'm not sure where to put anything in the code, so I end up with a blank page.

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited February 2008
    well, what's your exact code look like?

    And do you have images in the folder with the code for it to display?

    Basically, my code up there generates a random number from 0-3 and will display the corresponding image from the same folder (img0.jpg, img1.jpg...)

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited February 2008
    I am totally inept at coding, I guess. I wasn't sure how to edit the code you posted so it would work for me, so I went to this site here and more or less came up with this:
    <?php
    
    <?php include "Signature.php"; ?>
    
    $total = "7";
    
    $file_type = ".gif";
    
    $image_folder = "/music/signature";
    
    $start = "1";
    
    $random = mt_rand($start, $total);
    
    $image_name = $random . $file_type;
    
    echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
    
    ?>
    

    GoldenSeducer on
  • TrentusTrentus Registered User regular
    edited March 2008
    (Edit: I just found out that you can't use HTML in sigs, so this is rather pointless now. Still, here if anyone wants it)

    I actually wrote a little java script last year for the webpage at work that does just this. And here it is.
    var Images = new Array()
    
    Images[0] = '/source_to_image/here.jpg'
    Images[1] = '/source_to_image/also_here.png'
    
    var j = 0
    var p = Images.length;
    
    var preBuffer = new Array()
    for (i = 0; i < p; i++){
       preBuffer[i] = new Image()
       preBuffer[i].src = Images[i]
    }
    
    var whichImage = Math.round(Math.random()*(p-1));
    function showImage(){
    document.write('<img src="'+Images[whichImage]+'">');
    }
    

    Just add more images to the array, and put in their sources. Note that the more pics you have in the array, the more effective this script will be (for example, the same pic being shown twice will be less likely). The page that displayed the pic the script chose looked something like this:
    <html>
    <head>
    <script src="/source_to/script.js" type="text/javascript"></script>
    </head>
    
    <body>
    <div align="center">
    <script type="text/javascript" language="JavaScript">
    <!--
    showImage();
    //-->
    </script>
    </div>
    </body>
    </html>
    

    Of course, there's no reason you couldn't lump the script and page into one entity, but I'll leave that up to you. You also don't have to position the thing centre, but I did for the page I made. Hope this proves simple enough to use.

    Trentus on
  • DaenrisDaenris Registered User regular
    edited March 2008
    I am totally inept at coding, I guess. I wasn't sure how to edit the code you posted so it would work for me, so I went to this site here and more or less came up with this:
    <?php
    
    <?php include "Signature.php"; ?>
    
    $total = "7";
    
    $file_type = ".gif";
    
    $image_folder = "/music/signature";
    
    $start = "1";
    
    $random = mt_rand($start, $total);
    
    $image_name = $random . $file_type;
    
    echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";
    
    ?>
    

    Well right away I see a potential problem... you have two <?php right away, and then a ?>. This is basically opening and closing the PHP code blocks. So the ?> may be closing off the entire thing in which case none of the subsequent code is happening. Take out the <?php and ?> around the include line because you don't need them since you're already in a PHP code block. I'm also not even sure why you have the include line there at all?

    Also this probably won't work if you can't put HTML in a sig. Because all this is doing is making an HTML page with a single image on it. The code I posted is actually sending itself as an image file (that's what the header stuff is doing).

    Here's an edited version of my code to do what you want.
    <?php
    
    $start = 1;
    $total = 7;
    
    $file_type = ".gif";
    $image_folder = "/music/signature";
    
    $random = mt_rand($start, $total);
    
    $image_name = $random . $file_type;
    $image_path = $image_folder . "/" . $image_name;
    $image_info = pathinfo($image_path);
    
    $contentType = 'Content-type: image/gif';
    header($contentType);
    readfile($image_path);
    
    ?>
    

    I haven't tested it but this should do what you want it. Then you put it into the Signature box using IMG tags pointed at the PHP script location.

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    Maybe the problem is that I don't know what kind of format to save the page as.

    There's a guy on another forum with a rotating signature and apparently it's saved a .cgi.

    So far, it doesn't matter what I do to the pages, I end up with a blank page or a page with just ;?> on it.

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited March 2008
    You need to save this as a .php file. And the webhost it's on needs to support PHP.

    Daenris on
  • SeñorAmorSeñorAmor !!! Registered User regular
    edited March 2008
    Also make sure that the forums here support non-image extensions (like .php) in tags.  The old ones did not, forcing people in your position to use .htaccess modifications to have a rotating sig.

    SeñorAmor on
  • DaenrisDaenris Registered User regular
    edited March 2008
    Also make sure that the forums here support non-image extensions (like .php) in tags.  The old ones did not, forcing people in your position to use .htaccess modifications to have a rotating sig.

    It seems to have worked for me. I tried it yesterday after posting some sample code.

    Daenris on
  • PheezerPheezer Registered User, ClubPA regular
    edited March 2008
    Also make sure that the forums here support non-image extensions (like .php) in tags.  The old ones did not, forcing people in your position to use .htaccess modifications to have a rotating sig.

    It's also significantly more badass to just use the htaccess tricks so that normal people can't figure out how the image changes all the time.

    Pheezer on
    IT'S GOT ME REACHING IN MY POCKET IT'S GOT ME FORKING OVER CASH
    CUZ THERE'S SOMETHING IN THE MIDDLE AND IT'S GIVING ME A RASH
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    Well... I think I might have screwed up somewhere... not sure.

    I used Daenris' modified code and made a .php file.

    Here's the link: http://music.fenproductions.thibros.com/signature/Signature.php

    Here's what the page source looks like:
    <br />
    <b>Warning</b>:  readfile(/music/signature/1.gif): failed to open stream: No such file or directory in <b>/srv/www/htdocs/web4/html/music/signature/Signature.php</b> on line <b>17</b><br />
    

    Where did I go wrong?

    GoldenSeducer on
  • PheezerPheezer Registered User, ClubPA regular
    edited March 2008
    Provide your source code. When I go all "save object" on that link it gives me the output only :P

    Pheezer on
    IT'S GOT ME REACHING IN MY POCKET IT'S GOT ME FORKING OVER CASH
    CUZ THERE'S SOMETHING IN THE MIDDLE AND IT'S GIVING ME A RASH
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    <?php
    
    $start = 1;
    $total = 7;
    
    $file_type = ".gif";
    $image_folder = "/music/signature";
    
    $random = mt_rand($start, $total);
    
    $image_name = $random . $file_type;
    $image_path = $image_folder . "/" . $image_name;
    $image_info = pathinfo($image_path);
    
    $contentType = 'Content-type: image/gif';
    header($contentType);
    readfile($image_path);
    
    ?>
    

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited March 2008
    You may want to just try putting the images in the same folder as the PHP script, at least to test to see if it's working. Then change the path to:
    $image_path = $image_name;

    I'm guessing it's having an issue about actually finding the picture, because it is at least trying to open the image.

    Daenris on
  • PheezerPheezer Registered User, ClubPA regular
    edited March 2008
    Times like this I wish I knew PHP. Is there a default working folder that you need to escape out of? Do you need to specify that /music/signature is mounted from your account's home folder rather than from the current location of the script itself?

    I would think that it's liiiiiikely got something to do with that. You could try Daenris's suggestion and just use the same folder for both. I'd suggest also using sensible folder naming conventions, like /images/signature instead of /music/signature but I'm a freak that way :P

    Pheezer on
    IT'S GOT ME REACHING IN MY POCKET IT'S GOT ME FORKING OVER CASH
    CUZ THERE'S SOMETHING IN THE MIDDLE AND IT'S GIVING ME A RASH
  • DaenrisDaenris Registered User regular
    edited March 2008
    Pheezer wrote: »
    Times like this I wish I knew PHP. Is there a default working folder that you need to escape out of? Do you need to specify that /music/signature is mounted from your account's home folder rather than from the current location of the script itself?

    I would think that it's liiiiiikely got something to do with that. You could try Daenris's suggestion and just use the same folder for both. I'd suggest also using sensible folder naming conventions, like /images/signature instead of /music/signature but I'm a freak that way :P

    Yeah... or alternatively to my suggestion earlier of using the same folder, you could try using an absolute path which may work. Like use "http://www.mywebsite.com/music/signature&quot; as the folder. Though I haven't tested that either, and the first thing I would do is use the same folder, just to make sure the script is otherwise working. After you know that it's working you can worry about fixing the path information.

    Edit: Okay, I actually did just test it and it is a path issue. Using a path relative to your site base like /music/signature doesn't work. If you use an absolute path (http://...... as above) it works. If you use something relative to where your script is (like ../music/signature) it will work. As an example I have my script in a folder called test on my root directory. I put the images into music/signature off my root directory. So in my script I used ../music/signature (so up one directory from test, then music/signature) and it works.

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    Okay, but what is the actual link starts out with music.blahblahblah.etc/signature? Could that be an issue? I've thrown everything into the same folder, which, in my web editor shows up as /html/music/signature.

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited March 2008
    you mean the link to your script? Depends on what the script is named. Is it in a folder named signature or is the script file named signature.php?

    If it's in a folder named signature, and named signature.php, then the link to put in an img tag in your signature would be like http://www.blah.com/music/signature/signature.php

    If you mean the link to put in the script to access the image, like I said if they're in the same folder as the PHP script you don't need to list a folder. Just use the image name alone as the location.

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    Daenris wrote: »
    you mean the link to your script? Depends on what the script is named. Is it in a folder named signature or is the script file named signature.php?

    If it's in a folder named signature, and named signature.php, then the link to put in an img tag in your signature would be like http://www.blah.com/music/signature/signature.php

    If you mean the link to put in the script to access the image, like I said if they're in the same folder as the PHP script you don't need to list a folder. Just use the image name alone as the location.

    What I'm trying to say is all of my links start look something like this: "music.fenproduction.thibros.com/blahblahblah" 'cause nothing shows up any other way. Would this cause a problem with paths or would it not make a difference?

    Edit: Also, my signature.php file is in my /music/signature folder.

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited March 2008
    I'm really not sure what you're asking. What links?

    Edit: Can you post the exact code you're using right now? If they're in the same folder you don't need to use anything other than the image name. So just "1.gif" or whatever. You can completely eliminate the $image_path. If they're in another folder you need to reference the path using either paths relative to the script (like "../blah/1.gif") or an absolute path (like http://blah.com/images/1.gif).

    Edit2: Also, so you know... those images you have and are trying to use are over the 50kb recommended size for forum sigs on here.

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    Oh... I know why they're bigger... I saved them as bitmaps in MSPaint and uploaded them to my provider like a stupid poopyhead instead of saving them as gifs in GIMP like a smart person.>_< Problem solved.

    Like, the link to my avatar is "http://music.fenproductions.thibros.com/avas/GoldenSeducer.png&quot; The URL starts with "music."

    Anyway, the actual script I'm using right now is this (and God, I hope I haven't gone in circles. I'm so inept at coding...):
    <?php
    
    $start = 1;
    $total = 7;
    
    $file_type = ".gif";
    $image_folder = /music/signature";
    
    $random = mt_rand($start, $total);
    
    $image_name = 1.gif;
    $image_name = 2.gif;
    $image_name = 3.gif;
    $image_name = 4.gif;
    $image_name = 5.gif;
    $image_name = 6.gif;
    $image_name = 7.gif;
    $image_info = pathinfo($image_path);
    
    $contentType = 'Content-type: image/gif';
    header($contentType);
    readfile($image_path);
    
    ?>
    

    I still get the "There's an error in line 7" message when I try to view the page. (http://music.fenproductions.thibros.com/signature/signature2.php)

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited March 2008
    What's with all the image_name lines? And image_path isn't defined anywhere, so that won't work.


    Here... put exactly this into signature.php and see if it works.
    <?php
    $start = 1;
    $total = 7;
    
    $file_type = ".gif";
    
    $random = mt_rand($start, $total);
    
    $image_name = $random . $file_type;
    
    $contentType = 'Content-type: image/gif';
    header($contentType);
    readfile($image_name);
    ?>
    

    Edit: Also, that parse error isn't the same problem. That was showing up because you close quotes around /music/signature" but didn't open them. Of course, as I mentioned the script wouldn't have worked as it was anyway. But let me know if the above works (it should).

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    Oh, HOT! It works! Thanks, Daenris! :D

    Oh, the image lines were me trying to experiment a little. It was a failed attempt.

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited March 2008
    No problem. It still seems to choke for me on like... one image I think. Not sure which one, but you might want to double check on the format that they're in or something.

    To put it in your signature, just use the img tags within the signature edit box:

    [html]
    signature.php
    [/html]

    Because it won't accept it if you just try to put it in the signature image link field below that.

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    Well, there's only 7 images right now. I plan on expanding the list later. Thanks so much!! :D

    GoldenSeducer on
  • DaenrisDaenris Registered User regular
    edited March 2008
    Heh... while I appreciate the props, your sig should only be 80px high total. Since your image is 500x80 (the max size for a sig) you can't also include text with it. If you want any lines of text there you'll need to make the sig image shorter.

    Daenris on
  • GoldenSeducerGoldenSeducer AAAAAUGH!! Registered User regular
    edited March 2008
    Bah. I post in SE++. No one cares that much there.

    GoldenSeducer on
Sign In or Register to comment.