.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   a different sig each time (http://forums.bots-united.com/showthread.php?t=146)

Pierre-Marie Baty 02-01-2004 02:00

a different sig each time
 
While we're at it here's something that should be fun to do, however I'm quite clueless on how to do it.

I figured out at some point that a certain user on a certain forum had turned its signature in a way such as when you hit the "reload" button on your browser, the image displayed in his signature at the bottom of each of his posts was changing each time.

Do you know how he might have done it ?
All I can say is that I believe there's some PHP involved.

raiden 02-01-2004 16:10

Re: a different sig each time
 
PHP Code:

$randomnumber gmp_random(10); //generates a number between 0 and 10

echo "<img src=\"http://yourwebspace.de/".$randomnumber.".jpg\">"

I think that should work if you got webspace with 1.jpg till 10.jpg :>

Pierre-Marie Baty 02-01-2004 18:39

Re: a different sig each time
 
the problem is that forum softwares don't allow PHP in the users' signatures :)
I believe the guy used something like this:
<IMG src="http://hisdomain.com/image.jpg">
but the JPG image was in fact a PHP file, that would open a file at random in the directory and directly send its content over the network. I don't konw if this is feasible, though.

BAStumm 02-01-2004 23:15

Re: a different sig each time
 
php can generate random images so put the phpcode on YOUR php enabled server then put the image tags here pointing to a image.php file stead of image.jpg

Might be better to generate the image on the fly rather then the example above, see the php image() function.

BAStumm 03-01-2004 00:06

Re: a different sig each time
 
lets see if this works... Heres a graph bar I use on my site.


http://bs-linux.com/stats/loadgraph....th=25&avg=0.25
http://bs-linux.com/stats/loadgraph....th=58&avg=0.58

http://bs-linux.com/stats/loadgraph.php
If this works note that both images are the same php file with different ?width settings...

ok so a .php file is ok just can't have any ?blah=blah stuff at the end... If you click the links you will see that the image changes based on my width setting. You can use the image() function to make your sig random looks like. Just do a image from jpg in your php and then randomly use one of the 2 or more images you wish from your site.

http://php.net/image for more info on this...

BAStumm 03-01-2004 00:21

Re: a different sig each time
 
btw the other suggestion prolly wont work. Reason is it outputs html whereas using the image function outputs actual image header info for png, jpg, gif or whatever type you tell the php code to dynamically generate.

take this sample code from the php.net link I gave before...

Code:

<?php
  header("Content-type: image/png");
  $string = $_GET['text'];
  $im        = imagecreatefrompng("images/button1.png");
  $orange = imagecolorallocate($im, 220, 210, 60);
  $px        = (imagesx($im) - 7.5 * strlen($string)) / 2;
  imagestring($im, 3, $px, 9, $string, $orange);
  imagepng($im);
  imagedestroy($im);
?>

Now modify that with some random number generation to do a

if($num == 1) {$im = imagecreatefrompng("pic1.png");
if)$num == 2) ($im = imagecreatefrompng("pic2.png");
etc...

Pierre-Marie Baty 03-01-2004 00:25

Re: a different sig each time
 
hey thanks a lot! how could I forget the trusty php.net resource center :)

that's exactly what I need :)

BAStumm 03-01-2004 00:31

Re: a different sig each time
 
Code:

<?php
//image headers
header("Content-type: image/png");
// random number 1-9
$num = RAND(1,9);
// generate image name
$myimage = "pic".$num.".png";
//create image
$im = imagecreatefrompng($myimage);
// output the image binary       
imagepng($im);
//destroy image
imagedestroy($im);
?>

http://spokaneteamfortress.com/randimg.php

Like that perhaps. :)

rob 03-01-2004 00:53

Re: a different sig each time
 
nice work.

Pierre-Marie Baty 03-01-2004 01:01

Re: a different sig each time
 
I was to post about the same thing :D
PHP Code:

<?php
$filename 
sprintf ("sig%d.png"rand (09));
 
$image imagecreatefrompng ($filename);
 
