FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Randomizing images..

 


Diablosblizz
Hey, does anybody have a script that randomizes a couple of images? I just need to to randomize a page or somesort. Hmm but that makes me think.. I need to have a script that allows it to also randomize google adsence scripts..
Anybody have any ideas?

Thanks!
wumingsden
I use the following script to load a random image..

Code:
<?php

// Copyright to Mezmorizing

if ($dir = opendir("."))
{
     $list = buildimagearray($dir);
     displayrandomimage($list);
}
function buildimagearray($dir)
{
     while (false !== ($file = readdir($dir)))
     {
          if (!is_dir($file) && getimagesize($file))
          {
               $list[] = $file;
          }
     }
     return $list;
}
function displayrandomimage($list)
{
     srand ((double) microtime() * 10000000);
     $sig = array_rand ($list);
     $size = getimagesize ($list[$sig]);
     $fp = fopen($list[$sig], "rb");

     if ($size && $fp)

     {
          header("Content-type: {$size['mime']}");
          fpassthru($fp);
          exit;
     }
}

?>


I name the file index.php and place it in a folder full of images, that way it will randomly load all of the images.

(I was unable to use pastebin.com for some reason. If you post a lot of code then please try and use pastebin first).
jeremyyak
In your title it says you need an image randomizer.
But in your post you are talking about a script randomizer for Google Ads.

Which one do you need, because they are completely different Razz
~Jeremy~
Diablosblizz
I need both, cause I would like to include google adsence!

wumingsden, that works for me. THANK YOU!
sonam
1. create new folder e.g.adsence
2. copy/paste adsence codes in different txt files eg. ads1.txt, ads2.txt, etc.
3. put this file below on your page...

Code:
<?php
$dir= $_SERVER['DOCUMENT_ROOT'] . "/adsence"; // path to folder
$openDir  = opendir($dir);
    while (false !== ($fileNames = readdir($openDir))) {
   if($fileNames == "." || $fileNames == "..") {
          continue;
                }  else {
          $ff[] = $fileNames;
                       }       
    }

shuffle($ff); //rand all files in adsence folder
echo $ff[0]; // print first one from randomized array

?>


If you want random images just create new folder with images and set right path to this folder.

Note:
If you want to see image you must put one more line before echo:

Code:
$img = @$_SERVER['DOC_ROOT'] . "/imageFolder/" . $ff[0];
echo $img;



Sonam
sonam
Sorry, sorry, sorry...
make mistake. Dont echo $ff[0] in adsence code. This will just echo file name. Otherwise put this two lines:

Code:
$ads = $_SERVER['DOCUMENT_ROOT'] . "/adsence/" . $ff[0];
echo $ads;


Sonam
Diablosblizz
Code:
<?php
$dir= $_SERVER['DOCUMENT_ROOT'] . "/adsence"; // path to folder
$openDir  = opendir($dir);
    while (false !== ($fileNames = readdir($openDir))) {
   if($fileNames == "." || $fileNames == "..") {
          continue;
                }  else {
          $ff[] = $fileNames;
                       }       
    }

shuffle($ff); //rand all files in adsence folder
echo $ff[0]; // print first one from randomized array

?>


All this does is give me the file name..

Code:
<?php
$dir= $_SERVER['DOCUMENT_ROOT'] . "/adsence"; // path to folder
$openDir  = opendir($dir);
    while (false !== ($fileNames = readdir($openDir))) {
   if($fileNames == "." || $fileNames == "..") {
          continue;
                }  else {
          $ff[] = $fileNames;
                       }       
    }

shuffle($ff); //rand all files in adsence folder
$img = @$_SERVER['DOC_ROOT'] . "/imageFolder/" . $ff[0];
echo $img;

?>


Gives me the full URL, what do I do?
sonam
Sorry, yesterday I am out of order, now I am testing script and i see mistake...

Adsence:
Code:
<?php
$dir= $_SERVER['DOCUMENT_ROOT'] . "/adsence"; // path to folder
$openDir  = opendir($dir);
    while (false !== ($fileNames = readdir($openDir))) {
   if($fileNames == "." || $fileNames == "..") {
          continue;
                }  else {
          $ff[] = $fileNames;
                       }       
    }

shuffle($ff); //rand all files in adsence folder

$ads = $_SERVER['DOCUMENT_ROOT'] . "/adsence/" . $ff[0];

include_once($ads); // print first one from randomized array

?>


images:
Code:
<?php
$dir= $_SERVER['DOCUMENT_ROOT'] . "/imageFolder"; // path to folder
$openDir  = opendir($dir);
    while (false !== ($fileNames = readdir($openDir))) {
   if($fileNames == "." || $fileNames == "..") {
          continue;
                }  else {
          $ff[] = $fileNames;
                       }       
    }

shuffle($ff); //rand all files in image folder

$img = @$_SERVER['DOC_ROOT'] . "/imageFolder/" . $ff[0];

echo "<img src='$img' width='200' height='300' border='0' >"; // images must have same dimension - print first one from randomized array

?>


Sonam
Diablosblizz
Thanks!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.