110mb.com Forum
News: Currently none. But refer to this news before you post an error, maybe it's already being fixed Wink
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 05, 2008, 03:04:53 PM


Login with username, password and session length


Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: TIP: Random Image script  (Read 2016 times)
Mop (Gb)
Loyal 110MB Member
*******
Online Online

Posts: 3672


PHP Coder


WWW
« on: November 28, 2006, 12:15:57 AM »

How to create a script that, when accessed as an image, generates a random image every time its loaded:

1: Create a php file like this:

Code:
<?php
// Directory of images
$folder = 'images/';

// Do not edit any code below!
srand( time() );
if (
$directory = @opendir($folder))
{     
while (($image = readdir($directory)) !== false)     
{         
if ( eregi( '.(png|gif)$', $image) )         
{             
$images[] = $image;         
}     
}
closedir($directory);
}
$image = $images[rand() % sizeof( $images )];
if (
eregi( '.png$', $image ))
{     
header("Content-Type: image/png");
}
else
{     
header("Content-Type: image/gif");
}
header("Content-Length: " . filesize( $folder."/".$image ) );
readfile( $folder."/".$image );
?>

2: Save this file on your server, in a folder named 'random' (Or whatever name you want)

3: In the folder the Php file is in, create another folder and name it 'images' (You can name it something else, but in the script change the $folder variable to the name / path you use).  In this folder put all the .png / .gif images you want in the randomizer.

4: To put the random image on your site:
Code:
<image src="PATH TO PHP FILE">

Or as a sig on a forum:

Code:
[img]URL OF PHP FILE[/img]

Example of it:


(Refresh the page to change)

Enjoy!
 

Note: This script above is for .gif and .png images ONLY! You can change it to other formats by changeing all instances of 'gif' or 'png' to whatever format you want. (But who would want to, png and gif are the best formats anyway)

Note yet again: Some (bad) browers will show the image as not available. I know it works for sure on Safari / Firefox on Mac OsX, and beleive it works on Firefox for Windows. Also, I know it doesn't work on IE for Mac OsX. Not sure about other browers / systems.
« Last Edit: November 28, 2006, 12:18:57 AM by Gb » Logged



EnockNamun
Dream Castle Vacation Rentals
Hyper-Active Member
***
Offline Offline

Posts: 469


WWW
« Reply #1 on: October 30, 2007, 01:12:59 AM »

Mop, the scriptMASTER, is there a way to make this to where the image changes randomly every few seconds, without refreshing the page?  and can i even use php in a new 110mb account for my fansite/hobby page? (just for fun, so i don't really want to pay for this one)
« Last Edit: October 30, 2007, 01:18:10 AM by EnockNamun » Logged

inp o҉rtb
The Gangsta
Global Moderator
Official 110mb Guru
*****
Online Online

Posts: 11715


experimental theologian


WWW
« Reply #2 on: October 30, 2007, 01:36:35 AM »

Good shiz, Mop. Yo, you can use glob() instead of readdir(). Check it out ;]

Code:
if ($directory = @opendir($folder))
{     
while (($image = readdir($directory)) !== false)     
{         
if ( eregi( '.(png|gif)$', $image) )         
{             
$images[] = $image;         
}     
}
closedir($directory);
}

becomes

Code:
$images = array_merge(glob($folder."*.png"),glob($folder."*.gif"));

is there a way to make this to where the image changes randomly every few seconds, without refreshing the page?

Absolutely, using Javascript. Any clientside interaction/effect must be mediated using clientside technology. I'll let Mop do the actual coding because I'm too lazy ;]

can i even use php in a new 110mb account for my fansite/hobby page?

Absolutely. PHP is free.
Logged

Mop (Gb)
Loyal 110MB Member
*******
Online Online

Posts: 3672


PHP Coder


WWW
« Reply #3 on: November 01, 2007, 09:11:17 PM »

Woah, this is so old, I don't even remember posting it Lol.
Logged



Piotr GRD
Honoured 110MB Member
Loyal 110MB Member
*****
Offline Offline

Posts: 3849



WWW
« Reply #4 on: April 17, 2008, 06:17:42 AM »

Small improvements, if you don't mind, Mop. Smiley

Code:
<?php
// Directory of images
$folder = 'images/';
// put all images in this folder, it can be .gif .png .jpg .jpeg

// Do not edit any code below!
if ($directory = @opendir($folder))
{
   while ((
$image = readdir($directory)) !== false)
   {
      if (
eregi(".(png|gif|jpg|jpeg)$", $image))
      {
$images[] = $image; }
   }
   
closedir($directory);

   
$image = $folder . $images[mt_rand(0, (count($images) - 1))];
   
$ext = pathinfo($image, PATHINFO_EXTENSION);
   
$type = array('jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png');
   
   
header('Content-Type: ' . $type[$ext]);
   
header('Content-Length: ' . filesize($image));
   
readfile($image);
}
else
{ echo
'Can not open image directory'; }
?>


I am using little different script, but similar, for my new avatar grin
And AFTER creating it I just found this post. So I mixed Mop's and mine codes a little bit. Smiley

http://grd.110mb.com/pic/?avatar.jpg
Logged

  Duh
d3xt3r
Guest
« Reply #5 on: April 20, 2008, 06:07:33 AM »

Small improvements, if you don't mind, Mop. Smiley

Code:
<?php
// Directory of images
$folder = 'images/';
// put all images in this folder, it can be .gif .png .jpg .jpeg

// Do not edit any code below!
if ($directory = @opendir($folder))
{
   while ((
$image = readdir($directory)) !== false)
   {
      if (
eregi(".(png|gif|jpg|jpeg)$", $image))
      {
$images[] = $image; }
   }
   
closedir($directory);

   
$image = $folder . $images[mt_rand(0, (count($images) - 1))];
   
$ext = pathinfo($image, PATHINFO_EXTENSION);
   
$type = array('jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png');
   
   
header('Content-Type: ' . $type[$ext]);
   
header('Content-Length: ' . filesize($image));
   
readfile($image);
}
else
{ echo
'Can not open image directory'; }
?>


I am using little different script, but similar, for my new avatar grin
And AFTER creating it I just found this post. So I mixed Mop's and mine codes a little bit. Smiley

http://grd.110mb.com/pic/?avatar.jpg


I like your version. This is exactly something I wanted to do a while back but never did it. Now I don't have to write the code. Awesome. Thanks. Smiley
Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

  Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC

Theme by Dilber MC | Using SEO4SMF Mod

All content on this forum (especially posts) is copyright property of 110mb & the member
who wrote the post. So if you wish to steal anyone's post on this forum, you'll wish you
were dead because our lawyers will sue your ass!