header ("Content-type: image/png");
imagepng ($image);
imagedestroy ($image);
 
?>

creditz to you Brian, of course :)

BAStumm 03-01-2004 01:20

Re: a different sig each time
 
glad i could return a favor, you've helped me many a time PM.

TruB 06-01-2004 12:11

Re: a different sig each time
 
does it have to be a png file.. can i change that. im not very good at php.. i getting help from someone who dont know much but know more then me..

Pierre-Marie Baty 06-01-2004 13:07

Re: a different sig each time
 
No, you can use one of the many image format PHP supports (provided PHP has been installed on your server with the right image lib extensions).

Have a look at www.php.net and search for the image capabilities of PHP. Everything is explained.

TruB 06-01-2004 19:54

Re: a different sig each time
 
k. thanks..

Fiber_Optic 10-01-2004 23:57

Re: a different sig each time
 
Quote:

Originally Posted by Pierre-Marie Baty
Have a look at www.php.net and search for the image capabilities of PHP. Everything is explained.

The documentation of php is the best I've ever seen (maybe because there is an official one... when a C++ official doc? msdn? pff... lol ) just after the java doc which is so huge... but so useful ! :D

FrostyCoolSlug 10-04-2004 05:15

Re: a different sig each time
 
Another (possibly slightly hackier, but still does the same) way to do this only with out the GD libs, would be something like this:

PHP Code:

<?php
$filename 
sprintf ("sig%d.png"rand (09));
readfile($filename);
?>

Rather than creating a new image, this would just 'dump' the contents of the image, keeping the image headers in tact.

The only main differences between this and the GD version are (like i said), this doesnt require GD, and it can be generated faster, thru not having to create a seperate image.

Pierre-Marie Baty 10-04-2004 14:58

Re: a different sig each time
 
testing...
wahey it works!!

Thanks Frosty! That's true, I prefer this method ! :)

FrostyCoolSlug 10-04-2004 16:33

Re: a different sig each time
 
glad i could help :)

FrostyCoolSlug 12-05-2004 07:00

Re: a different sig each time
 
i've got a slightly more 'interesting' method here, which will open a dir, read all the images in it, (doesnt matter about filenames, so long as they have valid extentions), then pick one of them at random, and display it if you want it. Would be easier for 'organising' sigs :p

TruB 12-05-2004 11:30

Re: a different sig each time
 
do you mind giving out the source?.. or something that is easy to change so it fits or another.. i would be pretty happy if you did..

its not very importent to me.. just a cool thing to have.. must add im complete noob when it comes to php coding..

Pierre-Marie Baty 12-05-2004 15:03

Re: a different sig each time
 
oh yeah, I'm interested as well :)

that's teh way I'm learning PHP: by reading at other ppl's code...

[NvT]_KaszpiR_ 12-05-2004 16:45

Re: a different sig each time
 
once i was using crontab :D
then i used php genrated image
then the .php links were forbidden because it could return dangerouns output so i changed it to writ the .jpg file a diff file (well more like crontab script in fact)

now i use static jpg cause i got used to it - easier to find my own posts

FrostyCoolSlug 12-05-2004 17:48

Re: a different sig each time
 
ok, here we go then :)
This script is much longer than the others, but does what its told :)

PHP Code:

<?php

$filepath 
"images/";
$extentions = Array("PNG""JPG""GIF");

// Open the Directory and clear the files array (JIC)
$handle = @opendir($filepath) or die("");
$files = Array();

