Display Random Images Script - Image Rotation Code Pictures PHP Rotate HTML



Display Random Images Script - Image Rotation Code Pictures PHP Rotate HTML
 (380) Click. Work. Collect
Display Random Images Script - Image Rotation Code Pictures PHP Rotate HTML
Post Description:
Post Tags: display, random, images, script, image, rotation, code, pictures, php, rotate, html
This Post Has Been Viewed 2531 Times Since Mon Dec 11, 2006 9:41 pm Posted By braker with 3 replies
Display Random Images Script - Image Rotation Code Pictures PHP Rotate HTML
One of the many things you can do to make your website less boring is to display your visitors a different image everytime they visit your pages. You can do this PHP. While you can also do this with javascript, in this tutorial guide, i will show you how to generate random images and display them on your webpages using PHP.

The only requirement for this to work on your web pages is that you have PHP in your hosting. IF you don't have php, you can signup with www.webune.com for a php plan. if you have php, continue on...

the first thing i tell you to do is to open a text editor like notepad and transfer this code into a blank document:


     Code:
<?php
$image_directory = '.';
$image_extension = array();
$image_extension['gif'] = 'image/gif';
$image_extension['jpg'] = 'image/jpeg';
$image_extension['jpeg'] = 'image/jpeg';
$image_extension['png'] = 'image/png';

$image = null;
if (substr($image_directory,-1) != '/') {
$image_directory = $image_directory.'/';
}

if (isset($_GET['image'])) {
$img_info = pathinfo($_GET['image']);
if (
isset( $image_extension[ strtolower( $img_info['extension'] ) ] ) &&
file_exists( $image_directory.$img_info['basename'] )
) {
$image = $image_directory.$img_info['basename'];
}
} else {
$fileList = array();
$handle = opendir($image_directory);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $image_extension[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);

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

if ($image!=null) {
$img_info = pathinfo($image);
$contents = 'Content-type: '.$image_extension[ $img_info['extension'] ];
header ($contents);
readfile($image);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$picture = @imagecreate (100, 100)
or die ("Cannot initialize new GD image stream");
$bgcolor = imagecolorallocate ($picture, 255, 255, 255);
$font_color = imagecolorallocate ($picture, 0,0,0);
imagestring ($picture, 2, 5, 5, "IMAGE ERROR", $font_color);
imagepng ($picture);
imagedestroy($picture);
}
}
?>

this code will generate a random image for you. this is you can say the engine that makes it all work. now that you have transfered the code into notepad, you can save it as: wallpaperama-generator.php

once you haved samed it in your computer, then upload to your site. Remember, you must have php for this script to work, if you dont' have php yet, get a host who offers PHP. www.webune.com offers PHP Web hosting, check them out.

not all you have to do is to put this tag on whatever page you want to display the random images on a certain directory:


     Code:
<img src="http://yourdomain.com/wallpaperama-generator.php">


that's all the time i have right now. hope i helped you

Leave Your Comments     [ dejar commentarios ]
  * Name     [nombre]

  * eMail (will not be published)     [coreo electronico]

* Enter Your Reply or Comments:    [commentarios]


Add Picture To Comments         [incluir foto]
YES NO             upload
Receive Replies on my Comments (An email will be sent to you when someone replies to your comments)

     

Comments and replies About Display Random Images Script - Image Rotation Code Pictures PHP Rotate HTML




:: 1 :: #33073 - Reply By Thomas Cora On Tue Dec 25, 2007 10:50 pm
do you have a sample site where this runs... for some reason - i can't get it going....


i place the "img src=" yourdomain /wallpaperama-generator.php"


obviously i change my domain name... but no pictures show...
:: 2 :: #34352 - Reply By Mike On Thu Jan 03, 2008 1:06 pm
you have to put the php file in the same directory as your images. had the same error..
:: 3 :: #67208 - Reply By Doug On Tue Aug 19, 2008 6:39 am
can't get your script to work - see 'ya later.