// Loop through all the files in the directory
while (false !== ($file readdir($handle))) {
    
// Make sure they are not directories
    
if (!is_dir($file)) {
        foreach (
$extentions as $m00) {
            
// Case insencitive comparison of file extention and
            // Allowed extention
            
if (strcasecmp(getext($file), $m00)) {
                
// Positive Match, add the file to the array
                
$files[] = $file;
                
// Jump out the foreach and tell the while to continue
                
continue 2;
            } 
        } 
    } 

// Make sure there are some files..
if (!count($files)) {
    
// No Valid files were found, kill self.
    
return;
} else {
    
// Choose a random image (the keys are numbers, so can rand them)
    // Use count() -1 because count() starts at 1 and not 0 
    
$img rand(0count($files) - 1);

// Output the file.
readfile($filepath $files[$img]); 

// Function used for fetching extentions..
function getext($file)
{
    
$file explode("."$file);
    return 
$file[count($file) - 1];


?>

I've commented it well, so you know what i'm doing. I've tested it, and know it works :D

If you have any questions, gimme a shout :)

Pierre-Marie Baty 12-05-2004 19:14

Re: a different sig each time
 
hehe w00t thanks

I've made it a lil bit shorter :)

don't forget to close the directory handle in your code btw...
Code:

<?php
 
$handle = opendir (getcwd ()) or die ();
$files = Array ();
$file_count = 0;
 
// loop through all the files in the directory
while (($file = readdir ($handle)) !== false)
{
  if (is_dir ($file))
          continue; // discard directories
 
  $ext = strstr ($file, "."); // get the file extension
 
  // is this file of one of the allowed extensions ?
  if ((strcasecmp ($ext, ".png") == 0)
          || (strcasecmp ($ext, ".jpeg") == 0)
          || (strcasecmp ($ext, ".gif") == 0))
  {
          $files[$file_count] = $file; // yes it is, add the file to the array
          $file_count++; // we know now one image more
  }
}
 
closedir ($handle); // finished, don't forget to close directory handle
 
// did we find no image ?
if ($file_count == 0)
  return; // if so, don't output anything at all.
 
// output a random image file among those in the array
readfile ($files[rand (0, $file_count - 1)]);
 
?>


SoUlFaThEr 12-05-2004 20:10

Re: a different sig each time
 
how in the world could a simple Joe like me get this one going? i like that kind of stuff!

Pierre-Marie Baty 12-05-2004 23:40

Re: a different sig each time
 
Well, just copy what's inside the code tags and paste it in your notepad, then save the file and name it "sig.php" or whatever. Then upload it to your server in the directory where you put your signatures, and instead of using [ img]http://soulfathermaps.de/images/mysig.jpg[ /img] use [ img]http://soulfathermaps.de/images/sig.php[ /img]
That should be enough
Ensure the directory where your sig is has read access allowed to anybody.

SoUlFaThEr 12-05-2004 23:45

Re: a different sig each time
 
sounds real easy :)

and how do i make it select 4 pics designed just for this purpose? and not the whole bank of pics?

Huntkillaz 13-05-2004 00:12

Re: a different sig each time
 
to my understanding (php is one of my things in the the things to learn list :()

if u upload the 4 photo's\images and the sig in the same folder to the server
and link upto that sig.php script it will automatically choose from that 4 becoz there is no other in that folder

FrostyCoolSlug 13-05-2004 02:55

Re: a different sig each time
 
Quote:

$ext = strstr ($file, "."); // get the file extension
You gotta be careful with this, some people like adding extra .'s to their filenames (god knows why), so if someone had a file called my.pic.jpg, $ext would be pic.jpg, which wont work in comparison with the others.

Quote:

while (($file = readdir ($handle)) !== false)
I'm not sure why that was changed, i used what was recommended @ php.net as the 'right' way to do it.

Mine was designed to be more customisable, rather than 'just work', so you could select the folder easily, and simple add , "ext" to add an extention to the list..

What i've done here, is taken the speed of yours, and the customisability of mine, and combined them, and got the following :p

PHP Code:

<?php

$target 
getcwd() . "/images";
$extentions = Array("PNG""JPG""GIF");
$files = Array();
$file_count 0;

$handle opendir($target) or die ();
while ((
$file readdir($handle)) !== false) {
    if (
is_dir ($file))
        continue;
    
$tmp explode("."$file);
    
$ext $file[count($file) - 1];
    unset(
$tmp);
    foreach (
$extentions as $m00) {
        if (
strcasecmp($ext$m00)) {
            
$files[$file_count++] = $file;
            continue 
2;
        } 
    } 


closedir ($handle);

if (!
$file_count)
    return;

readfile ($files[rand (0$file_count) -1]);

?>

Its not commented, but you should know what it does by now ;)

Pierre-Marie Baty 13-05-2004 04:34

Re: a different sig each time
 
@Frosty: About the "rand (0, $file_count - 1)", I suppose the array indexing works just like in C ?
Let's say we have an array of 3 elements (hence $file_count = 3)
we have
array[0] -> first element
array[1] -> second element
array[2] -> third element (and no array[3], which is out of bounds)
is that right ?
so if I want to pick a random number between 0 and 2 inclusively, I call rand (0, $file_list - 1)... well that's how I see things, but you know PHP better than me so... ???:(

@SF: with this script you have to arrange your pics into subdirectories, because it takes the whole image list.
If you want a script that would output only a selected set of images randomly, you can write something like this:
Code:

<?php

$files = Array("image1.gif", "image2.jpeg", "image3.png"); // list of files
$file_count = 3; // number of files to choose from

// take one file randomly among the list and send it away
readfile ($files[rand (0, $file_count - 1)]);

?>


FrostyCoolSlug 13-05-2004 04:45

Re: a different sig each time
 
hmm, thinking about it, i'm not sure..

aah, right you are, soz i'm in a VERY adjitated mood tonight (someone decided they didnt like a script i was working on, so MASACARED it (all for google ratings) now i have a messed up Webserver, as well as a non-functioning script which i spend 3 solid days coding..) so my heads not screwed on right :p

so yea, you are right, my bad, soz :/

SoUlFaThEr 13-05-2004 09:37

Re: a different sig each time
 
ill just throw them in a separate folder :) thanks

TruB 13-05-2004 12:47

Re: a different sig each time
 
oh.. great.. a explaination how to use it too..

FrostyCoolSlug 13-05-2004 12:54

Re: a different sig each time
 
ok, with my 'latest' script..

Make a Directory, give it a name (probably images)..
chuck all your images in there, then edit the following:

$target = getcwd() . "/images";

replace "/images" with your folder name.

If you want to use an extention which isnt in the list, change the following line

Array("PNG", "JPG", "GIF");

to

Array("PNG", "JPG", "GIF", "EXT", "EXT2", "ETC");

then just call the php file in a browser, and each time you refresh, you should get a new random image.

Then use the BBCode IMG tag, and point the URL to where your php script is, or use an <img src='http://your.domain/path/to/something.php'>

(something.php being the script filename)

and it should work..

(If you wernt being sarcastic about the explanation thing, i appologise ;))

FrostyCoolSlug 13-05-2004 13:01

Re: a different sig each time
 
As a note, if you can use .htaccess you can 'force' a file to a php script, so rename your php file to something.jpg (replace something with whatever you want), and add this to your .htaccess

<Files something.jpg>
ForceType application/x-httpd-php
</Files>

I just tested that, and it seems to work :)

SoUlFaThEr 15-05-2004 12:32

Re: a different sig each time
 
i have that php saved in my folder and i looked at it just now and it says mysig.php.txt :(

Pierre-Marie Baty 15-05-2004 18:57

Re: a different sig each time
 
Open a Windows Explorer window
Click on Tools -> Folder Options...
Switch to the 2nd tab
Uncheck the "Hide known file extensions" checkbox
Click OK && try again

Your file was just named .txt instead of .php by notepad, as long as you don't tell windows to show the file extensions it won't allow you to name a file the way you want.

FrostyCoolSlug 16-05-2004 13:16

Re: a different sig each time
 
Windows is evil like that :)

TruB 16-05-2004 13:53

Re: a different sig each time
 
hopefully longhorn will skip annoying things from earlier releases.. big plus for being based on unix..

Pierre-Marie Baty 16-05-2004 23:29

Re: a different sig each time
 
being based on Unix ??? wtf ???
where do you take this from ? ???:(


All times are GMT +2. The time now is 20:47.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.